Skip to content

Commit 925b8fe

Browse files
committed
cli/command/stack: minor cleanups: use Println, rename vars
- use Println to print newline instead of custom format - use dockerCLI with Go's standard camelCase casing. - suppress some errors to make my IDE and linters happier Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent aa74f93 commit 925b8fe

5 files changed

Lines changed: 51 additions & 51 deletions

File tree

cli/command/stack/loader/loader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ func LoadComposefile(dockerCli command.Cli, opts options.Deploy) (*composetypes.
4040

4141
unsupportedProperties := loader.GetUnsupportedProperties(dicts...)
4242
if len(unsupportedProperties) > 0 {
43-
fmt.Fprintf(dockerCli.Err(), "Ignoring unsupported options: %s\n\n",
43+
_, _ = fmt.Fprintf(dockerCli.Err(), "Ignoring unsupported options: %s\n\n",
4444
strings.Join(unsupportedProperties, ", "))
4545
}
4646

4747
deprecatedProperties := loader.GetDeprecatedProperties(dicts...)
4848
if len(deprecatedProperties) > 0 {
49-
fmt.Fprintf(dockerCli.Err(), "Ignoring deprecated options:\n\n%s\n\n",
49+
_, _ = fmt.Fprintf(dockerCli.Err(), "Ignoring deprecated options:\n\n%s\n\n",
5050
propertyWarnings(deprecatedProperties))
5151
}
5252
return config, nil

cli/command/stack/services.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,28 @@ func RunServices(ctx context.Context, dockerCli command.Cli, opts options.Servic
5252
return formatWrite(dockerCli, services, opts)
5353
}
5454

55-
func formatWrite(dockerCli command.Cli, services []swarmtypes.Service, opts options.Services) error {
55+
func formatWrite(dockerCLI command.Cli, services []swarmtypes.Service, opts options.Services) error {
5656
// if no services in the stack, print message and exit 0
5757
if len(services) == 0 {
58-
_, _ = fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", opts.Namespace)
58+
_, _ = fmt.Fprintln(dockerCLI.Err(), "Nothing found in stack:", opts.Namespace)
5959
return nil
6060
}
6161
sort.Slice(services, func(i, j int) bool {
6262
return sortorder.NaturalLess(services[i].Spec.Name, services[j].Spec.Name)
6363
})
6464

65-
format := opts.Format
66-
if len(format) == 0 {
67-
if len(dockerCli.ConfigFile().ServicesFormat) > 0 && !opts.Quiet {
68-
format = dockerCli.ConfigFile().ServicesFormat
65+
f := opts.Format
66+
if len(f) == 0 {
67+
if len(dockerCLI.ConfigFile().ServicesFormat) > 0 && !opts.Quiet {
68+
f = dockerCLI.ConfigFile().ServicesFormat
6969
} else {
70-
format = formatter.TableFormatKey
70+
f = formatter.TableFormatKey
7171
}
7272
}
7373

7474
servicesCtx := formatter.Context{
75-
Output: dockerCli.Out(),
76-
Format: service.NewListFormat(format, opts.Quiet),
75+
Output: dockerCLI.Out(),
76+
Format: service.NewListFormat(f, opts.Quiet),
7777
}
7878
return service.ListFormatWrite(servicesCtx, services)
7979
}

cli/command/stack/swarm/deploy.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ const (
2323
)
2424

2525
// RunDeploy is the swarm implementation of docker stack deploy
26-
func RunDeploy(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, opts *options.Deploy, cfg *composetypes.Config) error {
26+
func RunDeploy(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, opts *options.Deploy, cfg *composetypes.Config) error {
2727
if err := validateResolveImageFlag(opts); err != nil {
2828
return err
2929
}
3030
// client side image resolution should not be done when the supported
3131
// server version is older than 1.30
32-
if versions.LessThan(dockerCli.Client().ClientVersion(), "1.30") {
32+
if versions.LessThan(dockerCLI.Client().ClientVersion(), "1.30") {
3333
opts.ResolveImage = ResolveImageNever
3434
}
3535

3636
if opts.Detach && !flags.Changed("detach") {
37-
fmt.Fprintln(dockerCli.Err(), "Since --detach=false was not specified, tasks will be created in the background.\n"+
37+
_, _ = fmt.Fprintln(dockerCLI.Err(), "Since --detach=false was not specified, tasks will be created in the background.\n"+
3838
"In a future release, --detach=false will become the default.")
3939
}
4040

41-
return deployCompose(ctx, dockerCli, opts, cfg)
41+
return deployCompose(ctx, dockerCLI, opts, cfg)
4242
}
4343

4444
// validateResolveImageFlag validates the opts.resolveImage command line option
@@ -67,12 +67,12 @@ func checkDaemonIsSwarmManager(ctx context.Context, dockerCli command.Cli) error
6767
}
6868

6969
// pruneServices removes services that are no longer referenced in the source
70-
func pruneServices(ctx context.Context, dockerCli command.Cli, namespace convert.Namespace, services map[string]struct{}) {
71-
client := dockerCli.Client()
70+
func pruneServices(ctx context.Context, dockerCCLI command.Cli, namespace convert.Namespace, services map[string]struct{}) {
71+
apiClient := dockerCCLI.Client()
7272

73-
oldServices, err := getStackServices(ctx, client, namespace.Name())
73+
oldServices, err := getStackServices(ctx, apiClient, namespace.Name())
7474
if err != nil {
75-
fmt.Fprintf(dockerCli.Err(), "Failed to list services: %s\n", err)
75+
_, _ = fmt.Fprintln(dockerCCLI.Err(), "Failed to list services:", err)
7676
}
7777

7878
pruneServices := []swarm.Service{}
@@ -81,5 +81,5 @@ func pruneServices(ctx context.Context, dockerCli command.Cli, namespace convert
8181
pruneServices = append(pruneServices, service)
8282
}
8383
}
84-
removeServices(ctx, dockerCli, pruneServices)
84+
removeServices(ctx, dockerCCLI, pruneServices)
8585
}

cli/command/stack/swarm/deploy_composefile.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ func validateExternalNetworks(ctx context.Context, apiClient client.NetworkAPICl
109109
return nil
110110
}
111111

112-
func createSecrets(ctx context.Context, dockerCli command.Cli, secrets []swarm.SecretSpec) error {
113-
apiClient := dockerCli.Client()
112+
func createSecrets(ctx context.Context, dockerCLI command.Cli, secrets []swarm.SecretSpec) error {
113+
apiClient := dockerCLI.Client()
114114

115115
for _, secretSpec := range secrets {
116116
secret, _, err := apiClient.SecretInspectWithRaw(ctx, secretSpec.Name)
@@ -122,7 +122,7 @@ func createSecrets(ctx context.Context, dockerCli command.Cli, secrets []swarm.S
122122
}
123123
case errdefs.IsNotFound(err):
124124
// secret does not exist, then we create a new one.
125-
fmt.Fprintf(dockerCli.Out(), "Creating secret %s\n", secretSpec.Name)
125+
_, _ = fmt.Fprintln(dockerCLI.Out(), "Creating secret", secretSpec.Name)
126126
if _, err := apiClient.SecretCreate(ctx, secretSpec); err != nil {
127127
return fmt.Errorf("failed to create secret %s: %w", secretSpec.Name, err)
128128
}
@@ -133,8 +133,8 @@ func createSecrets(ctx context.Context, dockerCli command.Cli, secrets []swarm.S
133133
return nil
134134
}
135135

136-
func createConfigs(ctx context.Context, dockerCli command.Cli, configs []swarm.ConfigSpec) error {
137-
apiClient := dockerCli.Client()
136+
func createConfigs(ctx context.Context, dockerCLI command.Cli, configs []swarm.ConfigSpec) error {
137+
apiClient := dockerCLI.Client()
138138

139139
for _, configSpec := range configs {
140140
config, _, err := apiClient.ConfigInspectWithRaw(ctx, configSpec.Name)
@@ -146,7 +146,7 @@ func createConfigs(ctx context.Context, dockerCli command.Cli, configs []swarm.C
146146
}
147147
case errdefs.IsNotFound(err):
148148
// config does not exist, then we create a new one.
149-
fmt.Fprintf(dockerCli.Out(), "Creating config %s\n", configSpec.Name)
149+
_, _ = fmt.Fprintln(dockerCLI.Out(), "Creating config", configSpec.Name)
150150
if _, err := apiClient.ConfigCreate(ctx, configSpec); err != nil {
151151
return fmt.Errorf("failed to create config %s: %w", configSpec.Name, err)
152152
}
@@ -157,8 +157,8 @@ func createConfigs(ctx context.Context, dockerCli command.Cli, configs []swarm.C
157157
return nil
158158
}
159159

160-
func createNetworks(ctx context.Context, dockerCli command.Cli, namespace convert.Namespace, networks map[string]network.CreateOptions) error {
161-
apiClient := dockerCli.Client()
160+
func createNetworks(ctx context.Context, dockerCLI command.Cli, namespace convert.Namespace, networks map[string]network.CreateOptions) error {
161+
apiClient := dockerCLI.Client()
162162

163163
existingNetworks, err := getStackNetworks(ctx, apiClient, namespace.Name())
164164
if err != nil {
@@ -179,17 +179,17 @@ func createNetworks(ctx context.Context, dockerCli command.Cli, namespace conver
179179
createOpts.Driver = defaultNetworkDriver
180180
}
181181

182-
fmt.Fprintf(dockerCli.Out(), "Creating network %s\n", name)
182+
_, _ = fmt.Fprintln(dockerCLI.Out(), "Creating network", name)
183183
if _, err := apiClient.NetworkCreate(ctx, name, createOpts); err != nil {
184184
return fmt.Errorf("failed to create network %s: %w", name, err)
185185
}
186186
}
187187
return nil
188188
}
189189

190-
func deployServices(ctx context.Context, dockerCli command.Cli, services map[string]swarm.ServiceSpec, namespace convert.Namespace, sendAuth bool, resolveImage string) ([]string, error) {
191-
apiClient := dockerCli.Client()
192-
out := dockerCli.Out()
190+
func deployServices(ctx context.Context, dockerCLI command.Cli, services map[string]swarm.ServiceSpec, namespace convert.Namespace, sendAuth bool, resolveImage string) ([]string, error) {
191+
apiClient := dockerCLI.Client()
192+
out := dockerCLI.Out()
193193

194194
existingServices, err := getStackServices(ctx, apiClient, namespace.Name())
195195
if err != nil {
@@ -212,14 +212,14 @@ func deployServices(ctx context.Context, dockerCli command.Cli, services map[str
212212

213213
if sendAuth {
214214
// Retrieve encoded auth token from the image reference
215-
encodedAuth, err = command.RetrieveAuthTokenFromImage(dockerCli.ConfigFile(), image)
215+
encodedAuth, err = command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), image)
216216
if err != nil {
217217
return nil, err
218218
}
219219
}
220220

221221
if service, exists := existingServiceMap[name]; exists {
222-
fmt.Fprintf(out, "Updating service %s (id: %s)\n", name, service.ID)
222+
_, _ = fmt.Fprintf(out, "Updating service %s (id: %s)\n", name, service.ID)
223223

224224
updateOpts := types.ServiceUpdateOptions{EncodedRegistryAuth: encodedAuth}
225225

@@ -259,12 +259,12 @@ func deployServices(ctx context.Context, dockerCli command.Cli, services map[str
259259
}
260260

261261
for _, warning := range response.Warnings {
262-
fmt.Fprintln(dockerCli.Err(), warning)
262+
_, _ = fmt.Fprintln(dockerCLI.Err(), warning)
263263
}
264264

265265
serviceIDs = append(serviceIDs, service.ID)
266266
} else {
267-
fmt.Fprintf(out, "Creating service %s\n", name)
267+
_, _ = fmt.Fprintln(out, "Creating service", name)
268268

269269
createOpts := types.ServiceCreateOptions{EncodedRegistryAuth: encodedAuth}
270270

cli/command/stack/swarm/remove.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func RunRemove(ctx context.Context, dockerCli command.Cli, opts options.Remove)
4848
}
4949

5050
if len(services)+len(networks)+len(secrets)+len(configs) == 0 {
51-
_, _ = fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", namespace)
51+
_, _ = fmt.Fprintln(dockerCli.Err(), "Nothing found in stack:", namespace)
5252
continue
5353
}
5454

@@ -82,26 +82,26 @@ func sortServiceByName(services []swarm.Service) func(i, j int) bool {
8282
}
8383
}
8484

85-
func removeServices(ctx context.Context, dockerCli command.Cli, services []swarm.Service) bool {
85+
func removeServices(ctx context.Context, dockerCLI command.Cli, services []swarm.Service) bool {
8686
var hasError bool
8787
sort.Slice(services, sortServiceByName(services))
8888
for _, service := range services {
89-
fmt.Fprintf(dockerCli.Out(), "Removing service %s\n", service.Spec.Name)
90-
if err := dockerCli.Client().ServiceRemove(ctx, service.ID); err != nil {
89+
_, _ = fmt.Fprintln(dockerCLI.Out(), "Removing service", service.Spec.Name)
90+
if err := dockerCLI.Client().ServiceRemove(ctx, service.ID); err != nil {
9191
hasError = true
92-
fmt.Fprintf(dockerCli.Err(), "Failed to remove service %s: %s", service.ID, err)
92+
_, _ = fmt.Fprintf(dockerCLI.Err(), "Failed to remove service %s: %s", service.ID, err)
9393
}
9494
}
9595
return hasError
9696
}
9797

98-
func removeNetworks(ctx context.Context, dockerCli command.Cli, networks []network.Summary) bool {
98+
func removeNetworks(ctx context.Context, dockerCLI command.Cli, networks []network.Summary) bool {
9999
var hasError bool
100100
for _, nw := range networks {
101-
fmt.Fprintf(dockerCli.Out(), "Removing network %s\n", nw.Name)
102-
if err := dockerCli.Client().NetworkRemove(ctx, nw.ID); err != nil {
101+
_, _ = fmt.Fprintln(dockerCLI.Out(), "Removing network", nw.Name)
102+
if err := dockerCLI.Client().NetworkRemove(ctx, nw.ID); err != nil {
103103
hasError = true
104-
fmt.Fprintf(dockerCli.Err(), "Failed to remove network %s: %s", nw.ID, err)
104+
_, _ = fmt.Fprintf(dockerCLI.Err(), "Failed to remove network %s: %s", nw.ID, err)
105105
}
106106
}
107107
return hasError
@@ -110,22 +110,22 @@ func removeNetworks(ctx context.Context, dockerCli command.Cli, networks []netwo
110110
func removeSecrets(ctx context.Context, dockerCli command.Cli, secrets []swarm.Secret) bool {
111111
var hasError bool
112112
for _, secret := range secrets {
113-
fmt.Fprintf(dockerCli.Out(), "Removing secret %s\n", secret.Spec.Name)
113+
_, _ = fmt.Fprintln(dockerCli.Out(), "Removing secret", secret.Spec.Name)
114114
if err := dockerCli.Client().SecretRemove(ctx, secret.ID); err != nil {
115115
hasError = true
116-
fmt.Fprintf(dockerCli.Err(), "Failed to remove secret %s: %s", secret.ID, err)
116+
_, _ = fmt.Fprintf(dockerCli.Err(), "Failed to remove secret %s: %s", secret.ID, err)
117117
}
118118
}
119119
return hasError
120120
}
121121

122-
func removeConfigs(ctx context.Context, dockerCli command.Cli, configs []swarm.Config) bool {
122+
func removeConfigs(ctx context.Context, dockerCLI command.Cli, configs []swarm.Config) bool {
123123
var hasError bool
124124
for _, config := range configs {
125-
fmt.Fprintf(dockerCli.Out(), "Removing config %s\n", config.Spec.Name)
126-
if err := dockerCli.Client().ConfigRemove(ctx, config.ID); err != nil {
125+
_, _ = fmt.Fprintln(dockerCLI.Out(), "Removing config", config.Spec.Name)
126+
if err := dockerCLI.Client().ConfigRemove(ctx, config.ID); err != nil {
127127
hasError = true
128-
fmt.Fprintf(dockerCli.Err(), "Failed to remove config %s: %s", config.ID, err)
128+
_, _ = fmt.Fprintf(dockerCLI.Err(), "Failed to remove config %s: %s", config.ID, err)
129129
}
130130
}
131131
return hasError

0 commit comments

Comments
 (0)