Skip to content

Commit bd8533a

Browse files
committed
cli/command/service: use stdlib errors
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 44dfe20 commit bd8533a

File tree

9 files changed

+40
-40
lines changed

9 files changed

+40
-40
lines changed

cli/command/service/formatter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service
22

33
import (
4+
"errors"
45
"fmt"
56
"sort"
67
"strconv"
@@ -16,7 +17,6 @@ import (
1617
"github.com/moby/moby/api/types/mount"
1718
"github.com/moby/moby/api/types/network"
1819
"github.com/moby/moby/api/types/swarm"
19-
"github.com/pkg/errors"
2020
)
2121

2222
const serviceInspectPrettyTemplate formatter.Format = `
@@ -231,7 +231,7 @@ func inspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef, getNetw
231231
}
232232
service, ok := serviceI.(swarm.Service)
233233
if !ok {
234-
return errors.Errorf("got wrong object to inspect")
234+
return errors.New("got wrong object to inspect")
235235
}
236236
if err := format(&serviceInspectContext{
237237
Service: service,

cli/command/service/generic_resource_opts.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"strings"
66

7-
"github.com/pkg/errors"
8-
97
"github.com/moby/moby/api/types/swarm"
108
swarmapi "github.com/moby/swarmkit/v2/api"
119
"github.com/moby/swarmkit/v2/api/genericresource"
@@ -37,7 +35,7 @@ func ParseGenericResources(value []string) ([]swarm.GenericResource, error) {
3735

3836
resources, err := genericresource.Parse(value)
3937
if err != nil {
40-
return nil, errors.Wrapf(err, "invalid generic resource specification")
38+
return nil, fmt.Errorf("invalid generic resource specification: %w", err)
4139
}
4240

4341
swarmResources := genericResourcesFromGRPC(resources)

cli/command/service/inspect.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package service
55

66
import (
77
"context"
8+
"errors"
9+
"fmt"
810
"strings"
911

1012
"github.com/containerd/errdefs"
@@ -13,7 +15,6 @@ import (
1315
"github.com/docker/cli/cli/command/formatter"
1416
flagsHelper "github.com/docker/cli/cli/flags"
1517
"github.com/moby/moby/client"
16-
"github.com/pkg/errors"
1718
"github.com/spf13/cobra"
1819
"github.com/spf13/pflag"
1920
)
@@ -35,7 +36,7 @@ func newInspectCommand(dockerCLI command.Cli) *cobra.Command {
3536
opts.refs = args
3637

3738
if opts.pretty && len(opts.format) > 0 {
38-
return errors.Errorf("--format is incompatible with human friendly format")
39+
return errors.New("--format is incompatible with human friendly format")
3940
}
4041
return runInspect(cmd.Context(), dockerCLI, opts)
4142
},
@@ -69,15 +70,15 @@ func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions)
6970
if err == nil || !errdefs.IsNotFound(err) {
7071
return service, nil, err
7172
}
72-
return nil, nil, errors.Errorf("Error: no such service: %s", ref)
73+
return nil, nil, fmt.Errorf("no such service: %s", ref)
7374
}
7475

7576
getNetwork := func(ref string) (any, []byte, error) {
7677
nw, _, err := apiClient.NetworkInspectWithRaw(ctx, ref, client.NetworkInspectOptions{Scope: "swarm"})
7778
if err == nil || !errdefs.IsNotFound(err) {
7879
return nw, nil, err
7980
}
80-
return nil, nil, errors.Errorf("Error: no such network: %s", ref)
81+
return nil, nil, fmt.Errorf("no such network: %s", ref)
8182
}
8283

8384
f := opts.format
@@ -91,7 +92,7 @@ func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions)
9192
// check if the user is trying to apply a template to the pretty format, which
9293
// is not supported
9394
if strings.HasPrefix(f, "pretty") && f != "pretty" {
94-
return errors.Errorf("Cannot supply extra formatting options to the pretty template")
95+
return errors.New("cannot supply extra formatting options to the pretty template")
9596
}
9697

9798
serviceCtx := formatter.Context{

cli/command/service/logs.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"fmt"
78
"io"
89
"sort"
@@ -18,7 +19,6 @@ import (
1819
"github.com/moby/moby/api/pkg/stdcopy"
1920
"github.com/moby/moby/api/types/swarm"
2021
"github.com/moby/moby/client"
21-
"github.com/pkg/errors"
2222
"github.com/spf13/cobra"
2323
"github.com/spf13/pflag"
2424
)
@@ -260,7 +260,7 @@ func (lw *logWriter) Write(buf []byte) (int, error) {
260260
// break up the log line into parts.
261261
parts := bytes.SplitN(buf, []byte(" "), numParts)
262262
if len(parts) != numParts {
263-
return 0, errors.Errorf("invalid context in log message: %v", string(buf))
263+
return 0, fmt.Errorf("invalid context in log message: %v", string(buf))
264264
}
265265
// parse the details out
266266
details, err := logdetails.Parse(string(parts[detailsIndex]))
@@ -325,19 +325,19 @@ func (lw *logWriter) Write(buf []byte) (int, error) {
325325
func parseContext(details map[string]string) (logContext, error) {
326326
nodeID, ok := details["com.docker.swarm.node.id"]
327327
if !ok {
328-
return logContext{}, errors.Errorf("missing node id in details: %v", details)
328+
return logContext{}, fmt.Errorf("missing node id in details: %v", details)
329329
}
330330
delete(details, "com.docker.swarm.node.id")
331331

332332
serviceID, ok := details["com.docker.swarm.service.id"]
333333
if !ok {
334-
return logContext{}, errors.Errorf("missing service id in details: %v", details)
334+
return logContext{}, fmt.Errorf("missing service id in details: %v", details)
335335
}
336336
delete(details, "com.docker.swarm.service.id")
337337

338338
taskID, ok := details["com.docker.swarm.task.id"]
339339
if !ok {
340-
return logContext{}, errors.Errorf("missing task id in details: %s", details)
340+
return logContext{}, fmt.Errorf("missing task id in details: %s", details)
341341
}
342342
delete(details, "com.docker.swarm.task.id")
343343

cli/command/service/opts.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package service
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"sort"
1011
"strconv"
@@ -20,7 +21,6 @@ import (
2021
"github.com/moby/moby/client"
2122
"github.com/moby/swarmkit/v2/api"
2223
"github.com/moby/swarmkit/v2/api/defaults"
23-
"github.com/pkg/errors"
2424
"github.com/spf13/pflag"
2525
)
2626

@@ -100,7 +100,7 @@ func (o *placementPrefOpts) Set(value string) error {
100100
return errors.New(`placement preference must be of the format "<strategy>=<arg>"`)
101101
}
102102
if strategy != "spread" {
103-
return errors.Errorf("unsupported placement preference %s (only spread is supported)", strategy)
103+
return fmt.Errorf("unsupported placement preference %s (only spread is supported)", strategy)
104104
}
105105

106106
o.prefs = append(o.prefs, swarm.PlacementPreference{
@@ -472,7 +472,7 @@ func (o *healthCheckOptions) toHealthConfig() (*container.HealthConfig, error) {
472472
o.retries != 0
473473
if o.noHealthcheck {
474474
if haveHealthSettings {
475-
return nil, errors.Errorf("--%s conflicts with --health-* options", flagNoHealthcheck)
475+
return nil, fmt.Errorf("--%s conflicts with --health-* options", flagNoHealthcheck)
476476
}
477477
healthConfig = &container.HealthConfig{Test: []string{"NONE"}}
478478
} else if haveHealthSettings {
@@ -610,7 +610,7 @@ func (options *serviceOptions) ToServiceMode() (swarm.ServiceMode, error) {
610610
switch options.mode {
611611
case "global":
612612
if options.replicas.Value() != nil {
613-
return serviceMode, errors.Errorf("replicas can only be used with replicated or replicated-job mode")
613+
return serviceMode, errors.New("replicas can only be used with replicated or replicated-job mode")
614614
}
615615

616616
if options.maxReplicas > 0 {
@@ -646,11 +646,11 @@ func (options *serviceOptions) ToServiceMode() (swarm.ServiceMode, error) {
646646
return serviceMode, errors.New("max-concurrent can only be used with replicated-job mode")
647647
}
648648
if options.replicas.Value() != nil {
649-
return serviceMode, errors.Errorf("replicas can only be used with replicated or replicated-job mode")
649+
return serviceMode, errors.New("replicas can only be used with replicated or replicated-job mode")
650650
}
651651
serviceMode.GlobalJob = &swarm.GlobalJob{}
652652
default:
653-
return serviceMode, errors.Errorf("Unknown mode: %s, only replicated and global supported", options.mode)
653+
return serviceMode, fmt.Errorf("unknown mode: %s, only replicated and global supported", options.mode)
654654
}
655655
return serviceMode, nil
656656
}
@@ -718,7 +718,7 @@ func (options *serviceOptions) ToService(ctx context.Context, apiClient client.N
718718
// flags are not set, then the values will be nil. If they are non-nil,
719719
// then return an error.
720720
if (serviceMode.ReplicatedJob != nil || serviceMode.GlobalJob != nil) && (updateConfig != nil || rollbackConfig != nil) {
721-
return service, errors.Errorf("update and rollback configuration is not supported for jobs")
721+
return service, errors.New("update and rollback configuration is not supported for jobs")
722722
}
723723

724724
networks := convertNetworks(options.networks)

cli/command/service/parse.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package service
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/moby/moby/api/types/filters"
78
"github.com/moby/moby/api/types/swarm"
89
"github.com/moby/moby/client"
9-
"github.com/pkg/errors"
1010
)
1111

1212
// ParseSecrets retrieves the secrets with the requested names and fills
@@ -20,7 +20,7 @@ func ParseSecrets(ctx context.Context, apiClient client.SecretAPIClient, request
2020

2121
for _, secret := range requestedSecrets {
2222
if _, exists := secretRefs[secret.File.Name]; exists {
23-
return nil, errors.Errorf("duplicate secret target for %s not allowed", secret.SecretName)
23+
return nil, fmt.Errorf("duplicate secret target for %s not allowed", secret.SecretName)
2424
}
2525
secretRef := new(swarm.SecretReference)
2626
*secretRef = *secret
@@ -49,7 +49,7 @@ func ParseSecrets(ctx context.Context, apiClient client.SecretAPIClient, request
4949
for _, ref := range secretRefs {
5050
id, ok := foundSecrets[ref.SecretName]
5151
if !ok {
52-
return nil, errors.Errorf("secret not found: %s", ref.SecretName)
52+
return nil, fmt.Errorf("secret not found: %s", ref.SecretName)
5353
}
5454

5555
// set the id for the ref to properly assign in swarm
@@ -98,7 +98,7 @@ func ParseConfigs(ctx context.Context, apiClient client.ConfigAPIClient, request
9898
}
9999

100100
if _, exists := configRefs[config.File.Name]; exists {
101-
return nil, errors.Errorf("duplicate config target for %s not allowed", config.ConfigName)
101+
return nil, fmt.Errorf("duplicate config target for %s not allowed", config.ConfigName)
102102
}
103103

104104
configRefs[config.File.Name] = configRef
@@ -129,7 +129,7 @@ func ParseConfigs(ctx context.Context, apiClient client.ConfigAPIClient, request
129129
for _, ref := range configRefs {
130130
id, ok := foundConfigs[ref.ConfigName]
131131
if !ok {
132-
return nil, errors.Errorf("config not found: %s", ref.ConfigName)
132+
return nil, fmt.Errorf("config not found: %s", ref.ConfigName)
133133
}
134134

135135
// set the id for the ref to properly assign in swarm
@@ -144,7 +144,7 @@ func ParseConfigs(ctx context.Context, apiClient client.ConfigAPIClient, request
144144
for _, ref := range runtimeRefs {
145145
id, ok := foundConfigs[ref.ConfigName]
146146
if !ok {
147-
return nil, errors.Errorf("config not found: %s", ref.ConfigName)
147+
return nil, fmt.Errorf("config not found: %s", ref.ConfigName)
148148
}
149149

150150
ref.ConfigID = id

cli/command/service/ps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"context"
5+
"errors"
56
"strings"
67

78
"github.com/docker/cli/cli"
@@ -12,7 +13,6 @@ import (
1213
"github.com/docker/cli/opts"
1314
"github.com/moby/moby/api/types/filters"
1415
"github.com/moby/moby/client"
15-
"github.com/pkg/errors"
1616
"github.com/spf13/cobra"
1717
"github.com/spf13/pflag"
1818
)

cli/command/service/trust.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package service
22

33
import (
44
"encoding/hex"
5+
"errors"
6+
"fmt"
57

68
"github.com/distribution/reference"
79
"github.com/docker/cli/cli/command"
810
"github.com/docker/cli/cli/trust"
911
"github.com/docker/cli/internal/registry"
1012
"github.com/moby/moby/api/types/swarm"
1113
"github.com/opencontainers/go-digest"
12-
"github.com/pkg/errors"
1314
"github.com/sirupsen/logrus"
1415
"github.com/theupdateframework/notary/tuf/data"
1516
)
@@ -23,7 +24,7 @@ func resolveServiceImageDigestContentTrust(dockerCli command.Cli, service *swarm
2324

2425
ref, err := reference.ParseAnyReference(service.TaskTemplate.ContainerSpec.Image)
2526
if err != nil {
26-
return errors.Wrapf(err, "invalid reference %s", service.TaskTemplate.ContainerSpec.Image)
27+
return fmt.Errorf("invalid reference %s: %w", service.TaskTemplate.ContainerSpec.Image, err)
2728
}
2829

2930
// If reference does not have digest (is not canonical nor image id)
@@ -40,7 +41,7 @@ func resolveServiceImageDigestContentTrust(dockerCli command.Cli, service *swarm
4041

4142
resolvedImage, err := trustedResolveDigest(dockerCli, taggedRef)
4243
if err != nil {
43-
return errors.Wrap(err, "failed to resolve image digest using content trust")
44+
return fmt.Errorf("failed to resolve image digest using content trust: %w", err)
4445
}
4546
resolvedFamiliar := reference.FamiliarString(resolvedImage)
4647
logrus.Debugf("resolved image tag to %s using content trust", resolvedFamiliar)
@@ -59,7 +60,7 @@ func trustedResolveDigest(cli command.Cli, ref reference.NamedTagged) (reference
5960
}
6061
notaryRepo, err := trust.GetNotaryRepository(cli.In(), cli.Out(), command.UserAgent(), repoInfo, &authConfig, "pull")
6162
if err != nil {
62-
return nil, errors.Wrap(err, "error establishing connection to trust repository")
63+
return nil, fmt.Errorf("error establishing connection to trust repository: %w", err)
6364
}
6465

6566
t, err := notaryRepo.GetTargetByName(ref.Tag(), trust.ReleasesRole, data.CanonicalTargetsRole)
@@ -69,7 +70,7 @@ func trustedResolveDigest(cli command.Cli, ref reference.NamedTagged) (reference
6970
// Only get the tag if it's in the top level targets role or the releases delegation role
7071
// ignore it if it's in any other delegation roles
7172
if t.Role != trust.ReleasesRole && t.Role != data.CanonicalTargetsRole {
72-
return nil, trust.NotaryError(repoInfo.Name.Name(), errors.Errorf("No trust data for %s", reference.FamiliarString(ref)))
73+
return nil, trust.NotaryError(repoInfo.Name.Name(), fmt.Errorf("no trust data for %s", reference.FamiliarString(ref)))
7374
}
7475

7576
logrus.Debugf("retrieving target for %s role", t.Role)

cli/command/service/update.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"sort"
78
"strings"
@@ -18,7 +19,6 @@ import (
1819
"github.com/moby/moby/api/types/versions"
1920
"github.com/moby/moby/client"
2021
"github.com/moby/swarmkit/v2/api/defaults"
21-
"github.com/pkg/errors"
2222
"github.com/spf13/cobra"
2323
"github.com/spf13/pflag"
2424
)
@@ -193,7 +193,7 @@ func runUpdate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet,
193193
clientSideRollback = true
194194
spec = service.PreviousSpec
195195
if spec == nil {
196-
return errors.Errorf("service does not have a previous specification to roll back to")
196+
return errors.New("service does not have a previous specification to roll back to")
197197
}
198198
} else {
199199
serverSideRollback = true
@@ -950,7 +950,7 @@ func updateMounts(flags *pflag.FlagSet, mounts *[]mount.Mount) error {
950950
values := flags.Lookup(flagMountAdd).Value.(*opts.MountOpt).Value()
951951
for _, mnt := range values {
952952
if _, ok := mountsByTarget[mnt.Target]; ok {
953-
return errors.Errorf("duplicate mount target")
953+
return errors.New("duplicate mount target")
954954
}
955955
mountsByTarget[mnt.Target] = mnt
956956
}
@@ -1144,7 +1144,7 @@ func updateReplicas(flags *pflag.FlagSet, serviceMode *swarm.ServiceMode) error
11441144
}
11451145

11461146
if serviceMode == nil || serviceMode.Replicated == nil {
1147-
return errors.Errorf("replicas can only be used with replicated mode")
1147+
return errors.New("replicas can only be used with replicated mode")
11481148
}
11491149
serviceMode.Replicated.Replicas = flags.Lookup(flagReplicas).Value.(*Uint64Opt).Value()
11501150
return nil
@@ -1284,7 +1284,7 @@ func updateHealthcheck(flags *pflag.FlagSet, containerSpec *swarm.ContainerSpec)
12841284
}
12851285
return nil
12861286
}
1287-
return errors.Errorf("--%s conflicts with --health-* options", flagNoHealthcheck)
1287+
return fmt.Errorf("--%s conflicts with --health-* options", flagNoHealthcheck)
12881288
}
12891289
if len(containerSpec.Healthcheck.Test) > 0 && containerSpec.Healthcheck.Test[0] == "NONE" {
12901290
containerSpec.Healthcheck.Test = nil
@@ -1355,7 +1355,7 @@ func updateNetworks(ctx context.Context, apiClient client.NetworkAPIClient, flag
13551355
return err
13561356
}
13571357
if _, exists := existingNetworks[nwID]; exists {
1358-
return errors.Errorf("service is already attached to network %s", nw.Target)
1358+
return fmt.Errorf("service is already attached to network %s", nw.Target)
13591359
}
13601360
nw.Target = nwID
13611361
newNetworks = append(newNetworks, nw)

0 commit comments

Comments
 (0)