Merge pull request #64001 from juanvallejo/jvallejo/sort-on-generic-output

Automatic merge from submit-queue (batch tested with PRs 63770, 63776, 64001, 64028, 63984). 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>.

sort on non-tabular output

**Release note**:

```release-note
NONE
```

Enables sorting objects when printing as json, yaml, etc.

cc @soltysh
This commit is contained in:
Kubernetes Submit Queue 2018-05-19 02:11:33 -07:00 committed by GitHub
commit 8a40e5f77f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -241,6 +241,8 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
if err != nil {
return nil, err
}
printer = maybeWrapSortingPrinter(printer, isSorting)
return printer.PrintObj, nil
}
@ -738,3 +740,13 @@ func shouldGetNewPrinterForMapping(printer printers.ResourcePrinter, lastMapping
func cmdSpecifiesOutputFmt(cmd *cobra.Command) bool {
return cmdutil.GetFlagString(cmd, "output") != ""
}
func maybeWrapSortingPrinter(printer printers.ResourcePrinter, sortBy string) printers.ResourcePrinter {
if len(sortBy) != 0 {
return &kubectl.SortingPrinter{
Delegate: printer,
SortField: fmt.Sprintf("%s", sortBy),
}
}
return printer
}