From 70138273098f1b8d411b2798384d14482190b5ee Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Thu, 4 Apr 2019 17:39:32 -0400 Subject: [PATCH] Stop special-casing tables in kubectl get to print 'No resources found' --- pkg/kubectl/cmd/get/get.go | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/pkg/kubectl/cmd/get/get.go b/pkg/kubectl/cmd/get/get.go index 70ac74f3747..0d890bb4fcd 100644 --- a/pkg/kubectl/cmd/get/get.go +++ b/pkg/kubectl/cmd/get/get.go @@ -487,13 +487,6 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e objs := make([]runtime.Object, len(infos)) for ix := range infos { - // TODO: remove this and just pass the table objects to the printer opaquely once `info.Object.(*metav1beta1.Table)` checking is removed below - if o.ServerPrint { - table, err := decodeIntoTable(infos[ix].Object) - if err == nil { - infos[ix].Object = table - } - } objs[ix] = infos[ix].Object } @@ -513,8 +506,11 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e var printer printers.ResourcePrinter var lastMapping *meta.RESTMapping - nonEmptyObjCount := 0 - w := utilprinters.GetNewTabWriter(o.Out) + + // track if we write any output + trackingWriter := &trackingWriterWrapper{Delegate: o.Out} + + w := utilprinters.GetNewTabWriter(trackingWriter) for ix := range objs { var mapping *meta.RESTMapping var info *resource.Info @@ -526,16 +522,6 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e mapping = info.Mapping } - // if dealing with a table that has no rows, skip remaining steps - // and avoid printing an unnecessary newline - if table, isTable := info.Object.(*metav1beta1.Table); isTable { - if len(table.Rows) == 0 { - continue - } - } - - nonEmptyObjCount++ - printWithNamespace := o.AllNamespaces if mapping != nil && mapping.Scope.Name() == meta.RESTScopeNameRoot { @@ -582,12 +568,23 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e } } w.Flush() - if nonEmptyObjCount == 0 && !o.IgnoreNotFound && len(allErrs) == 0 { + if trackingWriter.Written == 0 && !o.IgnoreNotFound && len(allErrs) == 0 { + // if we wrote no output, and had no errors, and are not ignoring NotFound, be sure we output something fmt.Fprintln(o.ErrOut, "No resources found.") } return utilerrors.NewAggregate(allErrs) } +type trackingWriterWrapper struct { + Delegate io.Writer + Written int +} + +func (t *trackingWriterWrapper) Write(p []byte) (n int, err error) { + t.Written += len(p) + return t.Delegate.Write(p) +} + // raw makes a simple HTTP request to the provided path on the server using the default // credentials. func (o *GetOptions) raw(f cmdutil.Factory) error {