From 65c7b6cafce3d239b33c4a20b52e3bb342528e95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Sat, 10 Aug 2019 16:49:37 -0400 Subject: [PATCH 1/2] Add helper to generate docs for --format flags --- common/utils.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/common/utils.go b/common/utils.go index 416b528..b65113f 100644 --- a/common/utils.go +++ b/common/utils.go @@ -3,6 +3,7 @@ package common import ( "errors" "fmt" + "reflect" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -94,6 +95,34 @@ func selectValue(jsonMap map[string]interface{}, jsonPath []string) (interface{} } } +func GetFormatHelp(v interface{}) (r string) { + typeOfV := reflect.TypeOf(v) + r = fmt.Sprintf(` +Format: + The --format flag accepts a Go template, which is passed a %s.%s object: + +%s +`, typeOfV.PkgPath(), typeOfV.Name(), fmt.Sprintf("%s%s", " ", repr(typeOfV, " ", " "))) + return +} + +func repr(t reflect.Type, margin, beforeMargin string) (r string) { + switch t.Kind() { + case reflect.Struct: + r = fmt.Sprintln("{") + for i := 0; i < t.NumField(); i++ { + tField := t.Field(i) + r += fmt.Sprintln(fmt.Sprintf("%s%s%s %s", beforeMargin, margin, tField.Name, repr(tField.Type, margin, beforeMargin+margin))) + } + r += fmt.Sprintf("%s}", beforeMargin) + case reflect.Array, reflect.Slice: + r = fmt.Sprintf("[]%s", repr(t.Elem(), margin, beforeMargin)) + default: + r = fmt.Sprintf("%s", t.Name()) + } + return +} + // Custom customerrors type StackNotFoundError struct { StackName string From 9551afd432be4a1b7321a3ea568829124a577ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Sat, 10 Aug 2019 16:51:36 -0400 Subject: [PATCH 2/2] Integrate --format flag docs helper with commands --- cmd/configList.go | 11 +---------- cmd/endpointGroupList.go | 4 ++++ cmd/endpointList.go | 4 ++++ cmd/stackList.go | 2 ++ cmd/status.go | 4 ++++ 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/cmd/configList.go b/cmd/configList.go index 4f04c90..282abc3 100644 --- a/cmd/configList.go +++ b/cmd/configList.go @@ -92,16 +92,7 @@ func init() { configListCmd.Flags().String("format", "", "Format output using a Go template.") viper.BindPFlag("config.list.format", configListCmd.Flags().Lookup("format")) - configListCmd.SetUsageTemplate(configListCmd.UsageTemplate() + ` -Format: - The --format flag accepts a Go template, which is executed in an object with this structure: - - { - Key string - EnvironmentVariable string - CurrentValue string - } -`) + configListCmd.SetUsageTemplate(configListCmd.UsageTemplate() + common.GetFormatHelp(config{})) } type config struct { diff --git a/cmd/endpointGroupList.go b/cmd/endpointGroupList.go index a1bccc7..f33323e 100644 --- a/cmd/endpointGroupList.go +++ b/cmd/endpointGroupList.go @@ -5,6 +5,8 @@ import ( "os" "text/template" + portainer "github.com/portainer/portainer/api" + "github.com/greenled/portainer-stack-utils/common" "github.com/sirupsen/logrus" "github.com/spf13/viper" @@ -62,4 +64,6 @@ func init() { endpointGroupListCmd.Flags().String("format", "", "Format output using a Go template.") viper.BindPFlag("endpoint.group.list.format", endpointGroupListCmd.Flags().Lookup("format")) + + endpointGroupListCmd.SetUsageTemplate(endpointGroupListCmd.UsageTemplate() + common.GetFormatHelp(portainer.EndpointGroup{})) } diff --git a/cmd/endpointList.go b/cmd/endpointList.go index 1ec3c70..5edfc29 100644 --- a/cmd/endpointList.go +++ b/cmd/endpointList.go @@ -21,6 +21,8 @@ import ( "os" "text/template" + portainer "github.com/portainer/portainer/api" + "github.com/greenled/portainer-stack-utils/common" "github.com/sirupsen/logrus" "github.com/spf13/viper" @@ -90,4 +92,6 @@ func init() { endpointListCmd.Flags().String("format", "", "Format output using a Go template.") viper.BindPFlag("endpoint.list.format", endpointListCmd.Flags().Lookup("format")) + + endpointListCmd.SetUsageTemplate(endpointListCmd.UsageTemplate() + common.GetFormatHelp(portainer.Endpoint{})) } diff --git a/cmd/stackList.go b/cmd/stackList.go index b25dbc5..7fb726b 100644 --- a/cmd/stackList.go +++ b/cmd/stackList.go @@ -98,4 +98,6 @@ func init() { stackListCmd.Flags().String("format", "", "Format output using a Go template.") viper.BindPFlag("stack.list.endpoint", stackListCmd.Flags().Lookup("endpoint")) viper.BindPFlag("stack.list.format", stackListCmd.Flags().Lookup("format")) + + stackListCmd.SetUsageTemplate(stackListCmd.UsageTemplate() + common.GetFormatHelp(portainer.Stack{})) } diff --git a/cmd/status.go b/cmd/status.go index 3a15a31..df59a0d 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -5,6 +5,8 @@ import ( "os" "text/template" + portainer "github.com/portainer/portainer/api" + "github.com/greenled/portainer-stack-utils/common" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -58,4 +60,6 @@ func init() { statusCmd.Flags().String("format", "", "Format output using a Go template.") viper.BindPFlag("status.format", statusCmd.Flags().Lookup("format")) + + statusCmd.SetUsageTemplate(statusCmd.UsageTemplate() + common.GetFormatHelp(portainer.Status{})) }