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 <marc.khouzam@montreal.ca>
pull/6641/head
Marc Khouzam 5 years ago
parent 7fd384c8fb
commit 483904656b

@ -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")
}

@ -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()

@ -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)
}

Loading…
Cancel
Save