From 7a5ed25c615036af55b1fc9920a60665c161700d Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Fri, 23 Sep 2016 14:43:25 -0400 Subject: [PATCH] return warning on empty list result in kubectl get The current default behavior of `kubectl get` is to return an empty output when there are no resources to display. This patch improves usability by returning a warning through stderr in the case of an empty list. --- pkg/kubectl/cmd/get.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index 514699b01e9..e356c062131 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -124,7 +124,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Comma // RunGet implements the generic Get command // TODO: convert all direct flag accessors to a struct and pass that instead of cmd -func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Command, args []string, options *GetOptions) error { +func RunGet(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args []string, options *GetOptions) error { if len(options.Raw) > 0 { restClient, err := f.RESTClient() if err != nil { @@ -310,6 +310,9 @@ func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Comm } allErrs = append(allErrs, err) } + if len(infos) == 0 { + outputEmptyListWarning(errOut) + } // the outermost object will be converted to the output-version, but inner // objects can use their mappings @@ -365,6 +368,9 @@ func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Comm if err != nil { allErrs = append(allErrs, err) } + if len(infos) == 0 { + outputEmptyListWarning(errOut) + } objs := make([]runtime.Object, len(infos)) for ix := range infos { @@ -478,3 +484,9 @@ func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Comm } return utilerrors.NewAggregate(allErrs) } + +// outputEmptyListWarning outputs a warning indicating that no items are available to display +func outputEmptyListWarning(out io.Writer) error { + _, err := fmt.Fprintf(out, "%s\n", "No resources found.") + return err +}