diff --git a/pkg/kubectl/cmd/apply.go b/pkg/kubectl/cmd/apply.go index a3c3019056e..a8ec0eed5f1 100644 --- a/pkg/kubectl/cmd/apply.go +++ b/pkg/kubectl/cmd/apply.go @@ -292,7 +292,7 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti count++ if len(output) > 0 && !shortOutput { - return cmdutil.PrintResourceInfoForCommand(cmd, info, f, out) + return f.PrintResourceInfoForCommand(cmd, info, out) } cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "created") return nil @@ -344,7 +344,7 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti } count++ if len(output) > 0 && !shortOutput { - return cmdutil.PrintResourceInfoForCommand(cmd, info, f, out) + return f.PrintResourceInfoForCommand(cmd, info, out) } cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "configured") return nil diff --git a/pkg/kubectl/cmd/apply_set_last_applied.go b/pkg/kubectl/cmd/apply_set_last_applied.go index 1bc06902f08..095d16ef7c3 100644 --- a/pkg/kubectl/cmd/apply_set_last_applied.go +++ b/pkg/kubectl/cmd/apply_set_last_applied.go @@ -190,7 +190,7 @@ func (o *SetLastAppliedOptions) RunSetLastApplied(f cmdutil.Factory, cmd *cobra. if len(o.Output) > 0 && !o.ShortOutput { info.Refresh(patchedObj, false) - return cmdutil.PrintResourceInfoForCommand(cmd, info, f, o.Out) + return f.PrintResourceInfoForCommand(cmd, info, o.Out) } cmdutil.PrintSuccess(o.Mapper, o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, o.DryRun, "configured") diff --git a/pkg/kubectl/cmd/auth/reconcile.go b/pkg/kubectl/cmd/auth/reconcile.go index da2b57ebbce..0c1a3969dc2 100644 --- a/pkg/kubectl/cmd/auth/reconcile.go +++ b/pkg/kubectl/cmd/auth/reconcile.go @@ -117,7 +117,7 @@ func (o *ReconcileOptions) Complete(cmd *cobra.Command, f cmdutil.Factory, args shortOutput := output == "name" o.Print = func(info *resource.Info) error { if len(output) > 0 && !shortOutput { - return cmdutil.PrintResourceInfoForCommand(cmd, info, f, o.Out) + return f.PrintResourceInfoForCommand(cmd, info, o.Out) } cmdutil.PrintSuccess(mapper, shortOutput, o.Out, info.Mapping.Resource, info.Name, dryRun, "reconciled") return nil diff --git a/pkg/kubectl/cmd/create.go b/pkg/kubectl/cmd/create.go index 5aecf9688e6..3b63cbd1787 100644 --- a/pkg/kubectl/cmd/create.go +++ b/pkg/kubectl/cmd/create.go @@ -229,7 +229,7 @@ func RunCreate(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opt shortOutput := output == "name" if len(output) > 0 && !shortOutput { - return cmdutil.PrintResourceInfoForCommand(cmd, info, f, out) + return f.PrintResourceInfoForCommand(cmd, info, out) } if !shortOutput { f.PrintObjectSpecificMessage(info.Object, out) diff --git a/pkg/kubectl/cmd/patch.go b/pkg/kubectl/cmd/patch.go index bb160564455..dffaff4d989 100644 --- a/pkg/kubectl/cmd/patch.go +++ b/pkg/kubectl/cmd/patch.go @@ -220,7 +220,7 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin } if len(options.OutputFormat) > 0 && options.OutputFormat != "name" { - return cmdutil.PrintResourceInfoForCommand(cmd, info, f, out) + return f.PrintResourceInfoForCommand(cmd, info, out) } mapper, _, err := f.UnstructuredObject() if err != nil { @@ -260,7 +260,7 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin if err := info.Refresh(targetObj, true); err != nil { return err } - return cmdutil.PrintResourceInfoForCommand(cmd, info, f, out) + return f.PrintResourceInfoForCommand(cmd, info, out) }) if err != nil { return err diff --git a/pkg/kubectl/cmd/testing/fake.go b/pkg/kubectl/cmd/testing/fake.go index ddda6aa15d9..f03e5d926fe 100644 --- a/pkg/kubectl/cmd/testing/fake.go +++ b/pkg/kubectl/cmd/testing/fake.go @@ -353,6 +353,20 @@ func (f *FakeFactory) PrinterForCommand(cmd *cobra.Command, isLocal bool, output return f.tf.Printer, f.tf.Err } +func (f *FakeFactory) PrintResourceInfoForCommand(cmd *cobra.Command, info *resource.Info, out io.Writer) error { + printer, err := f.PrinterForCommand(cmd, false, nil, printers.PrintOptions{}) + if err != nil { + return err + } + if !printer.IsGeneric() { + printer, err = f.PrinterForMapping(cmd, false, nil, nil, false) + if err != nil { + return err + } + } + return printer.PrintObj(info.Object, out) +} + func (f *FakeFactory) Printer(mapping *meta.RESTMapping, options printers.PrintOptions) (printers.ResourcePrinter, error) { return f.tf.Printer, f.tf.Err } @@ -682,6 +696,20 @@ func (f *fakeAPIFactory) PrinterForCommand(cmd *cobra.Command, isLocal bool, out return f.tf.Printer, f.tf.Err } +func (f *fakeAPIFactory) PrintResourceInfoForCommand(cmd *cobra.Command, info *resource.Info, out io.Writer) error { + printer, err := f.PrinterForCommand(cmd, false, nil, printers.PrintOptions{}) + if err != nil { + return err + } + if !printer.IsGeneric() { + printer, err = f.PrinterForMapping(cmd, false, nil, nil, false) + if err != nil { + return err + } + } + return printer.PrintObj(info.Object, out) +} + func (f *fakeAPIFactory) Describer(*meta.RESTMapping) (printers.Describer, error) { return f.tf.Describer, f.tf.Err } diff --git a/pkg/kubectl/cmd/util/factory.go b/pkg/kubectl/cmd/util/factory.go index 77972bdf687..a798031c650 100644 --- a/pkg/kubectl/cmd/util/factory.go +++ b/pkg/kubectl/cmd/util/factory.go @@ -243,6 +243,11 @@ type BuilderFactory interface { PrinterForMapping(cmd *cobra.Command, isLocal bool, outputOpts *printers.OutputOptions, mapping *meta.RESTMapping, withNamespace bool) (printers.ResourcePrinter, error) // PrintObject prints an api object given command line flags to modify the output format PrintObject(cmd *cobra.Command, isLocal bool, mapper meta.RESTMapper, obj runtime.Object, out io.Writer) error + // PrintResourceInfoForCommand receives a *cobra.Command and a *resource.Info and + // attempts to print an info object based on the specified output format. If the + // object passed is non-generic, it attempts to print the object using a HumanReadablePrinter. + // Requires that printer flags have been added to cmd (see AddPrinterFlags). + PrintResourceInfoForCommand(cmd *cobra.Command, info *resource.Info, out io.Writer) error // One stop shopping for a structured Builder NewBuilder() *resource.Builder // One stop shopping for a unstructured Builder diff --git a/pkg/kubectl/cmd/util/factory_builder.go b/pkg/kubectl/cmd/util/factory_builder.go index e62280a7358..bf514263d44 100644 --- a/pkg/kubectl/cmd/util/factory_builder.go +++ b/pkg/kubectl/cmd/util/factory_builder.go @@ -138,6 +138,20 @@ func (f *ring2Factory) PrintObject(cmd *cobra.Command, isLocal bool, mapper meta return printer.PrintObj(obj, out) } +func (f *ring2Factory) PrintResourceInfoForCommand(cmd *cobra.Command, info *resource.Info, out io.Writer) error { + printer, err := f.PrinterForCommand(cmd, false, nil, printers.PrintOptions{}) + if err != nil { + return err + } + if !printer.IsGeneric() { + printer, err = f.PrinterForMapping(cmd, false, nil, nil, false) + if err != nil { + return err + } + } + return printer.PrintObj(info.Object, out) +} + // NewBuilder returns a new resource builder for structured api objects. func (f *ring2Factory) NewBuilder() *resource.Builder { clientMapperFunc := resource.ClientMapperFunc(f.objectMappingFactory.ClientForMapping) diff --git a/pkg/kubectl/cmd/util/printing.go b/pkg/kubectl/cmd/util/printing.go index 7954fc8e245..a45fe1922f6 100644 --- a/pkg/kubectl/cmd/util/printing.go +++ b/pkg/kubectl/cmd/util/printing.go @@ -25,7 +25,6 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/kubernetes/pkg/kubectl" "k8s.io/kubernetes/pkg/kubectl/cmd/templates" - "k8s.io/kubernetes/pkg/kubectl/resource" "k8s.io/kubernetes/pkg/printers" printersinternal "k8s.io/kubernetes/pkg/printers/internalversion" @@ -138,24 +137,6 @@ func PrinterForCommand(cmd *cobra.Command, outputOpts *printers.OutputOptions, m return maybeWrapSortingPrinter(cmd, printer), nil } -// PrintResourceInfoForCommand receives a *cobra.Command and a *resource.Info and -// attempts to print an info object based on the specified output format. If the -// object passed is non-generic, it attempts to print the object using a HumanReadablePrinter. -// Requires that printer flags have been added to cmd (see AddPrinterFlags). -func PrintResourceInfoForCommand(cmd *cobra.Command, info *resource.Info, f Factory, out io.Writer) error { - printer, err := f.PrinterForCommand(cmd, false, nil, printers.PrintOptions{}) - if err != nil { - return err - } - if !printer.IsGeneric() { - printer, err = f.PrinterForMapping(cmd, false, nil, nil, false) - if err != nil { - return err - } - } - return printer.PrintObj(info.Object, out) -} - // extractOutputOptions parses printer specific commandline args and returns // printers.OutputsOptions object. func extractOutputOptions(cmd *cobra.Command) *printers.OutputOptions {