Compare commits

...

8 Commits

Author SHA1 Message Date
Sebastiaan van Stijn 1ea0e0839a
Merge be3646b87c into abb8e9b78a 2024-10-21 19:03:12 +01:00
Sebastiaan van Stijn abb8e9b78a
Merge pull request #5546 from thaJeztah/hints_coverage
cli/hints: add tests
2024-10-21 18:08:28 +02:00
Laura Brehm 7029147458
Merge pull request #5557 from thaJeztah/minor_linting_issues 2024-10-21 17:00:40 +01:00
Sebastiaan van Stijn be3646b87c
image ls --tree: order images alphabetically
The tree output currently uses the same sort order as the existing
non-tree output, and orders the images by "created" time in descending
order;

    docker image ls
    REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
    <none>           <none>    8262a6d8c38a   7 minutes ago    13.6MB
    docker-cli-dev   latest    f5f0547476ee   12 minutes ago   762MB
    nginx            alpine    2140dad235c1   2 weeks ago      76.7MB
    alpine           latest    beefdbd8a1da   6 weeks ago      24.2MB

However, the `--tree` view does not have a `CREATED` column, which makes
the output order seem "random". With the tree view being more verbose,
it may also be harder to find back images in the list when they're not sorted
in an easy to discover way.

This patch changes the sort order:

- alphabetically (natural sort) for tagged images
- untagged images are sorted last, as they're likely less relevant
  to the user, and should not be "polluting" th top of the list.
- if multiple untagged images exist, they are sorted by created
  date (descending) to get a stable order.

Before this patch:

    $ docker image ls --tree

    IMAGE                   ID             DISK USAGE   CONTENT SIZE   USED
    <untagged>              20ad73eca911       13.6MB         4.09MB    ✔
    └─ linux/arm64          1ab6fc68586e       13.6MB         4.09MB    ✔

    <untagged>              b3e87f642f5c       13.6MB         4.09MB
    └─ linux/arm64          1ab6fc68586e       13.6MB         4.09MB

    docker-cli-dev:latest   f5f0547476ee        762MB          179MB    ✔
    └─ linux/arm64          18ca7881145d        762MB          179MB    ✔

    nginx:alpine            2140dad235c1       76.7MB         21.5MB
    ├─ linux/arm64/v8       d1f949a77b81       76.7MB         21.5MB
    ├─ linux/amd64          ae136e431e76           0B             0B
    ├─ linux/arm/v6         ae1ee4b63c14           0B             0B
    ├─ linux/arm/v7         20ad73eca911           0B             0B
    ├─ linux/386            1e69bfb21757           0B             0B
    ├─ linux/ppc64le        7fef8bcf8b6c           0B             0B
    └─ linux/s390x          8c310bf29cfa           0B             0B

    alpine:latest           beefdbd8a1da       24.2MB         7.46MB
    ├─ linux/riscv64        80cde017a105       10.6MB         3.37MB
    ├─ linux/arm64/v8       9cee2b382fe2       13.6MB         4.09MB
    ├─ linux/amd64          33735bd63cf8           0B             0B
    ├─ linux/arm/v6         50f635c8b04d           0B             0B
    ├─ linux/arm/v7         f2f82d424957           0B             0B
    ├─ linux/386            b3e87f642f5c           0B             0B
    ├─ linux/ppc64le        c7a6800e3dc5           0B             0B
    └─ linux/s390x          2b5b26e09ca2           0B             0B

With this patch:

    $ docker image ls --tree

    IMAGE                   ID             DISK USAGE   CONTENT SIZE   USED
    alpine:latest           beefdbd8a1da       24.2MB         7.46MB
    ├─ linux/riscv64        80cde017a105       10.6MB         3.37MB
    ├─ linux/arm64/v8       9cee2b382fe2       13.6MB         4.09MB
    ├─ linux/amd64          33735bd63cf8           0B             0B
    ├─ linux/arm/v6         50f635c8b04d           0B             0B
    ├─ linux/arm/v7         f2f82d424957           0B             0B
    ├─ linux/386            b3e87f642f5c           0B             0B
    ├─ linux/ppc64le        c7a6800e3dc5           0B             0B
    └─ linux/s390x          2b5b26e09ca2           0B             0B

    docker-cli-dev:latest   f5f0547476ee        762MB          179MB    ✔
    └─ linux/arm64          18ca7881145d        762MB          179MB    ✔

    nginx:alpine            2140dad235c1       76.7MB         21.5MB
    ├─ linux/arm64/v8       d1f949a77b81       76.7MB         21.5MB
    ├─ linux/amd64          ae136e431e76           0B             0B
    ├─ linux/arm/v6         ae1ee4b63c14           0B             0B
    ├─ linux/arm/v7         20ad73eca911           0B             0B
    ├─ linux/386            1e69bfb21757           0B             0B
    ├─ linux/ppc64le        7fef8bcf8b6c           0B             0B
    └─ linux/s390x          8c310bf29cfa           0B             0B

    <untagged>              20ad73eca911       13.6MB         4.09MB    ✔
    └─ linux/arm64          1ab6fc68586e       13.6MB         4.09MB    ✔

    <untagged>              b3e87f642f5c       13.6MB         4.09MB
    └─ linux/arm64          1ab6fc68586e       13.6MB         4.09MB

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-20 23:54:57 +02:00
Sebastiaan van Stijn 6b9083776f
cli/command: AddPlatformFlag: suppress unhandled error
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-20 17:51:36 +02:00
Sebastiaan van Stijn fb61156b05
cli/command/registry: fix minor linting issues
- fix camelCase naming of verifyLoginOptions
- suppress unhandled errors that can be ignored

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-20 17:51:12 +02:00
Sebastiaan van Stijn 7187c78554
image ls: show each tag for an image as a separate entry
A single image can be tagged under multiple names. While they are the
same image under the hood (same digest), we always presented these as
separate images in the list.

This patch applies the same behavior for the tree view; we can consider
having some "compact" presentation in future where we collapse these iamges
(perhaps introducing a "names" column?)

Before this patch:

    $ docker pull --quiet alpine:3.20
    docker.io/library/alpine:3.20
    $ docker pull --quiet alpine:latest
    docker.io/library/alpine:latest

    $ docker image ls --tree

    IMAGE                   ID             DISK USAGE   CONTENT SIZE   USED
    alpine:3.20
    alpine:latest           beefdbd8a1da       13.6MB         4.09MB
    ├─ linux/arm64/v8       9cee2b382fe2       13.6MB         4.09MB
    ├─ linux/amd64          33735bd63cf8           0B             0B
    ├─ linux/arm/v6         50f635c8b04d           0B             0B
    ├─ linux/arm/v7         f2f82d424957           0B             0B
    ├─ linux/386            b3e87f642f5c           0B             0B
    ├─ linux/ppc64le        c7a6800e3dc5           0B             0B
    ├─ linux/riscv64        80cde017a105           0B             0B
    └─ linux/s390x          2b5b26e09ca2           0B             0B

With this patch applied:

    $ docker image ls --tree

    IMAGE                   ID             DISK USAGE   CONTENT SIZE   USED
    alpine:3.20             beefdbd8a1da       13.6MB         4.09MB
    ├─ linux/arm64/v8       9cee2b382fe2       13.6MB         4.09MB
    ├─ linux/amd64          33735bd63cf8           0B             0B
    ├─ linux/arm/v6         50f635c8b04d           0B             0B
    ├─ linux/arm/v7         f2f82d424957           0B             0B
    ├─ linux/386            b3e87f642f5c           0B             0B
    ├─ linux/ppc64le        c7a6800e3dc5           0B             0B
    ├─ linux/riscv64        80cde017a105           0B             0B
    └─ linux/s390x          2b5b26e09ca2           0B             0B

    alpine:latest           beefdbd8a1da       13.6MB         4.09MB
    ├─ linux/arm64/v8       9cee2b382fe2       13.6MB         4.09MB
    ├─ linux/amd64          33735bd63cf8           0B             0B
    ├─ linux/arm/v6         50f635c8b04d           0B             0B
    ├─ linux/arm/v7         f2f82d424957           0B             0B
    ├─ linux/386            b3e87f642f5c           0B             0B
    ├─ linux/ppc64le        c7a6800e3dc5           0B             0B
    ├─ linux/riscv64        80cde017a105           0B             0B
    └─ linux/s390x          2b5b26e09ca2           0B             0B

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-19 19:48:45 +02:00
Sebastiaan van Stijn 87acf77aef
cli/hints: add tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-19 00:48:16 +02:00
5 changed files with 98 additions and 13 deletions

View File

@ -14,6 +14,7 @@ import (
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/go-units"
"github.com/fvbommel/sortorder"
"github.com/morikuni/aec"
)
@ -82,16 +83,46 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
details.ContentSize = units.HumanSizeWithPrecision(float64(totalContent), 3)
view.images = append(view.images, topImage{
Names: img.RepoTags,
Details: details,
Children: children,
created: img.Created,
})
if len(img.RepoTags) == 0 {
// Untagged image
view.images = append(view.images, topImage{
Names: img.RepoTags,
Details: details,
Children: children,
created: img.Created,
})
} else {
// Present images tagged under multiple names as separate images.
for _, n := range img.RepoTags {
view.images = append(view.images, topImage{
Names: []string{n}, // Consider changing Names to be a single name for purpose of this presentation.
Details: details,
Children: children,
created: img.Created,
})
}
}
}
// Sort images alphabetically using natural-sort, with untagged images last.
sort.Slice(view.images, func(i, j int) bool {
return view.images[i].created > view.images[j].created
iUntagged, jUntagged := len(view.images[i].Names) == 0, len(view.images[j].Names) == 0
if iUntagged || jUntagged {
switch {
case iUntagged && jUntagged:
// Both untagged images; sort by created date (desc)
return view.images[i].created > view.images[j].created
case iUntagged:
// Sort untagged images last
return false
case jUntagged:
// Sort untagged images last
return true
}
}
// Sort alphabetically, ascending
return sortorder.NaturalLess(view.images[i].Names[0], view.images[j].Names[0])
})
return printImageTree(dockerCLI, view)

View File

@ -58,9 +58,9 @@ func NewLoginCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}
func verifyloginOptions(dockerCli command.Cli, opts *loginOptions) error {
func verifyLoginOptions(dockerCli command.Cli, opts *loginOptions) error {
if opts.password != "" {
fmt.Fprintln(dockerCli.Err(), "WARNING! Using --password via the CLI is insecure. Use --password-stdin.")
_, _ = fmt.Fprintln(dockerCli.Err(), "WARNING! Using --password via the CLI is insecure. Use --password-stdin.")
if opts.passwordStdin {
return errors.New("--password and --password-stdin are mutually exclusive")
}
@ -83,7 +83,7 @@ func verifyloginOptions(dockerCli command.Cli, opts *loginOptions) error {
}
func runLogin(ctx context.Context, dockerCli command.Cli, opts loginOptions) error {
if err := verifyloginOptions(dockerCli, &opts); err != nil {
if err := verifyLoginOptions(dockerCli, &opts); err != nil {
return err
}
var (
@ -174,7 +174,7 @@ func loginUser(ctx context.Context, dockerCli command.Cli, opts loginOptions, de
if !errors.Is(err, manager.ErrDeviceLoginStartFail) {
return response, err
}
fmt.Fprint(dockerCli.Err(), "Failed to start web-based login - falling back to command line login...\n\n")
_, _ = fmt.Fprint(dockerCli.Err(), "Failed to start web-based login - falling back to command line login...\n\n")
}
return loginWithUsernameAndPassword(ctx, dockerCli, opts, defaultUsername, serverAddress)

View File

@ -199,7 +199,7 @@ func PruneFilters(dockerCli Cli, pruneFilters filters.Args) filters.Args {
// AddPlatformFlag adds `platform` to a set of flags for API version 1.32 and later.
func AddPlatformFlag(flags *pflag.FlagSet, target *string) {
flags.StringVar(target, "platform", os.Getenv("DOCKER_DEFAULT_PLATFORM"), "Set platform if server is multi-platform capable")
flags.SetAnnotation("platform", "version", []string{"1.32"})
_ = flags.SetAnnotation("platform", "version", []string{"1.32"})
}
// ValidateOutputPath validates the output paths of the `export` and `save` commands.

View File

@ -5,7 +5,9 @@ import (
"strconv"
)
// Enabled returns whether cli hints are enabled or not
// Enabled returns whether cli hints are enabled or not. Hints are enabled by
// default, but can be disabled through the "DOCKER_CLI_HINTS" environment
// variable.
func Enabled() bool {
if v := os.Getenv("DOCKER_CLI_HINTS"); v != "" {
enabled, err := strconv.ParseBool(v)

52
cli/hints/hints_test.go Normal file
View File

@ -0,0 +1,52 @@
package hints
import (
"testing"
"gotest.tools/v3/assert"
)
func TestEnabled(t *testing.T) {
tests := []struct {
doc string
env string
expected bool
}{
{
doc: "default",
expected: true,
},
{
doc: "DOCKER_CLI_HINTS=1",
env: "1",
expected: true,
},
{
doc: "DOCKER_CLI_HINTS=true",
env: "true",
expected: true,
},
{
doc: "DOCKER_CLI_HINTS=0",
env: "0",
expected: false,
},
{
doc: "DOCKER_CLI_HINTS=false",
env: "false",
expected: false,
},
{
doc: "DOCKER_CLI_HINTS=not-a-bool",
env: "not-a-bool",
expected: true,
},
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
t.Setenv("DOCKER_CLI_HINTS", tc.env)
assert.Equal(t, Enabled(), tc.expected)
})
}
}