Merge pull request #2236 from thaJeztah/plugin_sorting

docker info: list CLI plugins alphabetically
This commit is contained in:
Silvin Lubecki 2020-01-06 15:42:16 +01:00 committed by GitHub
commit f7f4a19328
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -6,11 +6,13 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config" "github.com/docker/cli/cli/config"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"vbom.ml/util/sortorder"
) )
// ReexecEnvvar is the name of an ennvar which is set to the command // ReexecEnvvar is the name of an ennvar which is set to the command
@ -141,6 +143,10 @@ func ListPlugins(dockerCli command.Cli, rootcmd *cobra.Command) ([]Plugin, error
} }
} }
sort.Slice(plugins, func(i, j int) bool {
return sortorder.NaturalLess(plugins[i].Name, plugins[j].Name)
})
return plugins, nil return plugins, nil
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/docker/cli/cli/config" "github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/internal/test" "github.com/docker/cli/internal/test"
"github.com/spf13/cobra"
"gotest.tools/assert" "gotest.tools/assert"
"gotest.tools/fs" "gotest.tools/fs"
) )
@ -81,6 +82,34 @@ func TestListPluginCandidates(t *testing.T) {
assert.DeepEqual(t, candidates, exp) assert.DeepEqual(t, candidates, exp)
} }
func TestListPluginsIsSorted(t *testing.T) {
dir := fs.NewDir(t, t.Name(),
fs.WithFile("docker-bbb", `
#!/bin/sh
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0777)),
fs.WithFile("docker-aaa", `
#!/bin/sh
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0777)),
)
defer dir.Remove()
cli := test.NewFakeCli(nil)
cli.SetConfigFile(&configfile.ConfigFile{CLIPluginsExtraDirs: []string{dir.Path()}})
plugins, err := ListPlugins(cli, &cobra.Command{})
assert.NilError(t, err)
// We're only interested in the plugins we created for testing this, and only
// if they appear in the expected order
var names []string
for _, p := range plugins {
if p.Name == "aaa" || p.Name == "bbb" {
names = append(names, p.Name)
}
}
assert.DeepEqual(t, names, []string{"aaa", "bbb"})
}
func TestErrPluginNotFound(t *testing.T) { func TestErrPluginNotFound(t *testing.T) {
var err error = errPluginNotFound("test") var err error = errPluginNotFound("test")
err.(errPluginNotFound).NotFound() err.(errPluginNotFound).NotFound()