Merge pull request #32722 from juanvallejo/jvallejo_return-err-on-oc-get-empty-list

Automatic merge from submit-queue

return warning on empty list result in kubectl get

**Release note**:
```release-note
NONE
```

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.

##### Before
`$ kubectl get pods`
  - *empty output*

##### After
`$ kubectl get pods`
```
There are no resources to display.
```
pull/6/head
Kubernetes Submit Queue 2016-10-12 23:48:01 -07:00 committed by GitHub
commit b8b7f9ffc0
1 changed files with 13 additions and 1 deletions

View File

@ -125,7 +125,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 {
@ -322,6 +322,9 @@ func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Comm
}
errs = append(errs, err)
}
if len(infos) == 0 {
outputEmptyListWarning(errOut)
}
res := ""
if len(infos) > 0 {
@ -371,6 +374,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 {
@ -492,3 +498,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
}