sort secrets and configs in cli

Signed-off-by: Allen Sun <shlallen1990@gmail.com>
This commit is contained in:
allencloud 2017-07-07 16:22:52 +08:00 committed by Allen Sun
parent a41caadef0
commit 26f06c5bcb
16 changed files with 68 additions and 28 deletions

View File

@ -1,15 +1,27 @@
package config package config
import ( import (
"sort"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/net/context" "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 { type listOptions struct {
quiet bool quiet bool
format string format string
@ -55,6 +67,8 @@ func runConfigList(dockerCli command.Cli, options listOptions) error {
} }
} }
sort.Sort(byConfigName(configs))
configCtx := formatter.Context{ configCtx := formatter.Context{
Output: dockerCli.Out(), Output: dockerCli.Out(),
Format: formatter.NewConfigFormat(format, options.quiet), Format: formatter.NewConfigFormat(format, options.quiet),

View File

@ -50,14 +50,20 @@ func TestConfigList(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
*Config(ConfigID("ID-foo"), *Config(ConfigID("ID-1-foo"),
ConfigName("foo"), ConfigName("1-foo"),
ConfigVersion(swarm.Version{Index: 10}), ConfigVersion(swarm.Version{Index: 10}),
ConfigCreatedAt(time.Now().Add(-2*time.Hour)), ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
ConfigUpdatedAt(time.Now().Add(-1*time.Hour)), ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
), ),
*Config(ConfigID("ID-bar"), *Config(ConfigID("ID-10-foo"),
ConfigName("bar"), 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}), ConfigVersion(swarm.Version{Index: 11}),
ConfigCreatedAt(time.Now().Add(-2*time.Hour)), ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
ConfigUpdatedAt(time.Now().Add(-1*time.Hour)), ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
@ -66,9 +72,8 @@ func TestConfigList(t *testing.T) {
}, },
}) })
cmd := newConfigListCommand(cli) cmd := newConfigListCommand(cli)
cmd.SetOutput(cli.OutBuffer())
assert.NoError(t, cmd.Execute()) 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) { func TestConfigListWithQuietOption(t *testing.T) {

View File

@ -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

View File

@ -1,2 +1,2 @@
foo
bar label=label-bar bar label=label-bar
foo

View File

@ -1,3 +1,3 @@
ID NAME CREATED UPDATED 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-bar bar 2 hours ago About an hour ago
ID-foo foo 2 hours ago About an hour ago

View File

@ -1,2 +1,2 @@
foo
bar label=label-bar bar label=label-bar
foo

View File

@ -1,2 +1,2 @@
ID-foo
ID-bar ID-bar
ID-foo

View File

@ -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

View File

@ -1,15 +1,27 @@
package secret package secret
import ( import (
"sort"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/net/context" "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 { type listOptions struct {
quiet bool quiet bool
format string format string
@ -53,6 +65,9 @@ func runSecretList(dockerCli command.Cli, options listOptions) error {
format = formatter.TableFormatKey format = formatter.TableFormatKey
} }
} }
sort.Sort(bySecretName(secrets))
secretCtx := formatter.Context{ secretCtx := formatter.Context{
Output: dockerCli.Out(), Output: dockerCli.Out(),
Format: formatter.NewSecretFormat(format, options.quiet), Format: formatter.NewSecretFormat(format, options.quiet),

View File

@ -1,7 +1,6 @@
package secret package secret
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
"time" "time"
@ -48,18 +47,24 @@ func TestSecretListErrors(t *testing.T) {
} }
func TestSecretList(t *testing.T) { func TestSecretList(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), *Secret(SecretID("ID-1-foo"),
SecretName("foo"), SecretName("1-foo"),
SecretVersion(swarm.Version{Index: 10}), SecretVersion(swarm.Version{Index: 10}),
SecretCreatedAt(time.Now().Add(-2*time.Hour)), SecretCreatedAt(time.Now().Add(-2*time.Hour)),
SecretUpdatedAt(time.Now().Add(-1*time.Hour)), SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
), ),
*Secret(SecretID("ID-bar"), *Secret(SecretID("ID-10-foo"),
SecretName("bar"), 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}), SecretVersion(swarm.Version{Index: 11}),
SecretCreatedAt(time.Now().Add(-2*time.Hour)), SecretCreatedAt(time.Now().Add(-2*time.Hour)),
SecretUpdatedAt(time.Now().Add(-1*time.Hour)), SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
@ -69,9 +74,8 @@ func TestSecretList(t *testing.T) {
}, },
}) })
cmd := newSecretListCommand(cli) cmd := newSecretListCommand(cli)
cmd.SetOutput(buf)
assert.NoError(t, cmd.Execute()) 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) { func TestSecretListWithQuietOption(t *testing.T) {

View File

@ -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

View File

@ -1,2 +1,2 @@
foo
bar label=label-bar bar label=label-bar
foo

View File

@ -1,3 +1,3 @@
ID NAME DRIVER CREATED UPDATED 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-bar bar 2 hours ago About an hour ago
ID-foo foo 2 hours ago About an hour ago

View File

@ -1,2 +1,2 @@
foo
bar label=label-bar bar label=label-bar
foo

View File

@ -1,2 +1,2 @@
ID-foo
ID-bar ID-bar
ID-foo

View File

@ -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