From 26f06c5bcbaf1b66845fd74fe51218c424e9b285 Mon Sep 17 00:00:00 2001 From: allencloud Date: Fri, 7 Jul 2017 16:22:52 +0800 Subject: [PATCH] sort secrets and configs in cli Signed-off-by: Allen Sun --- cli/command/config/ls.go | 14 +++++++++++++ cli/command/config/ls_test.go | 17 ++++++++++------ .../config/testdata/config-list-sort.golden | 4 ++++ .../config-list-with-config-format.golden | 2 +- .../testdata/config-list-with-filter.golden | 2 +- .../testdata/config-list-with-format.golden | 2 +- .../config-list-with-quiet-option.golden | 2 +- .../config/testdata/config-list.golden | 3 --- cli/command/secret/ls.go | 15 ++++++++++++++ cli/command/secret/ls_test.go | 20 +++++++++++-------- .../secret/testdata/secret-list-sort.golden | 4 ++++ .../secret-list-with-config-format.golden | 2 +- .../testdata/secret-list-with-filter.golden | 2 +- .../testdata/secret-list-with-format.golden | 2 +- .../secret-list-with-quiet-option.golden | 2 +- .../secret/testdata/secret-list.golden | 3 --- 16 files changed, 68 insertions(+), 28 deletions(-) create mode 100644 cli/command/config/testdata/config-list-sort.golden delete mode 100644 cli/command/config/testdata/config-list.golden create mode 100644 cli/command/secret/testdata/secret-list-sort.golden delete mode 100644 cli/command/secret/testdata/secret-list.golden diff --git a/cli/command/config/ls.go b/cli/command/config/ls.go index f01e3b9154..9d1673b62c 100644 --- a/cli/command/config/ls.go +++ b/cli/command/config/ls.go @@ -1,15 +1,27 @@ package config import ( + "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/docker/docker/api/types" + "github.com/docker/docker/api/types/swarm" "github.com/spf13/cobra" "golang.org/x/net/context" + "vbom.ml/util/sortorder" ) +type byConfigName []swarm.Config + +func (r byConfigName) Len() int { return len(r) } +func (r byConfigName) Swap(i, j int) { r[i], r[j] = r[j], r[i] } +func (r byConfigName) Less(i, j int) bool { + return sortorder.NaturalLess(r[i].Spec.Name, r[j].Spec.Name) +} + type listOptions struct { quiet bool format string @@ -55,6 +67,8 @@ func runConfigList(dockerCli command.Cli, options listOptions) error { } } + sort.Sort(byConfigName(configs)) + configCtx := formatter.Context{ Output: dockerCli.Out(), Format: formatter.NewConfigFormat(format, options.quiet), diff --git a/cli/command/config/ls_test.go b/cli/command/config/ls_test.go index ac351cc725..f3946d2e5f 100644 --- a/cli/command/config/ls_test.go +++ b/cli/command/config/ls_test.go @@ -50,14 +50,20 @@ func TestConfigList(t *testing.T) { cli := test.NewFakeCli(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ - *Config(ConfigID("ID-foo"), - ConfigName("foo"), + *Config(ConfigID("ID-1-foo"), + ConfigName("1-foo"), ConfigVersion(swarm.Version{Index: 10}), ConfigCreatedAt(time.Now().Add(-2*time.Hour)), ConfigUpdatedAt(time.Now().Add(-1*time.Hour)), ), - *Config(ConfigID("ID-bar"), - ConfigName("bar"), + *Config(ConfigID("ID-10-foo"), + ConfigName("10-foo"), + ConfigVersion(swarm.Version{Index: 11}), + ConfigCreatedAt(time.Now().Add(-2*time.Hour)), + ConfigUpdatedAt(time.Now().Add(-1*time.Hour)), + ), + *Config(ConfigID("ID-2-foo"), + ConfigName("2-foo"), ConfigVersion(swarm.Version{Index: 11}), ConfigCreatedAt(time.Now().Add(-2*time.Hour)), ConfigUpdatedAt(time.Now().Add(-1*time.Hour)), @@ -66,9 +72,8 @@ func TestConfigList(t *testing.T) { }, }) cmd := newConfigListCommand(cli) - cmd.SetOutput(cli.OutBuffer()) assert.NoError(t, cmd.Execute()) - golden.Assert(t, cli.OutBuffer().String(), "config-list.golden") + golden.Assert(t, cli.OutBuffer().String(), "config-list-sort.golden") } func TestConfigListWithQuietOption(t *testing.T) { diff --git a/cli/command/config/testdata/config-list-sort.golden b/cli/command/config/testdata/config-list-sort.golden new file mode 100644 index 0000000000..141057c3f6 --- /dev/null +++ b/cli/command/config/testdata/config-list-sort.golden @@ -0,0 +1,4 @@ +ID NAME CREATED UPDATED +ID-1-foo 1-foo 2 hours ago About an hour ago +ID-2-foo 2-foo 2 hours ago About an hour ago +ID-10-foo 10-foo 2 hours ago About an hour ago diff --git a/cli/command/config/testdata/config-list-with-config-format.golden b/cli/command/config/testdata/config-list-with-config-format.golden index 11c39229b8..a64bb595c5 100644 --- a/cli/command/config/testdata/config-list-with-config-format.golden +++ b/cli/command/config/testdata/config-list-with-config-format.golden @@ -1,2 +1,2 @@ -foo bar label=label-bar +foo diff --git a/cli/command/config/testdata/config-list-with-filter.golden b/cli/command/config/testdata/config-list-with-filter.golden index 29983de8e9..6fdc13b8c1 100644 --- a/cli/command/config/testdata/config-list-with-filter.golden +++ b/cli/command/config/testdata/config-list-with-filter.golden @@ -1,3 +1,3 @@ ID NAME CREATED UPDATED -ID-foo foo 2 hours ago About an hour ago ID-bar bar 2 hours ago About an hour ago +ID-foo foo 2 hours ago About an hour ago diff --git a/cli/command/config/testdata/config-list-with-format.golden b/cli/command/config/testdata/config-list-with-format.golden index 11c39229b8..a64bb595c5 100644 --- a/cli/command/config/testdata/config-list-with-format.golden +++ b/cli/command/config/testdata/config-list-with-format.golden @@ -1,2 +1,2 @@ -foo bar label=label-bar +foo diff --git a/cli/command/config/testdata/config-list-with-quiet-option.golden b/cli/command/config/testdata/config-list-with-quiet-option.golden index 83fb6e8979..145fc38de7 100644 --- a/cli/command/config/testdata/config-list-with-quiet-option.golden +++ b/cli/command/config/testdata/config-list-with-quiet-option.golden @@ -1,2 +1,2 @@ -ID-foo ID-bar +ID-foo diff --git a/cli/command/config/testdata/config-list.golden b/cli/command/config/testdata/config-list.golden deleted file mode 100644 index 29983de8e9..0000000000 --- a/cli/command/config/testdata/config-list.golden +++ /dev/null @@ -1,3 +0,0 @@ -ID NAME CREATED UPDATED -ID-foo foo 2 hours ago About an hour ago -ID-bar bar 2 hours ago About an hour ago diff --git a/cli/command/secret/ls.go b/cli/command/secret/ls.go index 35bc3da0f2..7dc9a35f5f 100644 --- a/cli/command/secret/ls.go +++ b/cli/command/secret/ls.go @@ -1,15 +1,27 @@ package secret import ( + "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/docker/docker/api/types" + "github.com/docker/docker/api/types/swarm" "github.com/spf13/cobra" "golang.org/x/net/context" + "vbom.ml/util/sortorder" ) +type bySecretName []swarm.Secret + +func (r bySecretName) Len() int { return len(r) } +func (r bySecretName) Swap(i, j int) { r[i], r[j] = r[j], r[i] } +func (r bySecretName) Less(i, j int) bool { + return sortorder.NaturalLess(r[i].Spec.Name, r[j].Spec.Name) +} + type listOptions struct { quiet bool format string @@ -53,6 +65,9 @@ func runSecretList(dockerCli command.Cli, options listOptions) error { format = formatter.TableFormatKey } } + + sort.Sort(bySecretName(secrets)) + secretCtx := formatter.Context{ Output: dockerCli.Out(), Format: formatter.NewSecretFormat(format, options.quiet), diff --git a/cli/command/secret/ls_test.go b/cli/command/secret/ls_test.go index 83f6742af2..28b087a5ae 100644 --- a/cli/command/secret/ls_test.go +++ b/cli/command/secret/ls_test.go @@ -1,7 +1,6 @@ package secret import ( - "bytes" "io/ioutil" "testing" "time" @@ -48,18 +47,24 @@ func TestSecretListErrors(t *testing.T) { } func TestSecretList(t *testing.T) { - buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ - *Secret(SecretID("ID-foo"), - SecretName("foo"), + *Secret(SecretID("ID-1-foo"), + SecretName("1-foo"), SecretVersion(swarm.Version{Index: 10}), SecretCreatedAt(time.Now().Add(-2*time.Hour)), SecretUpdatedAt(time.Now().Add(-1*time.Hour)), ), - *Secret(SecretID("ID-bar"), - SecretName("bar"), + *Secret(SecretID("ID-10-foo"), + SecretName("10-foo"), + SecretVersion(swarm.Version{Index: 11}), + SecretCreatedAt(time.Now().Add(-2*time.Hour)), + SecretUpdatedAt(time.Now().Add(-1*time.Hour)), + SecretDriver("driver"), + ), + *Secret(SecretID("ID-2-foo"), + SecretName("2-foo"), SecretVersion(swarm.Version{Index: 11}), SecretCreatedAt(time.Now().Add(-2*time.Hour)), SecretUpdatedAt(time.Now().Add(-1*time.Hour)), @@ -69,9 +74,8 @@ func TestSecretList(t *testing.T) { }, }) cmd := newSecretListCommand(cli) - cmd.SetOutput(buf) assert.NoError(t, cmd.Execute()) - golden.Assert(t, cli.OutBuffer().String(), "secret-list.golden") + golden.Assert(t, cli.OutBuffer().String(), "secret-list-sort.golden") } func TestSecretListWithQuietOption(t *testing.T) { diff --git a/cli/command/secret/testdata/secret-list-sort.golden b/cli/command/secret/testdata/secret-list-sort.golden new file mode 100644 index 0000000000..805d26f371 --- /dev/null +++ b/cli/command/secret/testdata/secret-list-sort.golden @@ -0,0 +1,4 @@ +ID NAME DRIVER CREATED UPDATED +ID-1-foo 1-foo 2 hours ago About an hour ago +ID-2-foo 2-foo driver 2 hours ago About an hour ago +ID-10-foo 10-foo driver 2 hours ago About an hour ago diff --git a/cli/command/secret/testdata/secret-list-with-config-format.golden b/cli/command/secret/testdata/secret-list-with-config-format.golden index 11c39229b8..a64bb595c5 100644 --- a/cli/command/secret/testdata/secret-list-with-config-format.golden +++ b/cli/command/secret/testdata/secret-list-with-config-format.golden @@ -1,2 +1,2 @@ -foo bar label=label-bar +foo diff --git a/cli/command/secret/testdata/secret-list-with-filter.golden b/cli/command/secret/testdata/secret-list-with-filter.golden index 01ea6f3ae7..388d2874b1 100644 --- a/cli/command/secret/testdata/secret-list-with-filter.golden +++ b/cli/command/secret/testdata/secret-list-with-filter.golden @@ -1,3 +1,3 @@ ID NAME DRIVER CREATED UPDATED -ID-foo foo 2 hours ago About an hour ago ID-bar bar 2 hours ago About an hour ago +ID-foo foo 2 hours ago About an hour ago diff --git a/cli/command/secret/testdata/secret-list-with-format.golden b/cli/command/secret/testdata/secret-list-with-format.golden index 11c39229b8..a64bb595c5 100644 --- a/cli/command/secret/testdata/secret-list-with-format.golden +++ b/cli/command/secret/testdata/secret-list-with-format.golden @@ -1,2 +1,2 @@ -foo bar label=label-bar +foo diff --git a/cli/command/secret/testdata/secret-list-with-quiet-option.golden b/cli/command/secret/testdata/secret-list-with-quiet-option.golden index 83fb6e8979..145fc38de7 100644 --- a/cli/command/secret/testdata/secret-list-with-quiet-option.golden +++ b/cli/command/secret/testdata/secret-list-with-quiet-option.golden @@ -1,2 +1,2 @@ -ID-foo ID-bar +ID-foo diff --git a/cli/command/secret/testdata/secret-list.golden b/cli/command/secret/testdata/secret-list.golden deleted file mode 100644 index d8c2040438..0000000000 --- a/cli/command/secret/testdata/secret-list.golden +++ /dev/null @@ -1,3 +0,0 @@ -ID NAME DRIVER CREATED UPDATED -ID-foo foo 2 hours ago About an hour ago -ID-bar bar driver 2 hours ago About an hour ago