Skip to content

Commit

Permalink
feat(comp): Add descriptions for output format
Browse files Browse the repository at this point in the history
Ref: HIP 0008

When completing output formats, extra information will be shown
for shells that support completions (fish, zsh).  For example:

$ helm status -o <TAB>
json   -- Output result in JSON format
table  -- Output result in human-readable format
yaml   -- Output result in YAML format

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
  • Loading branch information
marckhouzam committed Feb 16, 2021
1 parent 1ae6e5f commit 1d3d961
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 6 additions & 2 deletions cmd/helm/flags.go
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"log"
"path/filepath"
"sort"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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
})

Expand Down
6 changes: 3 additions & 3 deletions 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
10 changes: 10 additions & 0 deletions pkg/cli/output/output.go
Expand Up @@ -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")

Expand Down

0 comments on commit 1d3d961

Please sign in to comment.