From 26151d910af70f9197d6c9428647584370dab3fb 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 | 6 +++++ cli/command/plugin/list_test.go | 24 +++++++++++++++++++ .../plugin/testdata/plugin-list-sort.golden | 3 +++ 3 files changed, 33 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..35612d185927 100644 --- a/cli/command/plugin/list.go +++ b/cli/command/plugin/list.go @@ -2,12 +2,14 @@ package plugin import ( "context" + "sort" "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/spf13/cobra" + "vbom.ml/util/sortorder" ) type listOptions struct { @@ -46,6 +48,10 @@ func runList(dockerCli command.Cli, options listOptions) error { return err } + sort.Slice(plugins, func(i, j int) bool { + return sortorder.NaturalLess(plugins[i].Name, plugins[j].Name) + }) + 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