Skip to content

Commit

Permalink
sort secrets and configs in cli
Browse files Browse the repository at this point in the history
Signed-off-by: allencloud <allen.sun@daocloud.io>
Signed-off-by: Allen Sun <shlallen1990@gmail.com>
  • Loading branch information
allencloud committed Sep 25, 2017
1 parent a41caad commit 996824e
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 24 deletions.
14 changes: 14 additions & 0 deletions cli/command/config/ls.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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),
Expand Down
17 changes: 11 additions & 6 deletions cli/command/config/ls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions cli/command/config/testdata/config-list-sort.golden
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
foo
bar label=label-bar
foo
3 changes: 2 additions & 1 deletion cli/command/config/testdata/config-list-with-filter.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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

2 changes: 1 addition & 1 deletion cli/command/config/testdata/config-list-with-format.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
foo
bar label=label-bar
foo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ID-foo
ID-bar
ID-foo
3 changes: 0 additions & 3 deletions cli/command/config/testdata/config-list.golden

This file was deleted.

15 changes: 15 additions & 0 deletions cli/command/secret/ls.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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),
Expand Down
20 changes: 12 additions & 8 deletions cli/command/secret/ls_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package secret

import (
"bytes"
"io/ioutil"
"testing"
"time"
Expand Down Expand Up @@ -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)),
Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions cli/command/secret/testdata/secret-list-sort.golden
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions cli/command/secret/testdata/secret-list.golden

This file was deleted.

0 comments on commit 996824e

Please sign in to comment.