Skip to content

Commit c057f46

Browse files
committed
cli/command: rename vars for consistency and prevent shadowing
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 1df8feb commit c057f46

File tree

21 files changed

+128
-135
lines changed

21 files changed

+128
-135
lines changed

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
}

cli/command/node/ps.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,22 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command {
5959
return cmd
6060
}
6161

62-
func runPs(ctx context.Context, dockerCli command.Cli, options psOptions) error {
63-
client := dockerCli.Client()
62+
func runPs(ctx context.Context, dockerCLI command.Cli, options psOptions) error {
63+
apiClient := dockerCLI.Client()
6464

6565
var (
6666
errs []string
6767
tasks []swarm.Task
6868
)
6969

7070
for _, nodeID := range options.nodeIDs {
71-
nodeRef, err := Reference(ctx, client, nodeID)
71+
nodeRef, err := Reference(ctx, apiClient, nodeID)
7272
if err != nil {
7373
errs = append(errs, err.Error())
7474
continue
7575
}
7676

77-
node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
77+
node, _, err := apiClient.NodeInspectWithRaw(ctx, nodeRef)
7878
if err != nil {
7979
errs = append(errs, err.Error())
8080
continue
@@ -83,7 +83,7 @@ func runPs(ctx context.Context, dockerCli command.Cli, options psOptions) error
8383
filter := options.filter.Value()
8484
filter.Add("node", node.ID)
8585

86-
nodeTasks, err := client.TaskList(ctx, swarm.TaskListOptions{Filters: filter})
86+
nodeTasks, err := apiClient.TaskList(ctx, swarm.TaskListOptions{Filters: filter})
8787
if err != nil {
8888
errs = append(errs, err.Error())
8989
continue
@@ -94,11 +94,11 @@ func runPs(ctx context.Context, dockerCli command.Cli, options psOptions) error
9494

9595
format := options.format
9696
if len(format) == 0 {
97-
format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
97+
format = task.DefaultFormat(dockerCLI.ConfigFile(), options.quiet)
9898
}
9999

100100
if len(errs) == 0 || len(tasks) != 0 {
101-
if err := task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format); err != nil {
101+
if err := task.Print(ctx, dockerCLI, tasks, idresolver.New(apiClient, options.noResolve), !options.noTrunc, options.quiet, format); err != nil {
102102
errs = append(errs, err.Error())
103103
}
104104
}

cli/command/node/update.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/docker/cli/cli/command/completion"
1010
"github.com/docker/cli/opts"
1111
"github.com/moby/moby/api/types/swarm"
12+
"github.com/moby/moby/client"
1213
"github.com/pkg/errors"
1314
"github.com/spf13/cobra"
1415
"github.com/spf13/pflag"
@@ -47,18 +48,15 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
4748
return cmd
4849
}
4950

50-
func runUpdate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, nodeID string) error {
51-
success := func(_ string) {
52-
fmt.Fprintln(dockerCli.Out(), nodeID)
53-
}
54-
return updateNodes(ctx, dockerCli, []string{nodeID}, mergeNodeUpdate(flags), success)
51+
func runUpdate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, nodeID string) error {
52+
return updateNodes(ctx, dockerCLI.Client(), []string{nodeID}, mergeNodeUpdate(flags), func(_ string) {
53+
_, _ = fmt.Fprintln(dockerCLI.Out(), nodeID)
54+
})
5555
}
5656

57-
func updateNodes(ctx context.Context, dockerCli command.Cli, nodes []string, mergeNode func(node *swarm.Node) error, success func(nodeID string)) error {
58-
client := dockerCli.Client()
59-
57+
func updateNodes(ctx context.Context, apiClient client.NodeAPIClient, nodes []string, mergeNode func(node *swarm.Node) error, success func(nodeID string)) error {
6058
for _, nodeID := range nodes {
61-
node, _, err := client.NodeInspectWithRaw(ctx, nodeID)
59+
node, _, err := apiClient.NodeInspectWithRaw(ctx, nodeID)
6260
if err != nil {
6361
return err
6462
}
@@ -70,7 +68,7 @@ func updateNodes(ctx context.Context, dockerCli command.Cli, nodes []string, mer
7068
}
7169
return err
7270
}
73-
err = client.NodeUpdate(ctx, node.ID, node.Version, node.Spec)
71+
err = apiClient.NodeUpdate(ctx, node.ID, node.Version, node.Spec)
7472
if err != nil {
7573
return err
7674
}

cli/command/secret/create.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func newSecretCreateCommand(dockerCli command.Cli) *cobra.Command {
4949
return cmd
5050
}
5151

52-
func runSecretCreate(ctx context.Context, dockerCli command.Cli, options createOptions) error {
53-
client := dockerCli.Client()
52+
func runSecretCreate(ctx context.Context, dockerCLI command.Cli, options createOptions) error {
53+
apiClient := dockerCLI.Client()
5454

5555
var secretData []byte
5656
if options.driver != "" {
@@ -59,7 +59,7 @@ func runSecretCreate(ctx context.Context, dockerCli command.Cli, options createO
5959
}
6060
} else {
6161
var err error
62-
secretData, err = readSecretData(dockerCli.In(), options.file)
62+
secretData, err = readSecretData(dockerCLI.In(), options.file)
6363
if err != nil {
6464
return err
6565
}
@@ -82,12 +82,12 @@ func runSecretCreate(ctx context.Context, dockerCli command.Cli, options createO
8282
Name: options.templateDriver,
8383
}
8484
}
85-
r, err := client.SecretCreate(ctx, spec)
85+
r, err := apiClient.SecretCreate(ctx, spec)
8686
if err != nil {
8787
return err
8888
}
8989

90-
_, _ = fmt.Fprintln(dockerCli.Out(), r.ID)
90+
_, _ = fmt.Fprintln(dockerCLI.Out(), r.ID)
9191
return nil
9292
}
9393

cli/command/secret/inspect.go

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

44-
func runSecretInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error {
45-
client := dockerCli.Client()
44+
func runSecretInspect(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(id string) (any, []byte, error) {
52-
return client.SecretInspectWithRaw(ctx, id)
52+
return apiClient.SecretInspectWithRaw(ctx, id)
5353
}
54-
f := opts.format
5554

5655
// check if the user is trying to apply a template to the pretty format, which
5756
// is not supported
58-
if strings.HasPrefix(f, "pretty") && f != "pretty" {
57+
if strings.HasPrefix(opts.format, "pretty") && opts.format != "pretty" {
5958
return errors.New("cannot supply extra formatting options to the pretty template")
6059
}
6160

6261
secretCtx := formatter.Context{
63-
Output: dockerCli.Out(),
64-
Format: newFormat(f, false),
62+
Output: dockerCLI.Out(),
63+
Format: newFormat(opts.format, false),
6564
}
6665

6766
if err := inspectFormatWrite(secretCtx, opts.names, getRef); err != nil {

0 commit comments

Comments
 (0)