From 7fd384c8fb5f54f6fc666db6d903008de0d87f4c Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Thu, 10 Oct 2019 15:49:18 -0400 Subject: [PATCH 1/2] ref(cmd): Use method to list formats This isolates the listing of the different formats to the output.go file. It is more future-proof if another format is added. Signed-off-by: Marc Khouzam --- cmd/helm/flags.go | 10 +++++++++- cmd/helm/root.go | 13 +++++++++++-- pkg/cli/output/output.go | 5 +++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/cmd/helm/flags.go b/cmd/helm/flags.go index 32d5b891b..a3cf59bae 100644 --- a/cmd/helm/flags.go +++ b/cmd/helm/flags.go @@ -18,6 +18,7 @@ package main import ( "fmt" + "strings" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -51,7 +52,14 @@ func addChartPathOptionsFlags(f *pflag.FlagSet, c *action.ChartPathOptions) { // bindOutputFlag will add the output flag to the given command and bind the // value to the given format pointer func bindOutputFlag(cmd *cobra.Command, varRef *output.Format) { - cmd.Flags().VarP(newOutputValue(output.Table, varRef), outputFlag, "o", fmt.Sprintf("prints the output in the specified format. Allowed values: %s, %s, %s", output.Table, output.JSON, output.YAML)) + var formats strings.Builder + for index, format := range output.Formats() { + if index != 0 { + formats.WriteString(", ") + } + formats.WriteString(format.String()) + } + cmd.Flags().VarP(newOutputValue(output.Table, varRef), outputFlag, "o", fmt.Sprintf("prints the output in the specified format. Allowed values: %s", formats.String())) // Setup shell completion for the flag cmd.MarkFlagCustom(outputFlag, "__helm_output_options") } diff --git a/cmd/helm/root.go b/cmd/helm/root.go index e28e8bdfb..0fed728e8 100644 --- a/cmd/helm/root.go +++ b/cmd/helm/root.go @@ -17,13 +17,16 @@ limitations under the License. package main // import "helm.sh/helm/v3/cmd/helm" import ( + "fmt" "io" + "strings" "github.com/spf13/cobra" "helm.sh/helm/v3/cmd/helm/require" "helm.sh/helm/v3/internal/experimental/registry" "helm.sh/helm/v3/pkg/action" + "helm.sh/helm/v3/pkg/cli/output" ) const ( @@ -92,7 +95,7 @@ __helm_get_namespaces() __helm_output_options() { __helm_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}" - COMPREPLY+=( $( compgen -W "table json yaml" -- "$cur" ) ) + COMPREPLY+=( $( compgen -W "%[1]s" -- "$cur" ) ) } __helm_binary_name() @@ -203,13 +206,19 @@ By default, the default directories depend on the Operating System. The defaults ` func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string) *cobra.Command { + var formats strings.Builder + for _, format := range output.Formats() { + formats.WriteString(format.String()) + formats.WriteByte(' ') + } + cmd := &cobra.Command{ Use: "helm", Short: "The Helm package manager for Kubernetes.", Long: globalUsage, SilenceUsage: true, Args: require.NoArgs, - BashCompletionFunction: bashCompletionFunc, + BashCompletionFunction: fmt.Sprintf(bashCompletionFunc, formats.String()), } flags := cmd.PersistentFlags() diff --git a/pkg/cli/output/output.go b/pkg/cli/output/output.go index da9ee63a8..3e6a52a2c 100644 --- a/pkg/cli/output/output.go +++ b/pkg/cli/output/output.go @@ -35,6 +35,11 @@ const ( YAML Format = "yaml" ) +// Formats returns a list of supported formats +func Formats() []Format { + return []Format{Table, JSON, YAML} +} + // ErrInvalidFormatType is returned when an unsupported format type is used var ErrInvalidFormatType = fmt.Errorf("invalid format type") From 483904656bad9d4cf98df7058a964e468050947a Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Thu, 10 Oct 2019 22:39:18 -0400 Subject: [PATCH 2/2] ref(cmd): Use string method to list formats This greatly simplifies how to obtain the list of output.Format. It no longer provides a way to list all output.Format, but focuses on providing a list of string representation of output.Format, as this is what is actually needed. Signed-off-by: Marc Khouzam --- cmd/helm/flags.go | 10 ++-------- cmd/helm/root.go | 8 +------- pkg/cli/output/output.go | 8 ++++---- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/cmd/helm/flags.go b/cmd/helm/flags.go index a3cf59bae..467abbd6e 100644 --- a/cmd/helm/flags.go +++ b/cmd/helm/flags.go @@ -52,14 +52,8 @@ func addChartPathOptionsFlags(f *pflag.FlagSet, c *action.ChartPathOptions) { // bindOutputFlag will add the output flag to the given command and bind the // value to the given format pointer func bindOutputFlag(cmd *cobra.Command, varRef *output.Format) { - var formats strings.Builder - for index, format := range output.Formats() { - if index != 0 { - formats.WriteString(", ") - } - formats.WriteString(format.String()) - } - cmd.Flags().VarP(newOutputValue(output.Table, varRef), outputFlag, "o", fmt.Sprintf("prints the output in the specified format. Allowed values: %s", formats.String())) + cmd.Flags().VarP(newOutputValue(output.Table, varRef), outputFlag, "o", + fmt.Sprintf("prints the output in the specified format. Allowed values: %s", strings.Join(output.Formats(), ", "))) // Setup shell completion for the flag cmd.MarkFlagCustom(outputFlag, "__helm_output_options") } diff --git a/cmd/helm/root.go b/cmd/helm/root.go index 0fed728e8..644a9d7db 100644 --- a/cmd/helm/root.go +++ b/cmd/helm/root.go @@ -206,19 +206,13 @@ By default, the default directories depend on the Operating System. The defaults ` func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string) *cobra.Command { - var formats strings.Builder - for _, format := range output.Formats() { - formats.WriteString(format.String()) - formats.WriteByte(' ') - } - cmd := &cobra.Command{ Use: "helm", Short: "The Helm package manager for Kubernetes.", Long: globalUsage, SilenceUsage: true, Args: require.NoArgs, - BashCompletionFunction: fmt.Sprintf(bashCompletionFunc, formats.String()), + BashCompletionFunction: fmt.Sprintf(bashCompletionFunc, strings.Join(output.Formats(), " ")), } flags := cmd.PersistentFlags() diff --git a/pkg/cli/output/output.go b/pkg/cli/output/output.go index 3e6a52a2c..e4eb046fc 100644 --- a/pkg/cli/output/output.go +++ b/pkg/cli/output/output.go @@ -35,15 +35,15 @@ const ( YAML Format = "yaml" ) -// Formats returns a list of supported formats -func Formats() []Format { - return []Format{Table, JSON, YAML} +// Formats returns a list of the string representation of the supported formats +func Formats() []string { + return []string{Table.String(), JSON.String(), YAML.String()} } // ErrInvalidFormatType is returned when an unsupported format type is used var ErrInvalidFormatType = fmt.Errorf("invalid format type") -// String returns the string reprsentation of the Format +// String returns the string representation of the Format func (o Format) String() string { return string(o) }