From 89f441cb5057e22caf7a25493bdeec324bb25027 Mon Sep 17 00:00:00 2001 From: Arash Deshmeh Date: Fri, 29 Jun 2018 12:15:43 -0400 Subject: [PATCH] The output of plugin list command is sorted by plugin name Signed-off-by: Arash Deshmeh --- cli/command/plugin/list.go | 11 +++++++++ cli/command/plugin/list_test.go | 24 +++++++++++++++++++ .../plugin/testdata/plugin-list-sort.golden | 3 +++ 3 files changed, 38 insertions(+) create mode 100644 cli/command/plugin/testdata/plugin-list-sort.golden diff --git a/cli/command/plugin/list.go b/cli/command/plugin/list.go index efbb0ffef418..25dc80a6d999 100644 --- a/cli/command/plugin/list.go +++ b/cli/command/plugin/list.go @@ -2,11 +2,15 @@ package plugin import ( "context" + "sort" + + "vbom.ml/util/sortorder" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/opts" + "github.com/docker/docker/api/types" "github.com/spf13/cobra" ) @@ -17,6 +21,12 @@ type listOptions struct { filter opts.FilterOpt } +type byName []*types.Plugin + +func (n byName) Len() int { return len(n) } +func (n byName) Swap(i, j int) { n[i], n[j] = n[j], n[i] } +func (n byName) Less(i, j int) bool { return sortorder.NaturalLess(n[i].Name, n[j].Name) } + func newListCommand(dockerCli command.Cli) *cobra.Command { options := listOptions{filter: opts.NewFilterOpt()} @@ -46,6 +56,7 @@ func runList(dockerCli command.Cli, options listOptions) error { return err } + sort.Sort(byName(plugins)) format := options.format if len(format) == 0 { if len(dockerCli.ConfigFile().PluginsFormat) > 0 && !options.quiet { diff --git a/cli/command/plugin/list_test.go b/cli/command/plugin/list_test.go index 03c23e51c75a..fbde655316d1 100644 --- a/cli/command/plugin/list_test.go +++ b/cli/command/plugin/list_test.go @@ -135,6 +135,30 @@ func TestList(t *testing.T) { golden: "plugin-list-with-format.golden", listFunc: singlePluginListFunc, }, + { + description: "list output is sorted based on plugin name", + args: []string{}, + flags: map[string]string{ + "format": "{{ .Name }}", + }, + golden: "plugin-list-sort.golden", + listFunc: func(filter filters.Args) (types.PluginsListResponse, error) { + return types.PluginsListResponse{ + { + ID: "id-1", + Name: "plugin-1-foo", + }, + { + ID: "id-2", + Name: "plugin-10-foo", + }, + { + ID: "id-3", + Name: "plugin-2-foo", + }, + }, nil + }, + }, } for _, tc := range testCases { diff --git a/cli/command/plugin/testdata/plugin-list-sort.golden b/cli/command/plugin/testdata/plugin-list-sort.golden new file mode 100644 index 000000000000..62c4a098ec2a --- /dev/null +++ b/cli/command/plugin/testdata/plugin-list-sort.golden @@ -0,0 +1,3 @@ +plugin-1-foo +plugin-2-foo +plugin-10-foo