Skip to content

Commit 0f875ba

Browse files
Merge pull request #6351 from thaJeztah/avoid_shadowing
cli/command: rename vars for consistency and prevent shadowing
2 parents 1df8feb + 9fd71c8 commit 0f875ba

30 files changed

+180
-186
lines changed

cli/command/cli_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,16 @@ func TestInitializeFromClient(t *testing.T) {
187187

188188
for _, tc := range testcases {
189189
t.Run(tc.doc, func(t *testing.T) {
190-
apiclient := &fakeClient{
190+
apiClient := &fakeClient{
191191
pingFunc: tc.pingFunc,
192192
version: defaultVersion,
193193
}
194194

195-
cli := &DockerCli{client: apiclient}
195+
cli := &DockerCli{client: apiClient}
196196
err := cli.Initialize(flags.NewClientOptions())
197197
assert.NilError(t, err)
198198
assert.DeepEqual(t, cli.ServerInfo(), tc.expectedServer)
199-
assert.Equal(t, apiclient.negotiated, tc.negotiated)
199+
assert.Equal(t, apiClient.negotiated, tc.negotiated)
200200
})
201201
}
202202
}

cli/command/container/create_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
116116
t.Run(tc.PullPolicy, func(t *testing.T) {
117117
pullCounter := 0
118118

119-
client := &fakeClient{
119+
apiClient := &fakeClient{
120120
createContainerFunc: func(
121121
config *container.Config,
122122
hostConfig *container.HostConfig,
@@ -140,7 +140,7 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
140140
return system.Info{IndexServerAddress: "https://indexserver.example.com"}, nil
141141
},
142142
}
143-
fakeCLI := test.NewFakeCli(client)
143+
fakeCLI := test.NewFakeCli(apiClient)
144144
id, err := createContainer(context.Background(), fakeCLI, config, &createOptions{
145145
name: "name",
146146
platform: runtime.GOOS,

cli/command/container/exec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,13 @@ func TestGetExecExitStatus(t *testing.T) {
234234
}
235235

236236
for _, testcase := range testcases {
237-
client := &fakeClient{
237+
apiClient := &fakeClient{
238238
execInspectFunc: func(id string) (container.ExecInspect, error) {
239239
assert.Check(t, is.Equal(execID, id))
240240
return container.ExecInspect{ExitCode: testcase.exitCode}, testcase.inspectError
241241
},
242242
}
243-
err := getExecExitStatus(context.Background(), client, execID)
243+
err := getExecExitStatus(context.Background(), apiClient, execID)
244244
assert.Check(t, is.Equal(testcase.expectedError, err))
245245
}
246246
}

cli/command/idresolver/idresolver_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ import (
1212
)
1313

1414
func TestResolveError(t *testing.T) {
15-
cli := &fakeClient{
15+
apiClient := &fakeClient{
1616
nodeInspectFunc: func(nodeID string) (swarm.Node, []byte, error) {
1717
return swarm.Node{}, []byte{}, errors.New("error inspecting node")
1818
},
1919
}
2020

21-
idResolver := New(cli, false)
21+
idResolver := New(apiClient, false)
2222
_, err := idResolver.Resolve(context.Background(), struct{}{}, "nodeID")
2323

2424
assert.Error(t, err, "unsupported type")
2525
}
2626

2727
func TestResolveWithNoResolveOption(t *testing.T) {
2828
resolved := false
29-
cli := &fakeClient{
29+
apiClient := &fakeClient{
3030
nodeInspectFunc: func(nodeID string) (swarm.Node, []byte, error) {
3131
resolved = true
3232
return swarm.Node{}, []byte{}, nil
@@ -37,7 +37,7 @@ func TestResolveWithNoResolveOption(t *testing.T) {
3737
},
3838
}
3939

40-
idResolver := New(cli, true)
40+
idResolver := New(apiClient, true)
4141
id, err := idResolver.Resolve(context.Background(), swarm.Node{}, "nodeID")
4242

4343
assert.NilError(t, err)
@@ -47,14 +47,14 @@ func TestResolveWithNoResolveOption(t *testing.T) {
4747

4848
func TestResolveWithCache(t *testing.T) {
4949
inspectCounter := 0
50-
cli := &fakeClient{
50+
apiClient := &fakeClient{
5151
nodeInspectFunc: func(nodeID string) (swarm.Node, []byte, error) {
5252
inspectCounter++
5353
return *builders.Node(builders.NodeName("node-foo")), []byte{}, nil
5454
},
5555
}
5656

57-
idResolver := New(cli, false)
57+
idResolver := New(apiClient, false)
5858

5959
ctx := context.Background()
6060
for i := 0; i < 2; i++ {
@@ -97,10 +97,10 @@ func TestResolveNode(t *testing.T) {
9797

9898
ctx := context.Background()
9999
for _, tc := range testCases {
100-
cli := &fakeClient{
100+
apiClient := &fakeClient{
101101
nodeInspectFunc: tc.nodeInspectFunc,
102102
}
103-
idResolver := New(cli, false)
103+
idResolver := New(apiClient, false)
104104
id, err := idResolver.Resolve(ctx, swarm.Node{}, tc.nodeID)
105105

106106
assert.NilError(t, err)
@@ -132,10 +132,10 @@ func TestResolveService(t *testing.T) {
132132

133133
ctx := context.Background()
134134
for _, tc := range testCases {
135-
cli := &fakeClient{
135+
apiClient := &fakeClient{
136136
serviceInspectFunc: tc.serviceInspectFunc,
137137
}
138-
idResolver := New(cli, false)
138+
idResolver := New(apiClient, false)
139139
id, err := idResolver.Resolve(ctx, swarm.Service{}, tc.serviceID)
140140

141141
assert.NilError(t, err)

cli/command/network/list.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
4545
return cmd
4646
}
4747

48-
func runList(ctx context.Context, dockerCli command.Cli, options listOptions) error {
49-
client := dockerCli.Client()
50-
networkResources, err := client.NetworkList(ctx, network.ListOptions{Filters: options.filter.Value()})
48+
func runList(ctx context.Context, dockerCLI command.Cli, options listOptions) error {
49+
apiClient := dockerCLI.Client()
50+
networkResources, err := apiClient.NetworkList(ctx, network.ListOptions{Filters: options.filter.Value()})
5151
if err != nil {
5252
return err
5353
}
5454

5555
format := options.format
5656
if len(format) == 0 {
57-
if len(dockerCli.ConfigFile().NetworksFormat) > 0 && !options.quiet {
58-
format = dockerCli.ConfigFile().NetworksFormat
57+
if len(dockerCLI.ConfigFile().NetworksFormat) > 0 && !options.quiet {
58+
format = dockerCLI.ConfigFile().NetworksFormat
5959
} else {
6060
format = formatter.TableFormatKey
6161
}
@@ -66,7 +66,7 @@ func runList(ctx context.Context, dockerCli command.Cli, options listOptions) er
6666
})
6767

6868
networksCtx := formatter.Context{
69-
Output: dockerCli.Out(),
69+
Output: dockerCLI.Out(),
7070
Format: newFormat(format, options.quiet),
7171
Trunc: !options.noTrunc,
7272
}

cli/command/node/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func Reference(ctx context.Context, apiClient client.APIClient, ref string) (str
5353
// If there's no node ID in /info, the node probably
5454
// isn't a manager. Call a swarm-specific endpoint to
5555
// get a more specific error message.
56+
//
57+
// FIXME(thaJeztah): this should not require calling a Swarm endpoint, and we could just suffice with info / ping (which has swarm status).
5658
_, err = apiClient.NodeList(ctx, swarm.NodeListOptions{})
5759
if err != nil {
5860
return "", err

cli/command/node/demote.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@ func newDemoteCommand(dockerCli command.Cli) *cobra.Command {
2222
}
2323
}
2424

25-
func runDemote(ctx context.Context, dockerCli command.Cli, nodes []string) error {
25+
func runDemote(ctx context.Context, dockerCLI command.Cli, nodes []string) error {
2626
demote := func(node *swarm.Node) error {
2727
if node.Spec.Role == swarm.NodeRoleWorker {
28-
_, _ = fmt.Fprintf(dockerCli.Out(), "Node %s is already a worker.\n", node.ID)
28+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Node %s is already a worker.\n", node.ID)
2929
return errNoRoleChange
3030
}
3131
node.Spec.Role = swarm.NodeRoleWorker
3232
return nil
3333
}
34-
success := func(nodeID string) {
35-
_, _ = fmt.Fprintf(dockerCli.Out(), "Manager %s demoted in the swarm.\n", nodeID)
36-
}
37-
return updateNodes(ctx, dockerCli, nodes, demote, success)
34+
return updateNodes(ctx, dockerCLI.Client(), nodes, demote, func(nodeID string) {
35+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Manager %s demoted in the swarm.\n", nodeID)
36+
})
3837
}

cli/command/node/inspect.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,31 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
4141
return cmd
4242
}
4343

44-
func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error {
45-
client := dockerCli.Client()
44+
func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions) error {
45+
apiClient := dockerCLI.Client()
4646

4747
if opts.pretty {
4848
opts.format = "pretty"
4949
}
5050

5151
getRef := func(ref string) (any, []byte, error) {
52-
nodeRef, err := Reference(ctx, client, ref)
52+
nodeRef, err := Reference(ctx, apiClient, ref)
5353
if err != nil {
5454
return nil, nil, err
5555
}
56-
node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
56+
node, _, err := apiClient.NodeInspectWithRaw(ctx, nodeRef)
5757
return node, nil, err
5858
}
59-
f := opts.format
6059

6160
// check if the user is trying to apply a template to the pretty format, which
6261
// is not supported
63-
if strings.HasPrefix(f, "pretty") && f != "pretty" {
62+
if strings.HasPrefix(opts.format, "pretty") && opts.format != "pretty" {
6463
return errors.New("cannot supply extra formatting options to the pretty template")
6564
}
6665

6766
nodeCtx := formatter.Context{
68-
Output: dockerCli.Out(),
69-
Format: newFormat(f, false),
67+
Output: dockerCLI.Out(),
68+
Format: newFormat(opts.format, false),
7069
}
7170

7271
if err := inspectFormatWrite(nodeCtx, opts.nodeIds, getRef); err != nil {

cli/command/node/list.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
5050
return cmd
5151
}
5252

53-
func runList(ctx context.Context, dockerCli command.Cli, options listOptions) error {
54-
client := dockerCli.Client()
53+
func runList(ctx context.Context, dockerCLI command.Cli, options listOptions) error {
54+
apiClient := dockerCLI.Client()
5555

56-
nodes, err := client.NodeList(
57-
ctx,
58-
swarm.NodeListOptions{Filters: options.filter.Value()})
56+
nodes, err := apiClient.NodeList(ctx, swarm.NodeListOptions{
57+
Filters: options.filter.Value(),
58+
})
5959
if err != nil {
6060
return err
6161
}
6262

63-
info := system.Info{}
63+
var info system.Info
6464
if len(nodes) > 0 && !options.quiet {
6565
// only non-empty nodes and not quiet, should we call /info api
66-
info, err = client.Info(ctx)
66+
info, err = apiClient.Info(ctx)
6767
if err != nil {
6868
return err
6969
}
@@ -72,13 +72,13 @@ func runList(ctx context.Context, dockerCli command.Cli, options listOptions) er
7272
format := options.format
7373
if len(format) == 0 {
7474
format = formatter.TableFormatKey
75-
if len(dockerCli.ConfigFile().NodesFormat) > 0 && !options.quiet {
76-
format = dockerCli.ConfigFile().NodesFormat
75+
if len(dockerCLI.ConfigFile().NodesFormat) > 0 && !options.quiet {
76+
format = dockerCLI.ConfigFile().NodesFormat
7777
}
7878
}
7979

8080
nodesCtx := formatter.Context{
81-
Output: dockerCli.Out(),
81+
Output: dockerCLI.Out(),
8282
Format: newFormat(format, options.quiet),
8383
}
8484
sort.Slice(nodes, func(i, j int) bool {

cli/command/node/promote.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@ func newPromoteCommand(dockerCli command.Cli) *cobra.Command {
2222
}
2323
}
2424

25-
func runPromote(ctx context.Context, dockerCli command.Cli, nodes []string) error {
25+
func runPromote(ctx context.Context, dockerCLI command.Cli, nodes []string) error {
2626
promote := func(node *swarm.Node) error {
2727
if node.Spec.Role == swarm.NodeRoleManager {
28-
_, _ = fmt.Fprintf(dockerCli.Out(), "Node %s is already a manager.\n", node.ID)
28+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Node %s is already a manager.\n", node.ID)
2929
return errNoRoleChange
3030
}
3131
node.Spec.Role = swarm.NodeRoleManager
3232
return nil
3333
}
34-
success := func(nodeID string) {
35-
_, _ = fmt.Fprintf(dockerCli.Out(), "Node %s promoted to a manager in the swarm.\n", nodeID)
36-
}
37-
return updateNodes(ctx, dockerCli, nodes, promote, success)
34+
return updateNodes(ctx, dockerCLI.Client(), nodes, promote, func(nodeID string) {
35+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Node %s promoted to a manager in the swarm.\n", nodeID)
36+
})
3837
}

0 commit comments

Comments
 (0)