mirror of https://github.com/k3s-io/k3s
Merge pull request #54877 from juanvallejo/jvallejo/move-PrintResourceInfoRorCommand-cmdutil-factory
Automatic merge from submit-queue (batch tested with PRs 54602, 54877, 55243, 55509, 55128). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. move cmd/util/printing.go#PrintResourceInfoForCommand -> factory… **Release note**: ```release-note NONE ``` This patch is one in a series of patches that aims to move all printing functions to the `cmdutil.Factory` in order to make the factory the one-stop shop for accessing printers in the client. This PR is related to https://github.com/kubernetes/kubernetes/pull/50113 and aims to break the set of changes introduced in this commit in order to make them easier to review. @fabianofranz @mengqiy @shiywang @seans3pull/6/head
commit
e725361722
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -230,7 +230,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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -354,6 +354,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
|
||||
}
|
||||
|
@ -683,6 +697,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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -143,6 +143,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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue