Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use sortorder library for sorting network list output #1266

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli/command/network/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion cli/command/network/testdata/network-list-sort.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
network-1-foo
network-10-foo
network-2-foo
network-10-foo
3 changes: 2 additions & 1 deletion cli/command/trust/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
33 changes: 33 additions & 0 deletions cli/command/trust/common_test.go
Original file line number Diff line number Diff line change
@@ -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}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: releasesRole := data.DelegationRole{BaseRole: data.BaseRole{Name: trust.ReleasesRole}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. Seems the PR was merged before I modify this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries; I looked at the comment, and thought it to be not relevant enough to update before merging 🤗

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))
}
3 changes: 2 additions & 1 deletion cli/command/trust/inspect_pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
18 changes: 18 additions & 0 deletions cli/command/trust/inspect_pretty_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package trust

import (
"bytes"
"encoding/hex"
"io/ioutil"
"testing"
Expand Down Expand Up @@ -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"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; might want to swap A, B and C in this list, so that it's more clear that the list is sorted by signer, not keys

"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()))
}
3 changes: 2 additions & 1 deletion cli/command/volume/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
18 changes: 18 additions & 0 deletions cli/command/volume/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
3 changes: 3 additions & 0 deletions cli/command/volume/testdata/volume-list-sort.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
volume-1-foo
volume-2-foo
volume-10-foo