From 573e0bddef882dbdc305c7131fdce95b285a1de2 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 8 Sep 2025 20:23:28 +0200 Subject: [PATCH] cli/command/service: use stdlib errors Signed-off-by: Sebastiaan van Stijn --- cli/command/service/formatter.go | 4 ++-- cli/command/service/generic_resource_opts.go | 4 +--- cli/command/service/inspect.go | 11 ++++++----- cli/command/service/logs.go | 10 +++++----- cli/command/service/opts.go | 14 +++++++------- cli/command/service/parse.go | 12 ++++++------ cli/command/service/ps.go | 2 +- cli/command/service/trust.go | 11 ++++++----- cli/command/service/update.go | 12 ++++++------ 9 files changed, 40 insertions(+), 40 deletions(-) diff --git a/cli/command/service/formatter.go b/cli/command/service/formatter.go index bb15ff16df..ba00600a99 100644 --- a/cli/command/service/formatter.go +++ b/cli/command/service/formatter.go @@ -1,6 +1,7 @@ package service import ( + "errors" "fmt" "sort" "strconv" @@ -16,7 +17,6 @@ import ( "github.com/moby/moby/api/types/mount" "github.com/moby/moby/api/types/network" "github.com/moby/moby/api/types/swarm" - "github.com/pkg/errors" ) const serviceInspectPrettyTemplate formatter.Format = ` @@ -231,7 +231,7 @@ func inspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef, getNetw } service, ok := serviceI.(swarm.Service) if !ok { - return errors.Errorf("got wrong object to inspect") + return errors.New("got wrong object to inspect") } if err := format(&serviceInspectContext{ Service: service, diff --git a/cli/command/service/generic_resource_opts.go b/cli/command/service/generic_resource_opts.go index 9d00d9444a..064111a3db 100644 --- a/cli/command/service/generic_resource_opts.go +++ b/cli/command/service/generic_resource_opts.go @@ -4,8 +4,6 @@ import ( "fmt" "strings" - "github.com/pkg/errors" - "github.com/moby/moby/api/types/swarm" swarmapi "github.com/moby/swarmkit/v2/api" "github.com/moby/swarmkit/v2/api/genericresource" @@ -37,7 +35,7 @@ func ParseGenericResources(value []string) ([]swarm.GenericResource, error) { resources, err := genericresource.Parse(value) if err != nil { - return nil, errors.Wrapf(err, "invalid generic resource specification") + return nil, fmt.Errorf("invalid generic resource specification: %w", err) } swarmResources := genericResourcesFromGRPC(resources) diff --git a/cli/command/service/inspect.go b/cli/command/service/inspect.go index 6afa2ccdba..46b70eb079 100644 --- a/cli/command/service/inspect.go +++ b/cli/command/service/inspect.go @@ -5,6 +5,8 @@ package service import ( "context" + "errors" + "fmt" "strings" "github.com/containerd/errdefs" @@ -13,7 +15,6 @@ import ( "github.com/docker/cli/cli/command/formatter" flagsHelper "github.com/docker/cli/cli/flags" "github.com/moby/moby/client" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -35,7 +36,7 @@ func newInspectCommand(dockerCLI command.Cli) *cobra.Command { opts.refs = args if opts.pretty && len(opts.format) > 0 { - return errors.Errorf("--format is incompatible with human friendly format") + return errors.New("--format is incompatible with human friendly format") } return runInspect(cmd.Context(), dockerCLI, opts) }, @@ -69,7 +70,7 @@ func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions) if err == nil || !errdefs.IsNotFound(err) { return service, nil, err } - return nil, nil, errors.Errorf("Error: no such service: %s", ref) + return nil, nil, fmt.Errorf("no such service: %s", ref) } getNetwork := func(ref string) (any, []byte, error) { @@ -77,7 +78,7 @@ func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions) if err == nil || !errdefs.IsNotFound(err) { return nw, nil, err } - return nil, nil, errors.Errorf("Error: no such network: %s", ref) + return nil, nil, fmt.Errorf("no such network: %s", ref) } f := opts.format @@ -91,7 +92,7 @@ func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions) // check if the user is trying to apply a template to the pretty format, which // is not supported if strings.HasPrefix(f, "pretty") && f != "pretty" { - return errors.Errorf("Cannot supply extra formatting options to the pretty template") + return errors.New("cannot supply extra formatting options to the pretty template") } serviceCtx := formatter.Context{ diff --git a/cli/command/service/logs.go b/cli/command/service/logs.go index 8130a2e451..3a103d9891 100644 --- a/cli/command/service/logs.go +++ b/cli/command/service/logs.go @@ -3,6 +3,7 @@ package service import ( "bytes" "context" + "errors" "fmt" "io" "sort" @@ -18,7 +19,6 @@ import ( "github.com/moby/moby/api/pkg/stdcopy" "github.com/moby/moby/api/types/swarm" "github.com/moby/moby/client" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -260,7 +260,7 @@ func (lw *logWriter) Write(buf []byte) (int, error) { // break up the log line into parts. parts := bytes.SplitN(buf, []byte(" "), numParts) if len(parts) != numParts { - return 0, errors.Errorf("invalid context in log message: %v", string(buf)) + return 0, fmt.Errorf("invalid context in log message: %v", string(buf)) } // parse the details out details, err := logdetails.Parse(string(parts[detailsIndex])) @@ -325,19 +325,19 @@ func (lw *logWriter) Write(buf []byte) (int, error) { func parseContext(details map[string]string) (logContext, error) { nodeID, ok := details["com.docker.swarm.node.id"] if !ok { - return logContext{}, errors.Errorf("missing node id in details: %v", details) + return logContext{}, fmt.Errorf("missing node id in details: %v", details) } delete(details, "com.docker.swarm.node.id") serviceID, ok := details["com.docker.swarm.service.id"] if !ok { - return logContext{}, errors.Errorf("missing service id in details: %v", details) + return logContext{}, fmt.Errorf("missing service id in details: %v", details) } delete(details, "com.docker.swarm.service.id") taskID, ok := details["com.docker.swarm.task.id"] if !ok { - return logContext{}, errors.Errorf("missing task id in details: %s", details) + return logContext{}, fmt.Errorf("missing task id in details: %s", details) } delete(details, "com.docker.swarm.task.id") diff --git a/cli/command/service/opts.go b/cli/command/service/opts.go index f8560b0182..2dc89fffcb 100644 --- a/cli/command/service/opts.go +++ b/cli/command/service/opts.go @@ -5,6 +5,7 @@ package service import ( "context" + "errors" "fmt" "sort" "strconv" @@ -20,7 +21,6 @@ import ( "github.com/moby/moby/client" "github.com/moby/swarmkit/v2/api" "github.com/moby/swarmkit/v2/api/defaults" - "github.com/pkg/errors" "github.com/spf13/pflag" ) @@ -100,7 +100,7 @@ func (o *placementPrefOpts) Set(value string) error { return errors.New(`placement preference must be of the format "="`) } if strategy != "spread" { - return errors.Errorf("unsupported placement preference %s (only spread is supported)", strategy) + return fmt.Errorf("unsupported placement preference %s (only spread is supported)", strategy) } o.prefs = append(o.prefs, swarm.PlacementPreference{ @@ -472,7 +472,7 @@ func (o *healthCheckOptions) toHealthConfig() (*container.HealthConfig, error) { o.retries != 0 if o.noHealthcheck { if haveHealthSettings { - return nil, errors.Errorf("--%s conflicts with --health-* options", flagNoHealthcheck) + return nil, fmt.Errorf("--%s conflicts with --health-* options", flagNoHealthcheck) } healthConfig = &container.HealthConfig{Test: []string{"NONE"}} } else if haveHealthSettings { @@ -610,7 +610,7 @@ func (options *serviceOptions) ToServiceMode() (swarm.ServiceMode, error) { switch options.mode { case "global": if options.replicas.Value() != nil { - return serviceMode, errors.Errorf("replicas can only be used with replicated or replicated-job mode") + return serviceMode, errors.New("replicas can only be used with replicated or replicated-job mode") } if options.maxReplicas > 0 { @@ -646,11 +646,11 @@ func (options *serviceOptions) ToServiceMode() (swarm.ServiceMode, error) { return serviceMode, errors.New("max-concurrent can only be used with replicated-job mode") } if options.replicas.Value() != nil { - return serviceMode, errors.Errorf("replicas can only be used with replicated or replicated-job mode") + return serviceMode, errors.New("replicas can only be used with replicated or replicated-job mode") } serviceMode.GlobalJob = &swarm.GlobalJob{} default: - return serviceMode, errors.Errorf("Unknown mode: %s, only replicated and global supported", options.mode) + return serviceMode, fmt.Errorf("unknown mode: %s, only replicated and global supported", options.mode) } return serviceMode, nil } @@ -718,7 +718,7 @@ func (options *serviceOptions) ToService(ctx context.Context, apiClient client.N // flags are not set, then the values will be nil. If they are non-nil, // then return an error. if (serviceMode.ReplicatedJob != nil || serviceMode.GlobalJob != nil) && (updateConfig != nil || rollbackConfig != nil) { - return service, errors.Errorf("update and rollback configuration is not supported for jobs") + return service, errors.New("update and rollback configuration is not supported for jobs") } networks := convertNetworks(options.networks) diff --git a/cli/command/service/parse.go b/cli/command/service/parse.go index 38c8e82674..9bc8bdb7d9 100644 --- a/cli/command/service/parse.go +++ b/cli/command/service/parse.go @@ -2,11 +2,11 @@ package service import ( "context" + "fmt" "github.com/moby/moby/api/types/filters" "github.com/moby/moby/api/types/swarm" "github.com/moby/moby/client" - "github.com/pkg/errors" ) // ParseSecrets retrieves the secrets with the requested names and fills @@ -20,7 +20,7 @@ func ParseSecrets(ctx context.Context, apiClient client.SecretAPIClient, request for _, secret := range requestedSecrets { if _, exists := secretRefs[secret.File.Name]; exists { - return nil, errors.Errorf("duplicate secret target for %s not allowed", secret.SecretName) + return nil, fmt.Errorf("duplicate secret target for %s not allowed", secret.SecretName) } secretRef := new(swarm.SecretReference) *secretRef = *secret @@ -49,7 +49,7 @@ func ParseSecrets(ctx context.Context, apiClient client.SecretAPIClient, request for _, ref := range secretRefs { id, ok := foundSecrets[ref.SecretName] if !ok { - return nil, errors.Errorf("secret not found: %s", ref.SecretName) + return nil, fmt.Errorf("secret not found: %s", ref.SecretName) } // set the id for the ref to properly assign in swarm @@ -98,7 +98,7 @@ func ParseConfigs(ctx context.Context, apiClient client.ConfigAPIClient, request } if _, exists := configRefs[config.File.Name]; exists { - return nil, errors.Errorf("duplicate config target for %s not allowed", config.ConfigName) + return nil, fmt.Errorf("duplicate config target for %s not allowed", config.ConfigName) } configRefs[config.File.Name] = configRef @@ -129,7 +129,7 @@ func ParseConfigs(ctx context.Context, apiClient client.ConfigAPIClient, request for _, ref := range configRefs { id, ok := foundConfigs[ref.ConfigName] if !ok { - return nil, errors.Errorf("config not found: %s", ref.ConfigName) + return nil, fmt.Errorf("config not found: %s", ref.ConfigName) } // set the id for the ref to properly assign in swarm @@ -144,7 +144,7 @@ func ParseConfigs(ctx context.Context, apiClient client.ConfigAPIClient, request for _, ref := range runtimeRefs { id, ok := foundConfigs[ref.ConfigName] if !ok { - return nil, errors.Errorf("config not found: %s", ref.ConfigName) + return nil, fmt.Errorf("config not found: %s", ref.ConfigName) } ref.ConfigID = id diff --git a/cli/command/service/ps.go b/cli/command/service/ps.go index 4d24ce1ca8..09661c903c 100644 --- a/cli/command/service/ps.go +++ b/cli/command/service/ps.go @@ -2,6 +2,7 @@ package service import ( "context" + "errors" "strings" "github.com/docker/cli/cli" @@ -12,7 +13,6 @@ import ( "github.com/docker/cli/opts" "github.com/moby/moby/api/types/filters" "github.com/moby/moby/client" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cli/command/service/trust.go b/cli/command/service/trust.go index 20377bc24f..cec9f712a0 100644 --- a/cli/command/service/trust.go +++ b/cli/command/service/trust.go @@ -2,6 +2,8 @@ package service import ( "encoding/hex" + "errors" + "fmt" "github.com/distribution/reference" "github.com/docker/cli/cli/command" @@ -9,7 +11,6 @@ import ( "github.com/docker/cli/internal/registry" "github.com/moby/moby/api/types/swarm" "github.com/opencontainers/go-digest" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/theupdateframework/notary/tuf/data" ) @@ -23,7 +24,7 @@ func resolveServiceImageDigestContentTrust(dockerCli command.Cli, service *swarm ref, err := reference.ParseAnyReference(service.TaskTemplate.ContainerSpec.Image) if err != nil { - return errors.Wrapf(err, "invalid reference %s", service.TaskTemplate.ContainerSpec.Image) + return fmt.Errorf("invalid reference %s: %w", service.TaskTemplate.ContainerSpec.Image, err) } // If reference does not have digest (is not canonical nor image id) @@ -40,7 +41,7 @@ func resolveServiceImageDigestContentTrust(dockerCli command.Cli, service *swarm resolvedImage, err := trustedResolveDigest(dockerCli, taggedRef) if err != nil { - return errors.Wrap(err, "failed to resolve image digest using content trust") + return fmt.Errorf("failed to resolve image digest using content trust: %w", err) } resolvedFamiliar := reference.FamiliarString(resolvedImage) logrus.Debugf("resolved image tag to %s using content trust", resolvedFamiliar) @@ -59,7 +60,7 @@ func trustedResolveDigest(cli command.Cli, ref reference.NamedTagged) (reference } notaryRepo, err := trust.GetNotaryRepository(cli.In(), cli.Out(), command.UserAgent(), repoInfo, &authConfig, "pull") if err != nil { - return nil, errors.Wrap(err, "error establishing connection to trust repository") + return nil, fmt.Errorf("error establishing connection to trust repository: %w", err) } t, err := notaryRepo.GetTargetByName(ref.Tag(), trust.ReleasesRole, data.CanonicalTargetsRole) @@ -69,7 +70,7 @@ func trustedResolveDigest(cli command.Cli, ref reference.NamedTagged) (reference // Only get the tag if it's in the top level targets role or the releases delegation role // ignore it if it's in any other delegation roles if t.Role != trust.ReleasesRole && t.Role != data.CanonicalTargetsRole { - return nil, trust.NotaryError(repoInfo.Name.Name(), errors.Errorf("No trust data for %s", reference.FamiliarString(ref))) + return nil, trust.NotaryError(repoInfo.Name.Name(), fmt.Errorf("no trust data for %s", reference.FamiliarString(ref))) } logrus.Debugf("retrieving target for %s role", t.Role) diff --git a/cli/command/service/update.go b/cli/command/service/update.go index 9603d1d998..e51c037c71 100644 --- a/cli/command/service/update.go +++ b/cli/command/service/update.go @@ -2,6 +2,7 @@ package service import ( "context" + "errors" "fmt" "sort" "strings" @@ -18,7 +19,6 @@ import ( "github.com/moby/moby/api/types/versions" "github.com/moby/moby/client" "github.com/moby/swarmkit/v2/api/defaults" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -193,7 +193,7 @@ func runUpdate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, clientSideRollback = true spec = service.PreviousSpec if spec == nil { - return errors.Errorf("service does not have a previous specification to roll back to") + return errors.New("service does not have a previous specification to roll back to") } } else { serverSideRollback = true @@ -950,7 +950,7 @@ func updateMounts(flags *pflag.FlagSet, mounts *[]mount.Mount) error { values := flags.Lookup(flagMountAdd).Value.(*opts.MountOpt).Value() for _, mnt := range values { if _, ok := mountsByTarget[mnt.Target]; ok { - return errors.Errorf("duplicate mount target") + return errors.New("duplicate mount target") } mountsByTarget[mnt.Target] = mnt } @@ -1144,7 +1144,7 @@ func updateReplicas(flags *pflag.FlagSet, serviceMode *swarm.ServiceMode) error } if serviceMode == nil || serviceMode.Replicated == nil { - return errors.Errorf("replicas can only be used with replicated mode") + return errors.New("replicas can only be used with replicated mode") } serviceMode.Replicated.Replicas = flags.Lookup(flagReplicas).Value.(*Uint64Opt).Value() return nil @@ -1284,7 +1284,7 @@ func updateHealthcheck(flags *pflag.FlagSet, containerSpec *swarm.ContainerSpec) } return nil } - return errors.Errorf("--%s conflicts with --health-* options", flagNoHealthcheck) + return fmt.Errorf("--%s conflicts with --health-* options", flagNoHealthcheck) } if len(containerSpec.Healthcheck.Test) > 0 && containerSpec.Healthcheck.Test[0] == "NONE" { containerSpec.Healthcheck.Test = nil @@ -1355,7 +1355,7 @@ func updateNetworks(ctx context.Context, apiClient client.NetworkAPIClient, flag return err } if _, exists := existingNetworks[nwID]; exists { - return errors.Errorf("service is already attached to network %s", nw.Target) + return fmt.Errorf("service is already attached to network %s", nw.Target) } nw.Target = nwID newNetworks = append(newNetworks, nw)