From 54ba28f4020b51978ef99b0132dc838408c34b48 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 31 Dec 2019 14:26:08 +0100 Subject: [PATCH] docker info: list CLI plugins alphabetically MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this change, plugins were listed in a random order: Client: Debug Mode: false Plugins: doodle: Docker Doodles all around! 🐳 🎃 (thaJeztah, v0.0.1) shell: Open a browser shell on the Docker Host. (thaJeztah, v0.0.1) app: Docker Application (Docker Inc., v0.8.0) buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker) With this change, plugins are listed alphabetically: Client: Debug Mode: false Plugins: app: Docker Application (Docker Inc., v0.8.0) buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker) doodle: Docker Doodles all around! 🐳 🎃 (thaJeztah, v0.0.1) shell: Open a browser shell on the Docker Host. (thaJeztah, v0.0.1) Signed-off-by: Sebastiaan van Stijn --- cli-plugins/manager/manager.go | 6 ++++++ cli-plugins/manager/manager_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/cli-plugins/manager/manager.go b/cli-plugins/manager/manager.go index e06286e9c4..7b7f7a863d 100644 --- a/cli-plugins/manager/manager.go +++ b/cli-plugins/manager/manager.go @@ -6,11 +6,13 @@ import ( "os" "os/exec" "path/filepath" + "sort" "strings" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/config" "github.com/spf13/cobra" + "vbom.ml/util/sortorder" ) // 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 } diff --git a/cli-plugins/manager/manager_test.go b/cli-plugins/manager/manager_test.go index d727fdac8a..b87953df44 100644 --- a/cli-plugins/manager/manager_test.go +++ b/cli-plugins/manager/manager_test.go @@ -7,6 +7,7 @@ import ( "github.com/docker/cli/cli/config" "github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/internal/test" + "github.com/spf13/cobra" "gotest.tools/assert" "gotest.tools/fs" ) @@ -81,6 +82,34 @@ func TestListPluginCandidates(t *testing.T) { 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) { var err error = errPluginNotFound("test") err.(errPluginNotFound).NotFound()