From b057ab6d986b58bc9183e174cac370d5da024f96 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 8 Sep 2025 20:23:08 +0200 Subject: [PATCH] cli/command/stack: use stdlib errors Signed-off-by: Sebastiaan van Stijn --- cli/command/stack/config_test.go | 2 +- cli/command/stack/deploy.go | 6 +++--- cli/command/stack/list_utils.go | 2 +- cli/command/stack/loader.go | 15 ++++++++------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/cli/command/stack/config_test.go b/cli/command/stack/config_test.go index f745ec23c2..b4243567e7 100644 --- a/cli/command/stack/config_test.go +++ b/cli/command/stack/config_test.go @@ -15,7 +15,7 @@ func TestConfigWithEmptyComposeFile(t *testing.T) { cmd.SetOut(io.Discard) cmd.SetErr(io.Discard) - assert.ErrorContains(t, cmd.Execute(), `Specify a Compose file`) + assert.ErrorContains(t, cmd.Execute(), `specify a Compose file`) } func TestConfigMergeInterpolation(t *testing.T) { diff --git a/cli/command/stack/deploy.go b/cli/command/stack/deploy.go index 72d6b13dc9..c7f699b15d 100644 --- a/cli/command/stack/deploy.go +++ b/cli/command/stack/deploy.go @@ -2,6 +2,7 @@ package stack import ( "context" + "errors" "fmt" "github.com/docker/cli/cli" @@ -10,7 +11,6 @@ import ( composetypes "github.com/docker/cli/cli/compose/types" "github.com/moby/moby/api/types/swarm" "github.com/moby/moby/api/types/versions" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -80,7 +80,7 @@ func runDeploy(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, case resolveImageAlways, resolveImageChanged, resolveImageNever: // valid options. default: - return errors.Errorf("Invalid option %s for flag --resolve-image", opts.resolveImage) + return fmt.Errorf("invalid option %s for flag --resolve-image", opts.resolveImage) } // client side image resolution should not be done when the supported @@ -108,7 +108,7 @@ func checkDaemonIsSwarmManager(ctx context.Context, dockerCli command.Cli) error return err } if !info.Swarm.ControlAvailable { - return errors.New("this node is not a swarm manager. Use \"docker swarm init\" or \"docker swarm join\" to connect this node to swarm and try again") + return errors.New(`this node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again`) } return nil } diff --git a/cli/command/stack/list_utils.go b/cli/command/stack/list_utils.go index b085642ead..2daafab6a7 100644 --- a/cli/command/stack/list_utils.go +++ b/cli/command/stack/list_utils.go @@ -2,10 +2,10 @@ package stack import ( "context" + "errors" "github.com/docker/cli/cli/compose/convert" "github.com/moby/moby/client" - "github.com/pkg/errors" ) // getStacks lists the swarm stacks with the number of services they contain. diff --git a/cli/command/stack/loader.go b/cli/command/stack/loader.go index 8ff33f1606..fb86685acd 100644 --- a/cli/command/stack/loader.go +++ b/cli/command/stack/loader.go @@ -4,6 +4,7 @@ package stack import ( + "errors" "fmt" "io" "os" @@ -17,7 +18,6 @@ import ( "github.com/docker/cli/cli/compose/loader" "github.com/docker/cli/cli/compose/schema" composetypes "github.com/docker/cli/cli/compose/types" - "github.com/pkg/errors" ) // loadComposeFile parse the composefile specified in the cli and returns its configOptions and version. @@ -30,9 +30,10 @@ func loadComposeFile(streams command.Streams, opts deployOptions) (*composetypes dicts := getDictsFrom(configDetails.ConfigFiles) config, err := loader.Load(configDetails) if err != nil { - if fpe, ok := err.(*loader.ForbiddenPropertiesError); ok { + var fpe *loader.ForbiddenPropertiesError + if errors.As(err, &fpe) { // this error is intentionally formatted multi-line - return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n", propertyWarnings(fpe.Properties)) + return nil, fmt.Errorf("compose file contains unsupported options:\n\n%s\n", propertyWarnings(fpe.Properties)) //nolint:staticcheck // ignore ST1005 } return nil, err @@ -53,10 +54,10 @@ func loadComposeFile(streams command.Streams, opts deployOptions) (*composetypes // Validate if each service has a valid image-reference. for _, svc := range config.Services { if svc.Image == "" { - return nil, errors.Errorf("invalid image reference for service %s: no image specified", svc.Name) + return nil, fmt.Errorf("invalid image reference for service %s: no image specified", svc.Name) } if _, err := reference.ParseAnyReference(svc.Image); err != nil { - return nil, errors.Wrapf(err, "invalid image reference for service %s", svc.Name) + return nil, fmt.Errorf("invalid image reference for service %s: %w", svc.Name, err) } } @@ -87,7 +88,7 @@ func getConfigDetails(composefiles []string, stdin io.Reader) (composetypes.Conf var details composetypes.ConfigDetails if len(composefiles) == 0 { - return details, errors.New("Specify a Compose file (with --compose-file)") + return details, errors.New("specify a Compose file (with --compose-file)") } if composefiles[0] == "-" && len(composefiles) == 1 { @@ -133,7 +134,7 @@ func buildEnvironment(env []string) (map[string]string, error) { k, v, ok := strings.Cut(s, "=") if !ok || k == "" { - return result, errors.Errorf("unexpected environment variable '%s'", s) + return result, fmt.Errorf("unexpected environment variable '%s'", s) } // value may be set, but empty if "s" is like "K=", not "K". result[k] = v