update PrintFlags#Complete to receive string template

This commit is contained in:
juanvallejo 2018-04-06 11:36:52 -04:00
parent b8dc20646c
commit 2a202cf49a
No known key found for this signature in database
GPG Key ID: 7D2C958002D6448D
14 changed files with 54 additions and 64 deletions

View File

@ -215,13 +215,17 @@ func (o *EnvOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
o.Resources = resources o.Resources = resources
o.Cmd = cmd o.Cmd = cmd
o.PrintFlags.Complete(o.DryRun) if o.DryRun {
// TODO(juanvallejo): This can be cleaned up even further by creating
// a PrintFlags struct that binds the --dry-run flag, and whose
// ToPrinter method returns a printer that understands how to print
// this success message.
o.PrintFlags.Complete("%s (dry run)")
}
printer, err := o.PrintFlags.ToPrinter() printer, err := o.PrintFlags.ToPrinter()
if err != nil { if err != nil {
return err return err
} }
o.PrintObj = func(obj runtime.Object) error { o.PrintObj = func(obj runtime.Object) error {
return printer.PrintObj(obj, o.Out) return printer.PrintObj(obj, o.Out)
} }

View File

@ -74,7 +74,7 @@ func TestSetEnvLocal(t *testing.T) {
opts := EnvOptions{ opts := EnvOptions{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },
@ -123,7 +123,7 @@ func TestSetMultiResourcesEnvLocal(t *testing.T) {
opts := EnvOptions{ opts := EnvOptions{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },
@ -514,7 +514,7 @@ func TestSetEnvRemote(t *testing.T) {
opts := EnvOptions{ opts := EnvOptions{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },

View File

@ -127,7 +127,9 @@ func (o *ImageOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
o.ResolveImage = f.ResolveImage o.ResolveImage = f.ResolveImage
o.Cmd = cmd o.Cmd = cmd
o.PrintFlags.Complete(o.DryRun) if o.DryRun {
o.PrintFlags.Complete("%s (dry run)")
}
printer, err := o.PrintFlags.ToPrinter() printer, err := o.PrintFlags.ToPrinter()
if err != nil { if err != nil {
@ -250,6 +252,11 @@ func (o *ImageOptions) Run() error {
continue continue
} }
// no changes
if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {
continue
}
if o.Local || o.DryRun { if o.Local || o.DryRun {
if err := o.PrintObj(patch.Info.AsVersioned()); err != nil { if err := o.PrintObj(patch.Info.AsVersioned()); err != nil {
return err return err
@ -257,11 +264,6 @@ func (o *ImageOptions) Run() error {
continue continue
} }
// no changes
if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {
continue
}
// patch the change // patch the change
obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch) obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
if err != nil { if err != nil {

View File

@ -74,7 +74,7 @@ func TestImageLocal(t *testing.T) {
opts := ImageOptions{ opts := ImageOptions{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },
@ -100,7 +100,7 @@ func TestImageLocal(t *testing.T) {
func TestSetImageValidation(t *testing.T) { func TestSetImageValidation(t *testing.T) {
printFlags := &printers.PrintFlags{ printFlags := &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
} }
testCases := []struct { testCases := []struct {
@ -196,7 +196,7 @@ func TestSetMultiResourcesImageLocal(t *testing.T) {
opts := ImageOptions{ opts := ImageOptions{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },
@ -590,7 +590,7 @@ func TestSetImageRemote(t *testing.T) {
opts := ImageOptions{ opts := ImageOptions{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },

View File

@ -148,13 +148,13 @@ func (o *ResourcesOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args
o.Cmd = cmd o.Cmd = cmd
o.DryRun = cmdutil.GetDryRunFlag(o.Cmd) o.DryRun = cmdutil.GetDryRunFlag(o.Cmd)
o.PrintFlags.Complete(o.DryRun) if o.DryRun {
o.PrintFlags.Complete("%s (dry run)")
}
printer, err := o.PrintFlags.ToPrinter() printer, err := o.PrintFlags.ToPrinter()
if err != nil { if err != nil {
return err return err
} }
o.PrintObj = func(obj runtime.Object) error { o.PrintObj = func(obj runtime.Object) error {
return printer.PrintObj(obj, o.Out) return printer.PrintObj(obj, o.Out)
} }

View File

@ -73,7 +73,7 @@ func TestResourcesLocal(t *testing.T) {
opts := ResourcesOptions{ opts := ResourcesOptions{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },
@ -127,7 +127,7 @@ func TestSetMultiResourcesLimitsLocal(t *testing.T) {
opts := ResourcesOptions{ opts := ResourcesOptions{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },
@ -508,7 +508,7 @@ func TestSetResourcesRemote(t *testing.T) {
opts := ResourcesOptions{ opts := ResourcesOptions{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },

View File

@ -53,7 +53,6 @@ type SelectorOptions struct {
selector *metav1.LabelSelector selector *metav1.LabelSelector
out io.Writer out io.Writer
PrintObject func(obj runtime.Object) error
ClientForMapping func(mapping *meta.RESTMapping) (resource.RESTClient, error) ClientForMapping func(mapping *meta.RESTMapping) (resource.RESTClient, error)
PrintObj func(runtime.Object) error PrintObj func(runtime.Object) error
@ -119,8 +118,6 @@ func (o *SelectorOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
o.dryrun = cmdutil.GetDryRunFlag(cmd) o.dryrun = cmdutil.GetDryRunFlag(cmd)
o.output = cmdutil.GetFlagString(cmd, "output") o.output = cmdutil.GetFlagString(cmd, "output")
o.PrintFlags.Complete(o.dryrun)
cmdNamespace, enforceNamespace, err := f.DefaultNamespace() cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
if err != nil { if err != nil {
return err return err
@ -158,14 +155,17 @@ func (o *SelectorOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
} }
} }
if o.dryrun {
o.PrintFlags.Complete("%s (dry run)")
}
printer, err := o.PrintFlags.ToPrinter() printer, err := o.PrintFlags.ToPrinter()
if err != nil { if err != nil {
return err return err
} }
o.PrintObj = func(obj runtime.Object) error {
o.PrintObject = func(obj runtime.Object) error {
return printer.PrintObj(obj, o.out) return printer.PrintObj(obj, o.out)
} }
o.ClientForMapping = func(mapping *meta.RESTMapping) (resource.RESTClient, error) { o.ClientForMapping = func(mapping *meta.RESTMapping) (resource.RESTClient, error) {
return f.ClientForMapping(mapping) return f.ClientForMapping(mapping)
} }
@ -208,7 +208,7 @@ func (o *SelectorOptions) RunSelector() error {
return patch.Err return patch.Err
} }
if o.local || o.dryrun { if o.local || o.dryrun {
return o.PrintObject(info.Object) return o.PrintObj(info.Object)
} }
patched, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch) patched, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
@ -225,7 +225,7 @@ func (o *SelectorOptions) RunSelector() error {
} }
info.Refresh(patched, true) info.Refresh(patched, true)
return o.PrintObject(patch.Info.AsVersioned()) return o.PrintObj(patch.Info.AsVersioned())
}) })
} }

View File

@ -120,13 +120,13 @@ func (saConfig *serviceAccountConfig) Complete(f cmdutil.Factory, cmd *cobra.Com
saConfig.updatePodSpecForObject = f.UpdatePodSpecForObject saConfig.updatePodSpecForObject = f.UpdatePodSpecForObject
saConfig.cmd = cmd saConfig.cmd = cmd
saConfig.PrintFlags.Complete(saConfig.dryRun) if saConfig.dryRun {
saConfig.PrintFlags.Complete("%s (dry run)")
}
printer, err := saConfig.PrintFlags.ToPrinter() printer, err := saConfig.PrintFlags.ToPrinter()
if err != nil { if err != nil {
return err return err
} }
saConfig.PrintObj = func(obj runtime.Object) error { saConfig.PrintObj = func(obj runtime.Object) error {
return printer.PrintObj(obj, saConfig.out) return printer.PrintObj(obj, saConfig.out)
} }

View File

@ -92,7 +92,7 @@ func TestSetServiceAccountLocal(t *testing.T) {
saConfig := serviceAccountConfig{ saConfig := serviceAccountConfig{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },
@ -136,7 +136,7 @@ func TestSetServiceAccountMultiLocal(t *testing.T) {
opts := serviceAccountConfig{ opts := serviceAccountConfig{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },
@ -380,7 +380,7 @@ func TestSetServiceAccountRemote(t *testing.T) {
saConfig := serviceAccountConfig{ saConfig := serviceAccountConfig{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },
@ -427,7 +427,7 @@ func TestServiceAccountValidation(t *testing.T) {
saConfig := &serviceAccountConfig{ saConfig := &serviceAccountConfig{
PrintFlags: &printers.PrintFlags{ PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),
NamePrintFlags: printers.NewNamePrintFlags("", false), NamePrintFlags: printers.NewNamePrintFlags(""),
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
}, },

View File

@ -74,7 +74,7 @@ type SubjectOptions struct {
Groups []string Groups []string
ServiceAccounts []string ServiceAccounts []string
PrintObject func(obj runtime.Object) error PrintObj func(obj runtime.Object) error
} }
func NewCmdSubject(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { func NewCmdSubject(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
@ -117,14 +117,14 @@ func (o *SubjectOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
o.Output = cmdutil.GetFlagString(cmd, "output") o.Output = cmdutil.GetFlagString(cmd, "output")
o.DryRun = cmdutil.GetDryRunFlag(cmd) o.DryRun = cmdutil.GetDryRunFlag(cmd)
o.PrintFlags.Complete(o.DryRun) if o.DryRun {
o.PrintFlags.Complete("%s (dry run)")
}
printer, err := o.PrintFlags.ToPrinter() printer, err := o.PrintFlags.ToPrinter()
if err != nil { if err != nil {
return err return err
} }
o.PrintObj = func(obj runtime.Object) error {
o.PrintObject = func(obj runtime.Object) error {
return printer.PrintObj(obj, o.Out) return printer.PrintObj(obj, o.Out)
} }
@ -251,7 +251,7 @@ func (o *SubjectOptions) Run(f cmdutil.Factory, fn updateSubjects) error {
} }
if o.Local || o.DryRun { if o.Local || o.DryRun {
if err := o.PrintObject(info.Object); err != nil { if err := o.PrintObj(info.Object); err != nil {
return err return err
} }
continue continue
@ -264,7 +264,7 @@ func (o *SubjectOptions) Run(f cmdutil.Factory, fn updateSubjects) error {
} }
info.Refresh(obj, true) info.Refresh(obj, true)
return o.PrintObject(info.AsVersioned()) return o.PrintObj(info.AsVersioned())
} }
return utilerrors.NewAggregate(allErrs) return utilerrors.NewAggregate(allErrs)
} }

View File

@ -45,8 +45,8 @@ type PrintFlags struct {
OutputFormat *string OutputFormat *string
} }
func (f *PrintFlags) Complete(dryRun bool) error { func (f *PrintFlags) Complete(messageTemplate string) error {
f.NamePrintFlags.DryRun = dryRun f.NamePrintFlags.Operation = fmt.Sprintf(messageTemplate, f.NamePrintFlags.Operation)
return nil return nil
} }
@ -80,6 +80,6 @@ func NewPrintFlags(operation string) *PrintFlags {
OutputFormat: &outputFormat, OutputFormat: &outputFormat,
JSONYamlPrintFlags: NewJSONYamlPrintFlags(), JSONYamlPrintFlags: NewJSONYamlPrintFlags(),
NamePrintFlags: NewNamePrintFlags(operation, false), NamePrintFlags: NewNamePrintFlags(operation),
} }
} }

View File

@ -31,10 +31,6 @@ import (
// a resource's fully-qualified Kind.group/name, or a successful // a resource's fully-qualified Kind.group/name, or a successful
// message about that resource if an Operation is provided. // message about that resource if an Operation is provided.
type NamePrintFlags struct { type NamePrintFlags struct {
// DryRun indicates whether the "(dry run)" message
// should be appended to the finalized "successful"
// message printed about an action on an object.
DryRun bool
// Operation describes the name of the action that // Operation describes the name of the action that
// took place on an object, to be included in the // took place on an object, to be included in the
// finalized "successful" message. // finalized "successful" message.
@ -53,10 +49,6 @@ func (f *NamePrintFlags) ToPrinter(outputFormat string) (ResourcePrinter, error)
Decoders: decoders, Decoders: decoders,
} }
if f.DryRun {
namePrinter.Operation = namePrinter.Operation + " (dry run)"
}
outputFormat = strings.ToLower(outputFormat) outputFormat = strings.ToLower(outputFormat)
switch outputFormat { switch outputFormat {
case "name": case "name":
@ -75,9 +67,8 @@ func (f *NamePrintFlags) AddFlags(c *cobra.Command) {}
// NewNamePrintFlags returns flags associated with // NewNamePrintFlags returns flags associated with
// --name printing, with default values set. // --name printing, with default values set.
func NewNamePrintFlags(operation string, dryRun bool) *NamePrintFlags { func NewNamePrintFlags(operation string) *NamePrintFlags {
return &NamePrintFlags{ return &NamePrintFlags{
Operation: operation, Operation: operation,
DryRun: dryRun,
} }
} }

View File

@ -49,12 +49,6 @@ func TestNamePrinterSupportsExpectedFormats(t *testing.T) {
operation: "patched", operation: "patched",
expectedOutput: "pod/foo", expectedOutput: "pod/foo",
}, },
{
name: "empty output format and an operation prints success message with dry run",
operation: "patched",
dryRun: true,
expectedOutput: "pod/foo patched (dry run)",
},
{ {
name: "operation and no valid \"name\" output does not match a printer", name: "operation and no valid \"name\" output does not match a printer",
operation: "patched", operation: "patched",
@ -83,7 +77,6 @@ func TestNamePrinterSupportsExpectedFormats(t *testing.T) {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
printFlags := printers.NamePrintFlags{ printFlags := printers.NamePrintFlags{
Operation: tc.operation, Operation: tc.operation,
DryRun: tc.dryRun,
} }
p, err := printFlags.ToPrinter(tc.outputFormat) p, err := printFlags.ToPrinter(tc.outputFormat)

View File

@ -42,7 +42,7 @@ func GetStandardPrinter(typer runtime.ObjectTyper, encoder runtime.Encoder, deco
printer = p printer = p
case "name": case "name":
nameFlags := NewNamePrintFlags("", false) nameFlags := NewNamePrintFlags("")
namePrinter, err := nameFlags.ToPrinter(format) namePrinter, err := nameFlags.ToPrinter(format)
if err != nil { if err != nil {
return nil, err return nil, err