mirror of
https://github.com/docker/cli.git
synced 2026-01-26 15:41:42 +03:00
Sort swarm stacks and nodes using natural sorting
This commit changes the order stacks and nodes are displayed. For example, running "docker stack ls" is expected to display the following list: NAME SERVICES service-1 1 service-2 1 service-10 1 However, currently this is what is printed: NAME SERVICES service-1 1 service-10 1 service-2 1 To fix this, "docker stack ls" and "docker node ls" are using natural sorting to make it more human readable. Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/net/context"
|
||||
"vbom.ml/util/sortorder"
|
||||
)
|
||||
|
||||
type listOptions struct {
|
||||
@@ -60,7 +61,7 @@ type byName []*formatter.Stack
|
||||
|
||||
func (n byName) Len() int { return len(n) }
|
||||
func (n byName) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
|
||||
func (n byName) Less(i, j int) bool { return n[i].Name < n[j].Name }
|
||||
func (n byName) Less(i, j int) bool { return sortorder.NaturalLess(n[i].Name, n[j].Name) }
|
||||
|
||||
func getStacks(ctx context.Context, apiclient client.APIClient) ([]*formatter.Stack, error) {
|
||||
services, err := apiclient.ServiceList(
|
||||
|
||||
@@ -98,10 +98,13 @@ func TestListWithoutFormat(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestListOrder(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{
|
||||
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
||||
return []swarm.Service{
|
||||
usecases := []struct {
|
||||
golden string
|
||||
swarmServices []swarm.Service
|
||||
}{
|
||||
{
|
||||
golden: "stack-list-sort.golden",
|
||||
swarmServices: []swarm.Service{
|
||||
*Service(
|
||||
ServiceLabels(map[string]string{
|
||||
"com.docker.stack.namespace": "service-name-foo",
|
||||
@@ -112,11 +115,40 @@ func TestListOrder(t *testing.T) {
|
||||
"com.docker.stack.namespace": "service-name-bar",
|
||||
}),
|
||||
),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
}, buf))
|
||||
assert.NoError(t, cmd.Execute())
|
||||
actual := buf.String()
|
||||
expected := golden.Get(t, []byte(actual), "stack-list-sort.golden")
|
||||
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
||||
{
|
||||
golden: "stack-list-sort-natural.golden",
|
||||
swarmServices: []swarm.Service{
|
||||
*Service(
|
||||
ServiceLabels(map[string]string{
|
||||
"com.docker.stack.namespace": "service-name-1-foo",
|
||||
}),
|
||||
),
|
||||
*Service(
|
||||
ServiceLabels(map[string]string{
|
||||
"com.docker.stack.namespace": "service-name-10-foo",
|
||||
}),
|
||||
),
|
||||
*Service(
|
||||
ServiceLabels(map[string]string{
|
||||
"com.docker.stack.namespace": "service-name-2-foo",
|
||||
}),
|
||||
),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, uc := range usecases {
|
||||
buf := new(bytes.Buffer)
|
||||
cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{
|
||||
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
||||
return uc.swarmServices, nil
|
||||
},
|
||||
}, buf))
|
||||
assert.NoError(t, cmd.Execute())
|
||||
actual := buf.String()
|
||||
expected := golden.Get(t, []byte(actual), uc.golden)
|
||||
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
||||
}
|
||||
}
|
||||
|
||||
4
cli/command/stack/testdata/stack-list-sort-natural.golden
vendored
Normal file
4
cli/command/stack/testdata/stack-list-sort-natural.golden
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
NAME SERVICES
|
||||
service-name-1-foo 1
|
||||
service-name-2-foo 1
|
||||
service-name-10-foo 1
|
||||
Reference in New Issue
Block a user