From c0e37dda144600b566498520c8bfa77597fed489 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 8 Sep 2025 20:24:34 +0200 Subject: [PATCH] cli/command/container: use stdlib errors Signed-off-by: Sebastiaan van Stijn --- cli/command/container/exec.go | 2 +- cli/command/container/export.go | 5 ++- cli/command/container/list.go | 6 +-- cli/command/container/opts.go | 62 +++++++++++++------------- cli/command/container/port.go | 5 +-- cli/command/container/prune.go | 2 +- cli/command/container/run.go | 2 +- cli/command/container/start.go | 10 ++--- cli/command/container/stats_helpers.go | 2 +- 9 files changed, 48 insertions(+), 48 deletions(-) diff --git a/cli/command/container/exec.go b/cli/command/container/exec.go index 3d3a8595a..48c8d5f30 100644 --- a/cli/command/container/exec.go +++ b/cli/command/container/exec.go @@ -2,6 +2,7 @@ package container import ( "context" + "errors" "fmt" "io" @@ -12,7 +13,6 @@ import ( "github.com/docker/cli/opts" "github.com/moby/moby/api/types/container" "github.com/moby/moby/client" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) diff --git a/cli/command/container/export.go b/cli/command/container/export.go index 8c295847a..1b73e3eae 100644 --- a/cli/command/container/export.go +++ b/cli/command/container/export.go @@ -2,13 +2,14 @@ package container import ( "context" + "errors" + "fmt" "io" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" "github.com/moby/sys/atomicwriter" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -53,7 +54,7 @@ func runExport(ctx context.Context, dockerCLI command.Cli, opts exportOptions) e } else { writer, err := atomicwriter.New(opts.output, 0o600) if err != nil { - return errors.Wrap(err, "failed to export container") + return fmt.Errorf("failed to export container: %w", err) } defer writer.Close() output = writer diff --git a/cli/command/container/list.go b/cli/command/container/list.go index 88b417077..6decdd3bd 100644 --- a/cli/command/container/list.go +++ b/cli/command/container/list.go @@ -2,6 +2,7 @@ package container import ( "context" + "fmt" "io" "github.com/docker/cli/cli" @@ -11,7 +12,6 @@ import ( "github.com/docker/cli/opts" "github.com/docker/cli/templates" "github.com/moby/moby/client" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -84,7 +84,7 @@ func buildContainerListOptions(options *psOptions) (*client.ContainerListOptions if len(options.format) > 0 { tmpl, err := templates.Parse(options.format) if err != nil { - return nil, errors.Wrap(err, "failed to parse template") + return nil, fmt.Errorf("failed to parse template: %w", err) } optionsProcessor := formatter.NewContainerContext() @@ -92,7 +92,7 @@ func buildContainerListOptions(options *psOptions) (*client.ContainerListOptions // This shouldn't error out but swallowing the error makes it harder // to track down if preProcessor issues come up. if err := tmpl.Execute(io.Discard, optionsProcessor); err != nil { - return nil, errors.Wrap(err, "failed to execute template") + return nil, fmt.Errorf("failed to execute template: %w", err) } // if `size` was not explicitly set to false (with `--size=false`) diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index 2db794247..d1faa7a88 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -3,6 +3,7 @@ package container import ( "bytes" "encoding/json" + "errors" "fmt" "os" "path" @@ -19,7 +20,6 @@ import ( "github.com/moby/moby/api/types/container" "github.com/moby/moby/api/types/mount" "github.com/moby/moby/api/types/network" - "github.com/pkg/errors" "github.com/spf13/pflag" cdi "tags.cncf.io/container-device-interface/pkg/parser" ) @@ -351,7 +351,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con // Validate the input mac address if copts.macAddress != "" { if _, err := opts.ValidateMACAddress(copts.macAddress); err != nil { - return nil, errors.Errorf("%s is not a valid mac address", copts.macAddress) + return nil, fmt.Errorf("%s is not a valid mac address", copts.macAddress) } } if copts.stdin { @@ -367,7 +367,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con swappiness := copts.swappiness if swappiness != -1 && (swappiness < 0 || swappiness > 100) { - return nil, errors.Errorf("invalid value: %d. Valid memory swappiness range is 0-100", swappiness) + return nil, fmt.Errorf("invalid value: %d. Valid memory swappiness range is 0-100", swappiness) } var binds []string @@ -442,7 +442,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con // Merge in exposed ports to the map of published ports for _, e := range copts.expose.GetSlice() { if strings.Contains(e, ":") { - return nil, errors.Errorf("invalid port format for --expose: %s", e) + return nil, fmt.Errorf("invalid port format for --expose: %s", e) } // support two formats for expose, original format /[] // or /[] @@ -451,7 +451,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con // if expose a port, the start and end port are the same start, end, err := nat.ParsePortRange(port) if err != nil { - return nil, errors.Errorf("invalid range format for --expose: %s, error: %s", e, err) + return nil, fmt.Errorf("invalid range format for --expose: %s, error: %w", e, err) } for i := start; i <= end; i++ { p, err := nat.NewPort(proto, strconv.FormatUint(i, 10)) @@ -505,22 +505,22 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con pidMode := container.PidMode(copts.pidMode) if !pidMode.Valid() { - return nil, errors.Errorf("--pid: invalid PID mode") + return nil, errors.New("--pid: invalid PID mode") } utsMode := container.UTSMode(copts.utsMode) if !utsMode.Valid() { - return nil, errors.Errorf("--uts: invalid UTS mode") + return nil, errors.New("--uts: invalid UTS mode") } usernsMode := container.UsernsMode(copts.usernsMode) if !usernsMode.Valid() { - return nil, errors.Errorf("--userns: invalid USER mode") + return nil, errors.New("--userns: invalid USER mode") } cgroupnsMode := container.CgroupnsMode(copts.cgroupnsMode) if !cgroupnsMode.Valid() { - return nil, errors.Errorf("--cgroupns: invalid CGROUP mode") + return nil, errors.New("--cgroupns: invalid CGROUP mode") } restartPolicy, err := opts.ParseRestartPolicy(copts.restartPolicy) @@ -555,7 +555,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con copts.healthStartInterval != 0 if copts.noHealthcheck { if haveHealthSettings { - return nil, errors.Errorf("--no-healthcheck conflicts with --health-* options") + return nil, errors.New("--no-healthcheck conflicts with --health-* options") } healthConfig = &container.HealthConfig{Test: []string{"NONE"}} } else if haveHealthSettings { @@ -564,13 +564,13 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con probe = []string{"CMD-SHELL", copts.healthCmd} } if copts.healthInterval < 0 { - return nil, errors.Errorf("--health-interval cannot be negative") + return nil, errors.New("--health-interval cannot be negative") } if copts.healthTimeout < 0 { - return nil, errors.Errorf("--health-timeout cannot be negative") + return nil, errors.New("--health-timeout cannot be negative") } if copts.healthRetries < 0 { - return nil, errors.Errorf("--health-retries cannot be negative") + return nil, errors.New("--health-retries cannot be negative") } if copts.healthStartPeriod < 0 { return nil, errors.New("--health-start-period cannot be negative") @@ -703,7 +703,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con } if copts.autoRemove && !hostConfig.RestartPolicy.IsNone() { - return nil, errors.Errorf("conflicting options: cannot specify both --restart and --rm") + return nil, errors.New("conflicting options: cannot specify both --restart and --rm") } // only set this value if the user provided the flag, else it should default to nil @@ -786,7 +786,7 @@ func parseNetworkOpts(copts *containerOptions) (map[string]*network.EndpointSett return nil, err } if _, ok := endpoints[n.Target]; ok { - return nil, invalidParameter(errors.Errorf("network %q is specified multiple times", n.Target)) + return nil, invalidParameter(fmt.Errorf("network %q is specified multiple times", n.Target)) } // For backward compatibility: if no custom options are provided for the network, @@ -884,7 +884,7 @@ func parseNetworkAttachmentOpt(ep opts.NetworkAttachmentOpts) (*network.Endpoint } if ep.MacAddress != "" { if _, err := opts.ValidateMACAddress(ep.MacAddress); err != nil { - return nil, errors.Errorf("%s is not a valid mac address", ep.MacAddress) + return nil, fmt.Errorf("%s is not a valid mac address", ep.MacAddress) } epConfig.MacAddress = ep.MacAddress } @@ -899,7 +899,7 @@ func convertToStandardNotation(ports []string) ([]string, error) { for _, param := range strings.Split(publish, ",") { k, v, ok := strings.Cut(param, "=") if !ok || k == "" { - return optsList, errors.Errorf("invalid publish opts format (should be name=value but got '%s')", param) + return optsList, fmt.Errorf("invalid publish opts format (should be name=value but got '%s')", param) } params[k] = v } @@ -914,7 +914,7 @@ func convertToStandardNotation(ports []string) ([]string, error) { func parseLoggingOpts(loggingDriver string, loggingOpts []string) (map[string]string, error) { loggingOptsMap := opts.ConvertKVStringsToMap(loggingOpts) if loggingDriver == "none" && len(loggingOpts) > 0 { - return map[string]string{}, errors.Errorf("invalid logging opts for driver %s", loggingDriver) + return map[string]string{}, fmt.Errorf("invalid logging opts for driver %s", loggingDriver) } return loggingOptsMap, nil } @@ -928,7 +928,7 @@ func parseSecurityOpts(securityOpts []string) ([]string, error) { } if (!ok || v == "") && k != "no-new-privileges" { // "no-new-privileges" is the only option that does not require a value. - return securityOpts, errors.Errorf("Invalid --security-opt: %q", opt) + return securityOpts, fmt.Errorf("invalid --security-opt: %q", opt) } if k == "seccomp" { switch v { @@ -939,11 +939,11 @@ func parseSecurityOpts(securityOpts []string) ([]string, error) { // content if it's valid JSON. f, err := os.ReadFile(v) if err != nil { - return securityOpts, errors.Errorf("opening seccomp profile (%s) failed: %v", v, err) + return securityOpts, fmt.Errorf("opening seccomp profile (%s) failed: %w", v, err) } b := bytes.NewBuffer(nil) if err := json.Compact(b, f); err != nil { - return securityOpts, errors.Errorf("compacting json for seccomp profile (%s) failed: %v", v, err) + return securityOpts, fmt.Errorf("compacting json for seccomp profile (%s) failed: %w", v, err) } securityOpts[key] = fmt.Sprintf("seccomp=%s", b.Bytes()) } @@ -978,7 +978,7 @@ func parseStorageOpts(storageOpts []string) (map[string]string, error) { for _, option := range storageOpts { k, v, ok := strings.Cut(option, "=") if !ok { - return nil, errors.Errorf("invalid storage option") + return nil, errors.New("invalid storage option") } m[k] = v } @@ -993,7 +993,7 @@ func parseDevice(device, serverOS string) (container.DeviceMapping, error) { case "windows": return parseWindowsDevice(device) } - return container.DeviceMapping{}, errors.Errorf("unknown server OS: %s", serverOS) + return container.DeviceMapping{}, fmt.Errorf("unknown server OS: %s", serverOS) } // parseLinuxDevice parses a device mapping string to a container.DeviceMapping struct @@ -1017,7 +1017,7 @@ func parseLinuxDevice(device string) (container.DeviceMapping, error) { case 1: src = arr[0] default: - return container.DeviceMapping{}, errors.Errorf("invalid device specification: %s", device) + return container.DeviceMapping{}, fmt.Errorf("invalid device specification: %s", device) } if dst == "" { @@ -1047,7 +1047,7 @@ func validateDeviceCgroupRule(val string) (string, error) { return val, nil } - return val, errors.Errorf("invalid device cgroup format '%s'", val) + return val, fmt.Errorf("invalid device cgroup format '%s'", val) } // validDeviceMode checks if the mode for device is valid or not. @@ -1079,7 +1079,7 @@ func validateDevice(val string, serverOS string) (string, error) { // Windows does validation entirely server-side return val, nil } - return "", errors.Errorf("unknown server OS: %s", serverOS) + return "", fmt.Errorf("unknown server OS: %s", serverOS) } // validateLinuxPath is the implementation of validateDevice knowing that the @@ -1094,12 +1094,12 @@ func validateLinuxPath(val string, validator func(string) bool) (string, error) var mode string if strings.Count(val, ":") > 2 { - return val, errors.Errorf("bad format for path: %s", val) + return val, fmt.Errorf("bad format for path: %s", val) } split := strings.SplitN(val, ":", 3) if split[0] == "" { - return val, errors.Errorf("bad format for path: %s", val) + return val, fmt.Errorf("bad format for path: %s", val) } switch len(split) { case 1: @@ -1118,13 +1118,13 @@ func validateLinuxPath(val string, validator func(string) bool) (string, error) containerPath = split[1] mode = split[2] if isValid := validator(split[2]); !isValid { - return val, errors.Errorf("bad mode specified: %s", mode) + return val, fmt.Errorf("bad mode specified: %s", mode) } val = fmt.Sprintf("%s:%s:%s", split[0], containerPath, mode) } if !path.IsAbs(containerPath) { - return val, errors.Errorf("%s is not an absolute path", containerPath) + return val, fmt.Errorf("%s is not an absolute path", containerPath) } return val, nil } @@ -1137,5 +1137,5 @@ func validateAttach(val string) (string, error) { return s, nil } } - return val, errors.Errorf("valid streams are STDIN, STDOUT and STDERR") + return val, errors.New("valid streams are STDIN, STDOUT and STDERR") } diff --git a/cli/command/container/port.go b/cli/command/container/port.go index af9a59668..1df09417d 100644 --- a/cli/command/container/port.go +++ b/cli/command/container/port.go @@ -13,7 +13,6 @@ import ( "github.com/docker/cli/cli/command/completion" "github.com/fvbommel/sortorder" "github.com/moby/moby/api/types/container" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -66,11 +65,11 @@ func runPort(ctx context.Context, dockerCli command.Cli, opts *portOptions) erro proto = "tcp" } if _, err = strconv.ParseUint(port, 10, 16); err != nil { - return errors.Wrapf(err, "Error: invalid port (%s)", port) + return fmt.Errorf("invalid port (%s): %w", port, err) } frontends, exists := c.NetworkSettings.Ports[container.PortRangeProto(port+"/"+proto)] if !exists || len(frontends) == 0 { - return errors.Errorf("Error: No public port '%s' published for %s", opts.port, opts.container) + return fmt.Errorf("no public port '%s' published for %s", opts.port, opts.container) } for _, frontend := range frontends { out = append(out, net.JoinHostPort(frontend.HostIP, frontend.HostPort)) diff --git a/cli/command/container/prune.go b/cli/command/container/prune.go index 68fa8516b..ef11836bd 100644 --- a/cli/command/container/prune.go +++ b/cli/command/container/prune.go @@ -2,6 +2,7 @@ package container import ( "context" + "errors" "fmt" "github.com/docker/cli/cli" @@ -10,7 +11,6 @@ import ( "github.com/docker/cli/internal/prompt" "github.com/docker/cli/opts" "github.com/docker/go-units" - "github.com/pkg/errors" "github.com/spf13/cobra" ) diff --git a/cli/command/container/run.go b/cli/command/container/run.go index 2df0e90eb..0500928a3 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -2,6 +2,7 @@ package container import ( "context" + "errors" "fmt" "io" "strings" @@ -15,7 +16,6 @@ import ( "github.com/moby/moby/client" "github.com/moby/sys/signal" "github.com/moby/term" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" diff --git a/cli/command/container/start.go b/cli/command/container/start.go index 5af07f72b..5e4d61298 100644 --- a/cli/command/container/start.go +++ b/cli/command/container/start.go @@ -2,6 +2,7 @@ package container import ( "context" + "errors" "fmt" "io" "strings" @@ -13,7 +14,6 @@ import ( "github.com/moby/moby/client" "github.com/moby/sys/signal" "github.com/moby/term" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -162,7 +162,7 @@ func RunStart(ctx context.Context, dockerCli command.Cli, opts *StartOptions) er // 5. Wait for attachment to break. if c.Config.Tty && dockerCli.Out().IsTerminal() { if err := MonitorTtySize(ctx, dockerCli, c.ID, false); err != nil { - fmt.Fprintln(dockerCli.Err(), "Error monitoring TTY size:", err) + _, _ = fmt.Fprintln(dockerCli.Err(), "Error monitoring TTY size:", err) } } if attachErr := <-cErr; attachErr != nil { @@ -197,15 +197,15 @@ func startContainersWithoutAttachments(ctx context.Context, dockerCli command.Cl var failedContainers []string for _, ctr := range containers { if err := dockerCli.Client().ContainerStart(ctx, ctr, client.ContainerStartOptions{}); err != nil { - fmt.Fprintln(dockerCli.Err(), err) + _, _ = fmt.Fprintln(dockerCli.Err(), err) failedContainers = append(failedContainers, ctr) continue } - fmt.Fprintln(dockerCli.Out(), ctr) + _, _ = fmt.Fprintln(dockerCli.Out(), ctr) } if len(failedContainers) > 0 { - return errors.Errorf("Error: failed to start containers: %s", strings.Join(failedContainers, ", ")) + return fmt.Errorf("failed to start containers: %s", strings.Join(failedContainers, ", ")) } return nil } diff --git a/cli/command/container/stats_helpers.go b/cli/command/container/stats_helpers.go index ffef30d7a..e80bcb97b 100644 --- a/cli/command/container/stats_helpers.go +++ b/cli/command/container/stats_helpers.go @@ -3,13 +3,13 @@ package container import ( "context" "encoding/json" + "errors" "io" "sync" "time" "github.com/moby/moby/api/types/container" "github.com/moby/moby/client" - "github.com/pkg/errors" "github.com/sirupsen/logrus" )