Merge pull request #6641 from VilledeMontreal/feat/refactorOutput

ref(cmd): Use method to list formats
pull/6652/head
Taylor Thomas 6 years ago committed by GitHub
commit 38d4f33a0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,6 +18,7 @@ package main
import ( import (
"fmt" "fmt"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -51,7 +52,8 @@ func addChartPathOptionsFlags(f *pflag.FlagSet, c *action.ChartPathOptions) {
// bindOutputFlag will add the output flag to the given command and bind the // bindOutputFlag will add the output flag to the given command and bind the
// value to the given format pointer // value to the given format pointer
func bindOutputFlag(cmd *cobra.Command, varRef *output.Format) { 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)) 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 // Setup shell completion for the flag
cmd.MarkFlagCustom(outputFlag, "__helm_output_options") cmd.MarkFlagCustom(outputFlag, "__helm_output_options")
} }

@ -17,13 +17,16 @@ limitations under the License.
package main // import "helm.sh/helm/v3/cmd/helm" package main // import "helm.sh/helm/v3/cmd/helm"
import ( import (
"fmt"
"io" "io"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"helm.sh/helm/v3/cmd/helm/require" "helm.sh/helm/v3/cmd/helm/require"
"helm.sh/helm/v3/internal/experimental/registry" "helm.sh/helm/v3/internal/experimental/registry"
"helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli/output"
) )
const ( const (
@ -92,7 +95,7 @@ __helm_get_namespaces()
__helm_output_options() __helm_output_options()
{ {
__helm_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}" __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() __helm_binary_name()
@ -209,7 +212,7 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string
Long: globalUsage, Long: globalUsage,
SilenceUsage: true, SilenceUsage: true,
Args: require.NoArgs, Args: require.NoArgs,
BashCompletionFunction: bashCompletionFunc, BashCompletionFunction: fmt.Sprintf(bashCompletionFunc, strings.Join(output.Formats(), " ")),
} }
flags := cmd.PersistentFlags() flags := cmd.PersistentFlags()

@ -35,10 +35,15 @@ const (
YAML Format = "yaml" YAML Format = "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 // ErrInvalidFormatType is returned when an unsupported format type is used
var ErrInvalidFormatType = fmt.Errorf("invalid format type") 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 { func (o Format) String() string {
return string(o) return string(o)
} }

Loading…
Cancel
Save