use sortorder lib for sorting the output of volume list command

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
This commit is contained in:
Arash Deshmeh 2018-08-08 11:57:39 -04:00
parent 04bb3c770f
commit 5cc1f9006a
3 changed files with 23 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/opts"
"github.com/spf13/cobra"
"vbom.ml/util/sortorder"
)
type listOptions struct {
@ -55,7 +56,7 @@ func runList(dockerCli command.Cli, options listOptions) error {
}
sort.Slice(volumes.Volumes, func(i, j int) bool {
return volumes.Volumes[i].Name < volumes.Volumes[j].Name
return sortorder.NaturalLess(volumes.Volumes[i].Name, volumes.Volumes[j].Name)
})
volumeCtx := formatter.Context{

View File

@ -109,3 +109,21 @@ func TestVolumeListWithFormat(t *testing.T) {
assert.NilError(t, cmd.Execute())
golden.Assert(t, cli.OutBuffer().String(), "volume-list-with-format.golden")
}
func TestVolumeListSortOrder(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
volumeListFunc: func(filter filters.Args) (volumetypes.VolumeListOKBody, error) {
return volumetypes.VolumeListOKBody{
Volumes: []*types.Volume{
Volume(VolumeName("volume-2-foo")),
Volume(VolumeName("volume-10-foo")),
Volume(VolumeName("volume-1-foo")),
},
}, nil
},
})
cmd := newListCommand(cli)
cmd.Flags().Set("format", "{{ .Name }}")
assert.NilError(t, cmd.Execute())
golden.Assert(t, cli.OutBuffer().String(), "volume-list-sort.golden")
}

View File

@ -0,0 +1,3 @@
volume-1-foo
volume-2-foo
volume-10-foo