Skip to content

Commit

Permalink
re-make print flags composeable for sophisticated callers
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Jul 10, 2018
1 parent bc6fac9 commit fabe703
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions pkg/kubectl/genericclioptions/print_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
Expand Down Expand Up @@ -63,9 +62,11 @@ type PrintFlags struct {

TypeSetterPrinter *printers.TypeSetterPrinter

OutputFormat *string
outputFlag *pflag.Flag
outputDefaulted bool
OutputFormat *string

// OutputFlagSpecified indicates whether the user specifically requested a certain kind of output.
// Using this function allows a sophisticated caller to change the flag binding logic if they so desire.
OutputFlagSpecified func() bool
}

func (f *PrintFlags) Complete(successTemplate string) error {
Expand All @@ -86,11 +87,13 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
outputFormat = *f.OutputFormat
}
// For backwards compatibility we want to support a --template argument given, even when no --output format is provided.
// If a default output format has been set, but no explicit output format has been provided via the --output flag, fallback
// If no explicit output format has been provided via the --output flag, fallback
// to honoring the --template argument.
if f.TemplatePrinterFlags != nil && f.TemplatePrinterFlags.TemplateArgument != nil &&
len(*f.TemplatePrinterFlags.TemplateArgument) > 0 &&
(len(outputFormat) == 0 || (f.outputDefaulted && f.outputFlag != nil && !f.outputFlag.Changed)) {
templateFlagSpecified := f.TemplatePrinterFlags != nil &&
f.TemplatePrinterFlags.TemplateArgument != nil &&
len(*f.TemplatePrinterFlags.TemplateArgument) > 0
outputFlagSpecified := f.OutputFlagSpecified != nil && f.OutputFlagSpecified()
if templateFlagSpecified && !outputFlagSpecified {
outputFormat = "go-template"
}

Expand Down Expand Up @@ -122,14 +125,17 @@ func (f *PrintFlags) AddFlags(cmd *cobra.Command) {

if f.OutputFormat != nil {
cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, fmt.Sprintf("Output format. One of: %s.", strings.Join(f.AllowedFormats(), "|")))
f.outputFlag = cmd.Flag("output")
if f.OutputFlagSpecified == nil {
f.OutputFlagSpecified = func() bool {
return cmd.Flag("output").Changed
}
}
}
}

// WithDefaultOutput sets a default output format if one is not provided through a flag value
func (f *PrintFlags) WithDefaultOutput(output string) *PrintFlags {
f.OutputFormat = &output
f.outputDefaulted = true
return f
}

Expand Down

0 comments on commit fabe703

Please sign in to comment.