From 26223f7017b9efbf9edfddd2ad5cbaddd3aca8ba Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 3 Jul 2024 15:43:47 +0200 Subject: [PATCH 1/5] cli/command/formatter: don't use unkeyed structs Signed-off-by: Sebastiaan van Stijn --- cli/command/formatter/container_test.go | 319 +++++++++++++++--------- 1 file changed, 203 insertions(+), 116 deletions(-) diff --git a/cli/command/formatter/container_test.go b/cli/command/formatter/container_test.go index 8f0d5e5c31..1185e07141 100644 --- a/cli/command/formatter/container_test.go +++ b/cli/command/formatter/container_test.go @@ -30,66 +30,155 @@ func TestContainerPsContext(t *testing.T) { expValue string call func() string }{ - {types.Container{ID: containerID}, true, stringid.TruncateID(containerID), ctx.ID}, - {types.Container{ID: containerID}, false, containerID, ctx.ID}, - {types.Container{Names: []string{"/foobar_baz"}}, true, "foobar_baz", ctx.Names}, - {types.Container{Image: "ubuntu"}, true, "ubuntu", ctx.Image}, - {types.Container{Image: "verylongimagename"}, true, "verylongimagename", ctx.Image}, - {types.Container{Image: "verylongimagename"}, false, "verylongimagename", ctx.Image}, { - types.Container{ + container: types.Container{ID: containerID}, + trunc: true, + expValue: stringid.TruncateID(containerID), + call: ctx.ID, + }, + { + container: types.Container{ID: containerID}, + expValue: containerID, + call: ctx.ID, + }, + { + container: types.Container{Names: []string{"/foobar_baz"}}, + trunc: true, + expValue: "foobar_baz", + call: ctx.Names, + }, + { + container: types.Container{Image: "ubuntu"}, + trunc: true, + expValue: "ubuntu", + call: ctx.Image, + }, + { + container: types.Container{Image: "verylongimagename"}, + trunc: true, + expValue: "verylongimagename", + call: ctx.Image, + }, + { + container: types.Container{Image: "verylongimagename"}, + expValue: "verylongimagename", + call: ctx.Image, + }, + { + container: types.Container{ Image: "a5a665ff33eced1e0803148700880edab4", ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5", }, - true, - "a5a665ff33ec", - ctx.Image, + trunc: true, + expValue: "a5a665ff33ec", + call: ctx.Image, }, { - types.Container{ + container: types.Container{ Image: "a5a665ff33eced1e0803148700880edab4", ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5", }, - false, - "a5a665ff33eced1e0803148700880edab4", - ctx.Image, + expValue: "a5a665ff33eced1e0803148700880edab4", + call: ctx.Image, }, - {types.Container{Image: ""}, true, "", ctx.Image}, - {types.Container{Command: "sh -c 'ls -la'"}, true, `"sh -c 'ls -la'"`, ctx.Command}, - {types.Container{Created: unix}, true, time.Unix(unix, 0).String(), ctx.CreatedAt}, - {types.Container{Ports: []types.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}}, true, "8080/tcp", ctx.Ports}, - {types.Container{Status: "RUNNING"}, true, "RUNNING", ctx.Status}, - {types.Container{SizeRw: 10}, true, "10B", ctx.Size}, - {types.Container{SizeRw: 10, SizeRootFs: 20}, true, "10B (virtual 20B)", ctx.Size}, - {types.Container{}, true, "", ctx.Labels}, - {types.Container{Labels: map[string]string{"cpu": "6", "storage": "ssd"}}, true, "cpu=6,storage=ssd", ctx.Labels}, - {types.Container{Created: unix}, true, "About a minute ago", ctx.RunningFor}, - {types.Container{ - Mounts: []types.MountPoint{ - { - Name: "this-is-a-long-volume-name-and-will-be-truncated-if-trunc-is-set", - Driver: "local", - Source: "/a/path", + { + container: types.Container{Image: ""}, + trunc: true, + expValue: "", + call: ctx.Image, + }, + { + container: types.Container{Command: "sh -c 'ls -la'"}, + trunc: true, + expValue: `"sh -c 'ls -la'"`, + call: ctx.Command, + }, + { + container: types.Container{Created: unix}, + trunc: true, + expValue: time.Unix(unix, 0).String(), + call: ctx.CreatedAt, + }, + { + container: types.Container{Ports: []types.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}}, + trunc: true, + expValue: "8080/tcp", + call: ctx.Ports, + }, + { + container: types.Container{Status: "RUNNING"}, + trunc: true, + expValue: "RUNNING", + call: ctx.Status, + }, + { + container: types.Container{SizeRw: 10}, + trunc: true, + expValue: "10B", + call: ctx.Size, + }, + { + container: types.Container{SizeRw: 10, SizeRootFs: 20}, + trunc: true, + expValue: "10B (virtual 20B)", + call: ctx.Size, + }, + { + container: types.Container{}, + trunc: true, + call: ctx.Labels, + }, + { + container: types.Container{Labels: map[string]string{"cpu": "6", "storage": "ssd"}}, + trunc: true, + expValue: "cpu=6,storage=ssd", + call: ctx.Labels, + }, + { + container: types.Container{Created: unix}, + trunc: true, + expValue: "About a minute ago", + call: ctx.RunningFor, + }, + { + container: types.Container{ + Mounts: []types.MountPoint{ + { + Name: "this-is-a-long-volume-name-and-will-be-truncated-if-trunc-is-set", + Driver: "local", + Source: "/a/path", + }, }, }, - }, true, "this-is-a-long…", ctx.Mounts}, - {types.Container{ - Mounts: []types.MountPoint{ - { - Driver: "local", - Source: "/a/path", + trunc: true, + expValue: "this-is-a-long…", + call: ctx.Mounts, + }, + { + container: types.Container{ + Mounts: []types.MountPoint{ + { + Driver: "local", + Source: "/a/path", + }, }, }, - }, false, "/a/path", ctx.Mounts}, - {types.Container{ - Mounts: []types.MountPoint{ - { - Name: "733908409c91817de8e92b0096373245f329f19a88e2c849f02460e9b3d1c203", - Driver: "local", - Source: "/a/path", + expValue: "/a/path", + call: ctx.Mounts, + }, + { + container: types.Container{ + Mounts: []types.MountPoint{ + { + Name: "733908409c91817de8e92b0096373245f329f19a88e2c849f02460e9b3d1c203", + Driver: "local", + Source: "/a/path", + }, }, }, - }, false, "733908409c91817de8e92b0096373245f329f19a88e2c849f02460e9b3d1c203", ctx.Mounts}, + expValue: "733908409c91817de8e92b0096373245f329f19a88e2c849f02460e9b3d1c203", + call: ctx.Mounts, + }, } for _, c := range cases { @@ -134,52 +223,52 @@ func TestContainerContextWrite(t *testing.T) { }{ // Errors { - Context{Format: "{{InvalidFunction}}"}, - `template parsing error: template: :1: function "InvalidFunction" not defined`, + context: Context{Format: "{{InvalidFunction}}"}, + expected: `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { - Context{Format: "{{nil}}"}, - `template parsing error: template: :1:2: executing "" at : nil is not a command`, + context: Context{Format: "{{nil}}"}, + expected: `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table Format { - Context{Format: NewContainerFormat("table", false, true)}, - `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE + context: Context{Format: NewContainerFormat("table", false, true)}, + expected: `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE containerID1 ubuntu "" 24 hours ago foobar_baz 0B containerID2 ubuntu "" 24 hours ago foobar_bar 0B `, }, { - Context{Format: NewContainerFormat("table", false, false)}, - `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES + context: Context{Format: NewContainerFormat("table", false, false)}, + expected: `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES containerID1 ubuntu "" 24 hours ago foobar_baz containerID2 ubuntu "" 24 hours ago foobar_bar `, }, { - Context{Format: NewContainerFormat("table {{.Image}}", false, false)}, - "IMAGE\nubuntu\nubuntu\n", + context: Context{Format: NewContainerFormat("table {{.Image}}", false, false)}, + expected: "IMAGE\nubuntu\nubuntu\n", }, { - Context{Format: NewContainerFormat("table {{.Image}}", false, true)}, - "IMAGE\nubuntu\nubuntu\n", + context: Context{Format: NewContainerFormat("table {{.Image}}", false, true)}, + expected: "IMAGE\nubuntu\nubuntu\n", }, { - Context{Format: NewContainerFormat("table {{.Image}}", true, false)}, - "containerID1\ncontainerID2\n", + context: Context{Format: NewContainerFormat("table {{.Image}}", true, false)}, + expected: "containerID1\ncontainerID2\n", }, { - Context{Format: NewContainerFormat("table", true, false)}, - "containerID1\ncontainerID2\n", + context: Context{Format: NewContainerFormat("table", true, false)}, + expected: "containerID1\ncontainerID2\n", }, { - Context{Format: NewContainerFormat("table {{.State}}", false, true)}, - "STATE\nrunning\nrunning\n", + context: Context{Format: NewContainerFormat("table {{.State}}", false, true)}, + expected: "STATE\nrunning\nrunning\n", }, // Raw Format { - Context{Format: NewContainerFormat("raw", false, false)}, - fmt.Sprintf(`container_id: containerID1 + context: Context{Format: NewContainerFormat("raw", false, false)}, + expected: fmt.Sprintf(`container_id: containerID1 image: ubuntu command: "" created_at: %s @@ -202,8 +291,8 @@ ports: `, expectedTime, expectedTime), }, { - Context{Format: NewContainerFormat("raw", false, true)}, - fmt.Sprintf(`container_id: containerID1 + context: Context{Format: NewContainerFormat("raw", false, true)}, + expected: fmt.Sprintf(`container_id: containerID1 image: ubuntu command: "" created_at: %s @@ -228,26 +317,26 @@ size: 0B `, expectedTime, expectedTime), }, { - Context{Format: NewContainerFormat("raw", true, false)}, - "container_id: containerID1\ncontainer_id: containerID2\n", + context: Context{Format: NewContainerFormat("raw", true, false)}, + expected: "container_id: containerID1\ncontainer_id: containerID2\n", }, // Custom Format { - Context{Format: "{{.Image}}"}, - "ubuntu\nubuntu\n", + context: Context{Format: "{{.Image}}"}, + expected: "ubuntu\nubuntu\n", }, { - Context{Format: NewContainerFormat("{{.Image}}", false, true)}, - "ubuntu\nubuntu\n", + context: Context{Format: NewContainerFormat("{{.Image}}", false, true)}, + expected: "ubuntu\nubuntu\n", }, // Special headers for customized table format { - Context{Format: NewContainerFormat(`table {{truncate .ID 5}}\t{{json .Image}} {{.RunningFor}}/{{title .Status}}/{{pad .Ports 2 2}}.{{upper .Names}} {{lower .Status}}`, false, true)}, - string(golden.Get(t, "container-context-write-special-headers.golden")), + context: Context{Format: NewContainerFormat(`table {{truncate .ID 5}}\t{{json .Image}} {{.RunningFor}}/{{title .Status}}/{{pad .Ports 2 2}}.{{upper .Names}} {{lower .Status}}`, false, true)}, + expected: string(golden.Get(t, "container-context-write-special-headers.golden")), }, { - Context{Format: NewContainerFormat(`table {{split .Image ":"}}`, false, false)}, - "IMAGE\n[ubuntu]\n[ubuntu]\n", + context: Context{Format: NewContainerFormat(`table {{split .Image ":"}}`, false, false)}, + expected: "IMAGE\n[ubuntu]\n[ubuntu]\n", }, } @@ -280,46 +369,44 @@ func TestContainerContextWriteWithNoContainers(t *testing.T) { expected string }{ { - Context{ + context: Context{ Format: "{{.Image}}", Output: out, }, - "", }, { - Context{ + context: Context{ Format: "table {{.Image}}", Output: out, }, - "IMAGE\n", + expected: "IMAGE\n", }, { - Context{ + context: Context{ Format: NewContainerFormat("{{.Image}}", false, true), Output: out, }, - "", }, { - Context{ + context: Context{ Format: NewContainerFormat("table {{.Image}}", false, true), Output: out, }, - "IMAGE\n", + expected: "IMAGE\n", }, { - Context{ + context: Context{ Format: "table {{.Image}}\t{{.Size}}", Output: out, }, - "IMAGE SIZE\n", + expected: "IMAGE SIZE\n", }, { - Context{ + context: Context{ Format: NewContainerFormat("table {{.Image}}\t{{.Size}}", false, true), Output: out, }, - "IMAGE SIZE\n", + expected: "IMAGE SIZE\n", }, } @@ -444,45 +531,45 @@ type ports struct { func TestDisplayablePorts(t *testing.T) { cases := []ports{ { - []types.Port{ + ports: []types.Port{ { PrivatePort: 9988, Type: "tcp", }, }, - "9988/tcp", + expected: "9988/tcp", }, { - []types.Port{ + ports: []types.Port{ { PrivatePort: 9988, Type: "udp", }, }, - "9988/udp", + expected: "9988/udp", }, { - []types.Port{ + ports: []types.Port{ { IP: "0.0.0.0", PrivatePort: 9988, Type: "tcp", }, }, - "0.0.0.0:0->9988/tcp", + expected: "0.0.0.0:0->9988/tcp", }, { - []types.Port{ + ports: []types.Port{ { PrivatePort: 9988, PublicPort: 8899, Type: "tcp", }, }, - "9988/tcp", + expected: "9988/tcp", }, { - []types.Port{ + ports: []types.Port{ { IP: "4.3.2.1", PrivatePort: 9988, @@ -490,10 +577,10 @@ func TestDisplayablePorts(t *testing.T) { Type: "tcp", }, }, - "4.3.2.1:8899->9988/tcp", + expected: "4.3.2.1:8899->9988/tcp", }, { - []types.Port{ + ports: []types.Port{ { IP: "4.3.2.1", PrivatePort: 9988, @@ -501,10 +588,10 @@ func TestDisplayablePorts(t *testing.T) { Type: "tcp", }, }, - "4.3.2.1:9988->9988/tcp", + expected: "4.3.2.1:9988->9988/tcp", }, { - []types.Port{ + ports: []types.Port{ { PrivatePort: 9988, Type: "udp", @@ -513,10 +600,10 @@ func TestDisplayablePorts(t *testing.T) { Type: "udp", }, }, - "9988/udp, 9988/udp", + expected: "9988/udp, 9988/udp", }, { - []types.Port{ + ports: []types.Port{ { IP: "1.2.3.4", PublicPort: 9998, @@ -529,10 +616,10 @@ func TestDisplayablePorts(t *testing.T) { Type: "udp", }, }, - "1.2.3.4:9998-9999->9998-9999/udp", + expected: "1.2.3.4:9998-9999->9998-9999/udp", }, { - []types.Port{ + ports: []types.Port{ { IP: "1.2.3.4", PublicPort: 8887, @@ -545,10 +632,10 @@ func TestDisplayablePorts(t *testing.T) { Type: "udp", }, }, - "1.2.3.4:8887->9998/udp, 1.2.3.4:8888->9999/udp", + expected: "1.2.3.4:8887->9998/udp, 1.2.3.4:8888->9999/udp", }, { - []types.Port{ + ports: []types.Port{ { PrivatePort: 9998, Type: "udp", @@ -557,10 +644,10 @@ func TestDisplayablePorts(t *testing.T) { Type: "udp", }, }, - "9998-9999/udp", + expected: "9998-9999/udp", }, { - []types.Port{ + ports: []types.Port{ { IP: "1.2.3.4", PrivatePort: 6677, @@ -572,10 +659,10 @@ func TestDisplayablePorts(t *testing.T) { Type: "udp", }, }, - "9988/udp, 1.2.3.4:7766->6677/tcp", + expected: "9988/udp, 1.2.3.4:7766->6677/tcp", }, { - []types.Port{ + ports: []types.Port{ { IP: "1.2.3.4", PrivatePort: 9988, @@ -593,10 +680,10 @@ func TestDisplayablePorts(t *testing.T) { Type: "tcp", }, }, - "4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/tcp, 1.2.3.4:8899->9988/udp", + expected: "4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/tcp, 1.2.3.4:8899->9988/udp", }, { - []types.Port{ + ports: []types.Port{ { PrivatePort: 9988, PublicPort: 8899, @@ -613,10 +700,10 @@ func TestDisplayablePorts(t *testing.T) { Type: "tcp", }, }, - "9988/udp, 4.3.2.1:3322->2233/tcp, 1.2.3.4:7766->6677/tcp", + expected: "9988/udp, 4.3.2.1:3322->2233/tcp, 1.2.3.4:7766->6677/tcp", }, { - []types.Port{ + ports: []types.Port{ { PrivatePort: 80, Type: "tcp", @@ -674,7 +761,7 @@ func TestDisplayablePorts(t *testing.T) { Type: "sctp", }, }, - "80/tcp, 80/udp, 1024/tcp, 1024/udp, 12345/sctp, 1.1.1.1:1024->80/tcp, 1.1.1.1:1024->80/udp, 2.1.1.1:1024->80/tcp, 2.1.1.1:1024->80/udp, 1.1.1.1:80->1024/tcp, 1.1.1.1:80->1024/udp, 2.1.1.1:80->1024/tcp, 2.1.1.1:80->1024/udp", + expected: "80/tcp, 80/udp, 1024/tcp, 1024/udp, 12345/sctp, 1.1.1.1:1024->80/tcp, 1.1.1.1:1024->80/udp, 2.1.1.1:1024->80/tcp, 2.1.1.1:1024->80/udp, 1.1.1.1:80->1024/tcp, 1.1.1.1:80->1024/udp, 2.1.1.1:80->1024/tcp, 2.1.1.1:80->1024/udp", }, } From 3a77fdd91f4e92bcdfafa61414fe2a6f1ba820bd Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 3 Jul 2024 15:59:48 +0200 Subject: [PATCH 2/5] cli/command/trust: unconvert Signed-off-by: Sebastiaan van Stijn --- cli/command/trust/inspect_pretty_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/command/trust/inspect_pretty_test.go b/cli/command/trust/inspect_pretty_test.go index f564a5f7c6..13519f1390 100644 --- a/cli/command/trust/inspect_pretty_test.go +++ b/cli/command/trust/inspect_pretty_test.go @@ -187,7 +187,7 @@ func TestNotaryRoleToSigner(t *testing.T) { } assert.Check(t, is.Equal(role.String(), notaryRoleToSigner(role))) } - assert.Check(t, is.Equal("notarole", notaryRoleToSigner(data.RoleName("notarole")))) + assert.Check(t, is.Equal("notarole", notaryRoleToSigner("notarole"))) } // check if a role name is "released": either targets/releases or targets TUF roles @@ -196,9 +196,9 @@ func TestIsReleasedTarget(t *testing.T) { for _, role := range data.BaseRoles { assert.Check(t, is.Equal(role == data.CanonicalTargetsRole, isReleasedTarget(role))) } - assert.Check(t, !isReleasedTarget(data.RoleName("targets/not-releases"))) - assert.Check(t, !isReleasedTarget(data.RoleName("random"))) - assert.Check(t, !isReleasedTarget(data.RoleName("targets/releases/subrole"))) + assert.Check(t, !isReleasedTarget("targets/not-releases")) + assert.Check(t, !isReleasedTarget("random")) + assert.Check(t, !isReleasedTarget("targets/releases/subrole")) } // creates a mock delegation with a given name and no keys From 42ba29395b8026df6ec9a803a77b09cbd5172222 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 3 Jul 2024 16:14:40 +0200 Subject: [PATCH 3/5] rename vars to prevent colliding with imports Signed-off-by: Sebastiaan van Stijn --- cli/command/container/pause.go | 8 ++++---- cli/command/container/start.go | 4 ++-- cli/command/container/unpause.go | 8 ++++---- cli/command/container/update.go | 8 ++++---- cli/command/container/wait.go | 6 +++--- cli/command/formatter/container.go | 4 ++-- cli/command/formatter/disk_usage.go | 19 +++++++++---------- cli/command/image/client_test.go | 4 ++-- cli/command/image/inspect_test.go | 16 ++++++++-------- cli/command/network/disconnect.go | 10 +++++----- internal/test/builders/container.go | 14 +++++++------- 11 files changed, 50 insertions(+), 51 deletions(-) diff --git a/cli/command/container/pause.go b/cli/command/container/pause.go index 8323827e8d..495c1c9f9a 100644 --- a/cli/command/container/pause.go +++ b/cli/command/container/pause.go @@ -32,8 +32,8 @@ func NewPauseCommand(dockerCli command.Cli) *cobra.Command { Annotations: map[string]string{ "aliases": "docker container pause, docker pause", }, - ValidArgsFunction: completion.ContainerNames(dockerCli, false, func(container types.Container) bool { - return container.State != "paused" + ValidArgsFunction: completion.ContainerNames(dockerCli, false, func(ctr types.Container) bool { + return ctr.State != "paused" }), } } @@ -41,12 +41,12 @@ func NewPauseCommand(dockerCli command.Cli) *cobra.Command { func runPause(ctx context.Context, dockerCli command.Cli, opts *pauseOptions) error { var errs []string errChan := parallelOperation(ctx, opts.containers, dockerCli.Client().ContainerPause) - for _, container := range opts.containers { + for _, ctr := range opts.containers { if err := <-errChan; err != nil { errs = append(errs, err.Error()) continue } - fmt.Fprintln(dockerCli.Out(), container) + _, _ = fmt.Fprintln(dockerCli.Out(), ctr) } if len(errs) > 0 { return errors.New(strings.Join(errs, "\n")) diff --git a/cli/command/container/start.go b/cli/command/container/start.go index ba04e34435..7cd6d0e7ce 100644 --- a/cli/command/container/start.go +++ b/cli/command/container/start.go @@ -43,8 +43,8 @@ func NewStartCommand(dockerCli command.Cli) *cobra.Command { Annotations: map[string]string{ "aliases": "docker container start, docker start", }, - ValidArgsFunction: completion.ContainerNames(dockerCli, true, func(container types.Container) bool { - return container.State == "exited" || container.State == "created" + ValidArgsFunction: completion.ContainerNames(dockerCli, true, func(ctr types.Container) bool { + return ctr.State == "exited" || ctr.State == "created" }), } diff --git a/cli/command/container/unpause.go b/cli/command/container/unpause.go index 3d82dcff80..3190d75790 100644 --- a/cli/command/container/unpause.go +++ b/cli/command/container/unpause.go @@ -32,8 +32,8 @@ func NewUnpauseCommand(dockerCli command.Cli) *cobra.Command { Annotations: map[string]string{ "aliases": "docker container unpause, docker unpause", }, - ValidArgsFunction: completion.ContainerNames(dockerCli, false, func(container types.Container) bool { - return container.State == "paused" + ValidArgsFunction: completion.ContainerNames(dockerCli, false, func(ctr types.Container) bool { + return ctr.State == "paused" }), } return cmd @@ -42,12 +42,12 @@ func NewUnpauseCommand(dockerCli command.Cli) *cobra.Command { func runUnpause(ctx context.Context, dockerCli command.Cli, opts *unpauseOptions) error { var errs []string errChan := parallelOperation(ctx, opts.containers, dockerCli.Client().ContainerUnpause) - for _, container := range opts.containers { + for _, ctr := range opts.containers { if err := <-errChan; err != nil { errs = append(errs, err.Error()) continue } - fmt.Fprintln(dockerCli.Out(), container) + _, _ = fmt.Fprintln(dockerCli.Out(), ctr) } if len(errs) > 0 { return errors.New(strings.Join(errs, "\n")) diff --git a/cli/command/container/update.go b/cli/command/container/update.go index 7ea668830b..997d61b354 100644 --- a/cli/command/container/update.go +++ b/cli/command/container/update.go @@ -130,17 +130,17 @@ func runUpdate(ctx context.Context, dockerCli command.Cli, options *updateOption warns []string errs []string ) - for _, container := range options.containers { - r, err := dockerCli.Client().ContainerUpdate(ctx, container, updateConfig) + for _, ctr := range options.containers { + r, err := dockerCli.Client().ContainerUpdate(ctx, ctr, updateConfig) if err != nil { errs = append(errs, err.Error()) } else { - fmt.Fprintln(dockerCli.Out(), container) + _, _ = fmt.Fprintln(dockerCli.Out(), ctr) } warns = append(warns, r.Warnings...) } if len(warns) > 0 { - fmt.Fprintln(dockerCli.Out(), strings.Join(warns, "\n")) + _, _ = fmt.Fprintln(dockerCli.Out(), strings.Join(warns, "\n")) } if len(errs) > 0 { return errors.New(strings.Join(errs, "\n")) diff --git a/cli/command/container/wait.go b/cli/command/container/wait.go index 91cca5b6aa..8eb7d82f80 100644 --- a/cli/command/container/wait.go +++ b/cli/command/container/wait.go @@ -39,12 +39,12 @@ func NewWaitCommand(dockerCli command.Cli) *cobra.Command { func runWait(ctx context.Context, dockerCli command.Cli, opts *waitOptions) error { var errs []string - for _, container := range opts.containers { - resultC, errC := dockerCli.Client().ContainerWait(ctx, container, "") + for _, ctr := range opts.containers { + resultC, errC := dockerCli.Client().ContainerWait(ctx, ctr, "") select { case result := <-resultC: - fmt.Fprintf(dockerCli.Out(), "%d\n", result.StatusCode) + _, _ = fmt.Fprintf(dockerCli.Out(), "%d\n", result.StatusCode) case err := <-errC: errs = append(errs, err.Error()) } diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index aeaa03a6d3..17afec1b4b 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -68,8 +68,8 @@ ports: {{- pad .Ports 1 0}} // ContainerWrite renders the context for a list of containers func ContainerWrite(ctx Context, containers []types.Container) error { render := func(format func(subContext SubContext) error) error { - for _, container := range containers { - err := format(&ContainerContext{trunc: ctx.Trunc, c: container}) + for _, ctr := range containers { + err := format(&ContainerContext{trunc: ctx.Trunc, c: ctr}) if err != nil { return err } diff --git a/cli/command/formatter/disk_usage.go b/cli/command/formatter/disk_usage.go index d91df2fc55..cd1d75bef3 100644 --- a/cli/command/formatter/disk_usage.go +++ b/cli/command/formatter/disk_usage.go @@ -336,8 +336,8 @@ func (c *diskUsageContainersContext) isActive(container types.Container) bool { func (c *diskUsageContainersContext) Active() string { used := 0 - for _, container := range c.containers { - if c.isActive(*container) { + for _, ctr := range c.containers { + if c.isActive(*ctr) { used++ } } @@ -348,22 +348,21 @@ func (c *diskUsageContainersContext) Active() string { func (c *diskUsageContainersContext) Size() string { var size int64 - for _, container := range c.containers { - size += container.SizeRw + for _, ctr := range c.containers { + size += ctr.SizeRw } return units.HumanSize(float64(size)) } func (c *diskUsageContainersContext) Reclaimable() string { - var reclaimable int64 - var totalSize int64 + var reclaimable, totalSize int64 - for _, container := range c.containers { - if !c.isActive(*container) { - reclaimable += container.SizeRw + for _, ctr := range c.containers { + if !c.isActive(*ctr) { + reclaimable += ctr.SizeRw } - totalSize += container.SizeRw + totalSize += ctr.SizeRw } if totalSize > 0 { diff --git a/cli/command/image/client_test.go b/cli/command/image/client_test.go index 35bf3ab772..eb3a4cebef 100644 --- a/cli/command/image/client_test.go +++ b/cli/command/image/client_test.go @@ -24,9 +24,9 @@ type fakeClient struct { imagesPruneFunc func(pruneFilter filters.Args) (image.PruneReport, error) imageLoadFunc func(input io.Reader, quiet bool) (image.LoadResponse, error) imageListFunc func(options image.ListOptions) ([]image.Summary, error) - imageInspectFunc func(image string) (types.ImageInspect, []byte, error) + imageInspectFunc func(img string) (types.ImageInspect, []byte, error) imageImportFunc func(source image.ImportSource, ref string, options image.ImportOptions) (io.ReadCloser, error) - imageHistoryFunc func(image string) ([]image.HistoryResponseItem, error) + imageHistoryFunc func(img string) ([]image.HistoryResponseItem, error) imageBuildFunc func(context.Context, io.Reader, types.ImageBuildOptions) (types.ImageBuildResponse, error) } diff --git a/cli/command/image/inspect_test.go b/cli/command/image/inspect_test.go index fd0067e022..1a6425f8c9 100644 --- a/cli/command/image/inspect_test.go +++ b/cli/command/image/inspect_test.go @@ -38,15 +38,15 @@ func TestNewInspectCommandSuccess(t *testing.T) { name string args []string imageCount int - imageInspectFunc func(image string) (types.ImageInspect, []byte, error) + imageInspectFunc func(img string) (types.ImageInspect, []byte, error) }{ { name: "simple", args: []string{"image"}, imageCount: 1, - imageInspectFunc: func(image string) (types.ImageInspect, []byte, error) { + imageInspectFunc: func(img string) (types.ImageInspect, []byte, error) { imageInspectInvocationCount++ - assert.Check(t, is.Equal("image", image)) + assert.Check(t, is.Equal("image", img)) return types.ImageInspect{}, nil, nil }, }, @@ -54,21 +54,21 @@ func TestNewInspectCommandSuccess(t *testing.T) { name: "format", imageCount: 1, args: []string{"--format='{{.ID}}'", "image"}, - imageInspectFunc: func(image string) (types.ImageInspect, []byte, error) { + imageInspectFunc: func(img string) (types.ImageInspect, []byte, error) { imageInspectInvocationCount++ - return types.ImageInspect{ID: image}, nil, nil + return types.ImageInspect{ID: img}, nil, nil }, }, { name: "simple-many", args: []string{"image1", "image2"}, imageCount: 2, - imageInspectFunc: func(image string) (types.ImageInspect, []byte, error) { + imageInspectFunc: func(img string) (types.ImageInspect, []byte, error) { imageInspectInvocationCount++ if imageInspectInvocationCount == 1 { - assert.Check(t, is.Equal("image1", image)) + assert.Check(t, is.Equal("image1", img)) } else { - assert.Check(t, is.Equal("image2", image)) + assert.Check(t, is.Equal("image2", img)) } return types.ImageInspect{}, nil, nil }, diff --git a/cli/command/network/disconnect.go b/cli/command/network/disconnect.go index abed538f66..318a2d2a2b 100644 --- a/cli/command/network/disconnect.go +++ b/cli/command/network/disconnect.go @@ -50,18 +50,18 @@ func runDisconnect(ctx context.Context, dockerCli command.Cli, opts disconnectOp } func isConnected(network string) func(types.Container) bool { - return func(container types.Container) bool { - if container.NetworkSettings == nil { + return func(ctr types.Container) bool { + if ctr.NetworkSettings == nil { return false } - _, ok := container.NetworkSettings.Networks[network] + _, ok := ctr.NetworkSettings.Networks[network] return ok } } func not(fn func(types.Container) bool) func(types.Container) bool { - return func(container types.Container) bool { - ok := fn(container) + return func(ctr types.Container) bool { + ok := fn(ctr) return !ok } } diff --git a/internal/test/builders/container.go b/internal/test/builders/container.go index 0f31998a06..359b14e76a 100644 --- a/internal/test/builders/container.go +++ b/internal/test/builders/container.go @@ -8,10 +8,10 @@ import ( // Container creates a container with default values. // Any number of container function builder can be passed to augment it. -func Container(name string, builders ...func(container *types.Container)) *types.Container { +func Container(name string, builders ...func(c *types.Container)) *types.Container { // now := time.Now() // onehourago := now.Add(-120 * time.Minute) - container := &types.Container{ + ctr := &types.Container{ ID: "container_id", Names: []string{"/" + name}, Command: "top", @@ -21,10 +21,10 @@ func Container(name string, builders ...func(container *types.Container)) *types } for _, builder := range builders { - builder(container) + builder(ctr) } - return container + return ctr } // WithLabel adds a label to the container @@ -45,14 +45,14 @@ func WithName(name string) func(*types.Container) { } // WithPort adds a port mapping to the container -func WithPort(privateport, publicport uint16, builders ...func(*types.Port)) func(*types.Container) { +func WithPort(privatePort, publicPort uint16, builders ...func(*types.Port)) func(*types.Container) { return func(c *types.Container) { if c.Ports == nil { c.Ports = []types.Port{} } port := &types.Port{ - PrivatePort: privateport, - PublicPort: publicport, + PrivatePort: privatePort, + PublicPort: publicPort, } for _, builder := range builders { builder(port) From 229616e173049d35646256091f1c062d8e3e561d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 3 Jul 2024 16:36:56 +0200 Subject: [PATCH 4/5] cli/command/image: fakeClient.ImagesPrune: fix unhandled err-return Signed-off-by: Sebastiaan van Stijn --- cli/command/image/client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/command/image/client_test.go b/cli/command/image/client_test.go index eb3a4cebef..8789a34789 100644 --- a/cli/command/image/client_test.go +++ b/cli/command/image/client_test.go @@ -69,7 +69,7 @@ func (cli *fakeClient) Info(_ context.Context) (system.Info, error) { func (cli *fakeClient) ImagePull(_ context.Context, ref string, options image.PullOptions) (io.ReadCloser, error) { if cli.imagePullFunc != nil { - cli.imagePullFunc(ref, options) + return cli.imagePullFunc(ref, options) } return io.NopCloser(strings.NewReader("")), nil } From b711372cab3efae6bc59bc735be6d38689ec33e0 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 3 Jul 2024 16:29:16 +0200 Subject: [PATCH 5/5] cli/command/container: TestNewAttachCommandErrors: use struct-literals Signed-off-by: Sebastiaan van Stijn --- cli/command/container/attach_test.go | 43 ++++++++++++++++------------ 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/cli/command/container/attach_test.go b/cli/command/container/attach_test.go index 7c16aec778..11f4ced45f 100644 --- a/cli/command/container/attach_test.go +++ b/cli/command/container/attach_test.go @@ -32,10 +32,13 @@ func TestNewAttachCommandErrors(t *testing.T) { args: []string{"5cb5bb5e4a3b"}, expectedError: "You cannot attach to a stopped container", containerInspectFunc: func(containerID string) (types.ContainerJSON, error) { - c := types.ContainerJSON{} - c.ContainerJSONBase = &types.ContainerJSONBase{} - c.ContainerJSONBase.State = &types.ContainerState{Running: false} - return c, nil + return types.ContainerJSON{ + ContainerJSONBase: &types.ContainerJSONBase{ + State: &types.ContainerState{ + Running: false, + }, + }, + }, nil }, }, { @@ -43,13 +46,14 @@ func TestNewAttachCommandErrors(t *testing.T) { args: []string{"5cb5bb5e4a3b"}, expectedError: "You cannot attach to a paused container", containerInspectFunc: func(containerID string) (types.ContainerJSON, error) { - c := types.ContainerJSON{} - c.ContainerJSONBase = &types.ContainerJSONBase{} - c.ContainerJSONBase.State = &types.ContainerState{ - Running: true, - Paused: true, - } - return c, nil + return types.ContainerJSON{ + ContainerJSONBase: &types.ContainerJSONBase{ + State: &types.ContainerState{ + Running: true, + Paused: true, + }, + }, + }, nil }, }, { @@ -57,14 +61,15 @@ func TestNewAttachCommandErrors(t *testing.T) { args: []string{"5cb5bb5e4a3b"}, expectedError: "You cannot attach to a restarting container", containerInspectFunc: func(containerID string) (types.ContainerJSON, error) { - c := types.ContainerJSON{} - c.ContainerJSONBase = &types.ContainerJSONBase{} - c.ContainerJSONBase.State = &types.ContainerState{ - Running: true, - Paused: false, - Restarting: true, - } - return c, nil + return types.ContainerJSON{ + ContainerJSONBase: &types.ContainerJSONBase{ + State: &types.ContainerState{ + Running: true, + Paused: false, + Restarting: true, + }, + }, + }, nil }, }, }