Merge pull request #1266 from adshmh/use-natural-sort-order-for-network-list

use sortorder library for sorting network list output
This commit is contained in:
Sebastiaan van Stijn 2018-08-09 12:05:55 +02:00 committed by GitHub
commit 6ef11c516d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 81 additions and 5 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/docker/cli/opts"
"github.com/docker/docker/api/types"
"github.com/spf13/cobra"
"vbom.ml/util/sortorder"
)
type listOptions struct {
@ -59,7 +60,7 @@ func runList(dockerCli command.Cli, options listOptions) error {
}
sort.Slice(networkResources, func(i, j int) bool {
return networkResources[i].Name < networkResources[j].Name
return sortorder.NaturalLess(networkResources[i].Name, networkResources[j].Name)
})
networksCtx := formatter.Context{

View File

@ -1,3 +1,3 @@
network-1-foo
network-10-foo
network-2-foo
network-10-foo

View File

@ -14,6 +14,7 @@ import (
"github.com/theupdateframework/notary"
"github.com/theupdateframework/notary/client"
"github.com/theupdateframework/notary/tuf/data"
"vbom.ml/util/sortorder"
)
// trustTagKey represents a unique signed tag and hex-encoded hash pair
@ -149,7 +150,7 @@ func matchReleasedSignatures(allTargets []client.TargetSignedStruct) []trustTagR
signatureRows = append(signatureRows, trustTagRow{targetKey, signers})
}
sort.Slice(signatureRows, func(i, j int) bool {
return signatureRows[i].SignedTag < signatureRows[j].SignedTag
return sortorder.NaturalLess(signatureRows[i].SignedTag, signatureRows[j].SignedTag)
})
return signatureRows
}

View File

@ -0,0 +1,33 @@
package trust
import (
"testing"
"github.com/docker/cli/cli/trust"
"github.com/theupdateframework/notary/client"
"github.com/theupdateframework/notary/tuf/data"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
func TestMatchReleasedSignaturesSortOrder(t *testing.T) {
var releasesRole = data.DelegationRole{BaseRole: data.BaseRole{Name: trust.ReleasesRole}}
targets := []client.TargetSignedStruct{
{Target: client.Target{Name: "target10-foo"}, Role: releasesRole},
{Target: client.Target{Name: "target1-foo"}, Role: releasesRole},
{Target: client.Target{Name: "target2-foo"}, Role: releasesRole},
}
rows := matchReleasedSignatures(targets)
var targetNames []string
for _, r := range rows {
targetNames = append(targetNames, r.SignedTag)
}
expected := []string{
"target1-foo",
"target2-foo",
"target10-foo",
}
assert.Check(t, is.DeepEqual(expected, targetNames))
}

View File

@ -8,6 +8,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/theupdateframework/notary/client"
"vbom.ml/util/sortorder"
)
func prettyPrintTrustInfo(cli command.Cli, remote string) error {
@ -86,7 +87,7 @@ func printSignerInfo(out io.Writer, roleToKeyIDs map[string][]string) error {
})
}
sort.Slice(formattedSignerInfo, func(i, j int) bool {
return formattedSignerInfo[i].Name < formattedSignerInfo[j].Name
return sortorder.NaturalLess(formattedSignerInfo[i].Name, formattedSignerInfo[j].Name)
})
return formatter.SignerInfoWrite(signerInfoCtx, formattedSignerInfo)
}

View File

@ -1,6 +1,7 @@
package trust
import (
"bytes"
"encoding/hex"
"io/ioutil"
"testing"
@ -440,3 +441,20 @@ func TestFormatAdminRole(t *testing.T) {
targetsRoleWithSigs := client.RoleWithSignatures{Role: targetsRole, Signatures: nil}
assert.Check(t, is.Equal("Repository Key:\tabc, key11, key99\n", formatAdminRole(targetsRoleWithSigs)))
}
func TestPrintSignerInfoSortOrder(t *testing.T) {
roleToKeyIDs := map[string][]string{
"signer2-foo": {"B"},
"signer10-foo": {"C"},
"signer1-foo": {"A"},
}
expected := `SIGNER KEYS
signer1-foo A
signer2-foo B
signer10-foo C
`
buf := new(bytes.Buffer)
assert.NilError(t, printSignerInfo(buf, roleToKeyIDs))
assert.Check(t, is.Equal(expected, buf.String()))
}

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