From 593b267ed57d6e1e52312dbe8c2145529ef88416 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sat, 14 Nov 2020 11:50:06 -0500 Subject: [PATCH] feat(comp): Add descriptions for output format Ref: HIP 0008 When completing output formats, extra information will be shown for shells that support completions (fish, zsh). For example: $ helm status -o json -- Output result in JSON format table -- Output result in human-readable format yaml -- Output result in YAML format Signed-off-by: Marc Khouzam --- cmd/helm/flags.go | 8 ++++++-- cmd/helm/testdata/output/output-comp.txt | 6 +++--- pkg/cli/output/output.go | 10 ++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cmd/helm/flags.go b/cmd/helm/flags.go index 92857a817..87dc23fdc 100644 --- a/cmd/helm/flags.go +++ b/cmd/helm/flags.go @@ -21,6 +21,7 @@ import ( "fmt" "log" "path/filepath" + "sort" "strings" "github.com/spf13/cobra" @@ -66,11 +67,14 @@ func bindOutputFlag(cmd *cobra.Command, varRef *output.Format) { err := cmd.RegisterFlagCompletionFunc(outputFlag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { var formatNames []string - for _, format := range output.Formats() { + for format, desc := range output.FormatsWithDesc() { if strings.HasPrefix(format, toComplete) { - formatNames = append(formatNames, format) + formatNames = append(formatNames, fmt.Sprintf("%s\t%s", format, desc)) } } + + // Sort the results to get a deterministic order for the tests + sort.Strings(formatNames) return formatNames, cobra.ShellCompDirectiveNoFileComp }) diff --git a/cmd/helm/testdata/output/output-comp.txt b/cmd/helm/testdata/output/output-comp.txt index e7799a56b..6232b2928 100644 --- a/cmd/helm/testdata/output/output-comp.txt +++ b/cmd/helm/testdata/output/output-comp.txt @@ -1,5 +1,5 @@ -table -json -yaml +json Output result in JSON format +table Output result in human-readable format +yaml Output result in YAML format :4 Completion ended with directive: ShellCompDirectiveNoFileComp diff --git a/pkg/cli/output/output.go b/pkg/cli/output/output.go index e4eb046fc..a46c977ad 100644 --- a/pkg/cli/output/output.go +++ b/pkg/cli/output/output.go @@ -40,6 +40,16 @@ func Formats() []string { return []string{Table.String(), JSON.String(), YAML.String()} } +// FormatsWithDesc returns a list of the string representation of the supported formats +// including a description +func FormatsWithDesc() map[string]string { + return map[string]string{ + Table.String(): "Output result in human-readable format", + JSON.String(): "Output result in JSON format", + YAML.String(): "Output result in YAML format", + } +} + // ErrInvalidFormatType is returned when an unsupported format type is used var ErrInvalidFormatType = fmt.Errorf("invalid format type")