Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move cmd/util/printing.go#PrintResourceInfoForCommand -> factory… #54877

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/kubectl/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/apply_set_last_applied.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/auth/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubectl/cmd/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions pkg/kubectl/cmd/testing/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/kubectl/cmd/util/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions pkg/kubectl/cmd/util/factory_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 0 additions & 19 deletions pkg/kubectl/cmd/util/printing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {
Expand Down