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.
This commit is contained in:
juanvallejo 2016-09-23 14:43:25 -04:00
parent b95fa62fe1
commit 7a5ed25c61

View File

@ -124,7 +124,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Comma
// RunGet implements the generic Get command // RunGet implements the generic Get command
// TODO: convert all direct flag accessors to a struct and pass that instead of cmd // 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 { if len(options.Raw) > 0 {
restClient, err := f.RESTClient() restClient, err := f.RESTClient()
if err != nil { 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) allErrs = append(allErrs, err)
} }
if len(infos) == 0 {
outputEmptyListWarning(errOut)
}
// the outermost object will be converted to the output-version, but inner // the outermost object will be converted to the output-version, but inner
// objects can use their mappings // 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 { if err != nil {
allErrs = append(allErrs, err) allErrs = append(allErrs, err)
} }
if len(infos) == 0 {
outputEmptyListWarning(errOut)
}
objs := make([]runtime.Object, len(infos)) objs := make([]runtime.Object, len(infos))
for ix := range 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) 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
}