Skip to content

Commit

Permalink
The output of plugin list command is sorted by plugin name
Browse files Browse the repository at this point in the history
Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
  • Loading branch information
adshmh committed Jun 30, 2018
1 parent 34ba66b commit 89f441c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cli/command/plugin/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()}

Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions cli/command/plugin/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions cli/command/plugin/testdata/plugin-list-sort.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugin-1-foo
plugin-2-foo
plugin-10-foo

0 comments on commit 89f441c

Please sign in to comment.