Skip to content

Commit c0e37dd

Browse files
committed
cli/command/container: use stdlib errors
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent b774e75 commit c0e37dd

File tree

9 files changed

+48
-48
lines changed

9 files changed

+48
-48
lines changed

cli/command/container/exec.go

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

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78

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

cli/command/container/export.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package container
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
57
"io"
68

79
"github.com/docker/cli/cli"
810
"github.com/docker/cli/cli/command"
911
"github.com/docker/cli/cli/command/completion"
1012
"github.com/moby/sys/atomicwriter"
11-
"github.com/pkg/errors"
1213
"github.com/spf13/cobra"
1314
)
1415

@@ -53,7 +54,7 @@ func runExport(ctx context.Context, dockerCLI command.Cli, opts exportOptions) e
5354
} else {
5455
writer, err := atomicwriter.New(opts.output, 0o600)
5556
if err != nil {
56-
return errors.Wrap(err, "failed to export container")
57+
return fmt.Errorf("failed to export container: %w", err)
5758
}
5859
defer writer.Close()
5960
output = writer

cli/command/container/list.go

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

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

78
"github.com/docker/cli/cli"
@@ -11,7 +12,6 @@ import (
1112
"github.com/docker/cli/opts"
1213
"github.com/docker/cli/templates"
1314
"github.com/moby/moby/client"
14-
"github.com/pkg/errors"
1515
"github.com/spf13/cobra"
1616
)
1717

@@ -84,15 +84,15 @@ func buildContainerListOptions(options *psOptions) (*client.ContainerListOptions
8484
if len(options.format) > 0 {
8585
tmpl, err := templates.Parse(options.format)
8686
if err != nil {
87-
return nil, errors.Wrap(err, "failed to parse template")
87+
return nil, fmt.Errorf("failed to parse template: %w", err)
8888
}
8989

9090
optionsProcessor := formatter.NewContainerContext()
9191

9292
// This shouldn't error out but swallowing the error makes it harder
9393
// to track down if preProcessor issues come up.
9494
if err := tmpl.Execute(io.Discard, optionsProcessor); err != nil {
95-
return nil, errors.Wrap(err, "failed to execute template")
95+
return nil, fmt.Errorf("failed to execute template: %w", err)
9696
}
9797

9898
// if `size` was not explicitly set to false (with `--size=false`)

cli/command/container/opts.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package container
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"os"
89
"path"
@@ -19,7 +20,6 @@ import (
1920
"github.com/moby/moby/api/types/container"
2021
"github.com/moby/moby/api/types/mount"
2122
"github.com/moby/moby/api/types/network"
22-
"github.com/pkg/errors"
2323
"github.com/spf13/pflag"
2424
cdi "tags.cncf.io/container-device-interface/pkg/parser"
2525
)
@@ -351,7 +351,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
351351
// Validate the input mac address
352352
if copts.macAddress != "" {
353353
if _, err := opts.ValidateMACAddress(copts.macAddress); err != nil {
354-
return nil, errors.Errorf("%s is not a valid mac address", copts.macAddress)
354+
return nil, fmt.Errorf("%s is not a valid mac address", copts.macAddress)
355355
}
356356
}
357357
if copts.stdin {
@@ -367,7 +367,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
367367

368368
swappiness := copts.swappiness
369369
if swappiness != -1 && (swappiness < 0 || swappiness > 100) {
370-
return nil, errors.Errorf("invalid value: %d. Valid memory swappiness range is 0-100", swappiness)
370+
return nil, fmt.Errorf("invalid value: %d. Valid memory swappiness range is 0-100", swappiness)
371371
}
372372

373373
var binds []string
@@ -442,7 +442,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
442442
// Merge in exposed ports to the map of published ports
443443
for _, e := range copts.expose.GetSlice() {
444444
if strings.Contains(e, ":") {
445-
return nil, errors.Errorf("invalid port format for --expose: %s", e)
445+
return nil, fmt.Errorf("invalid port format for --expose: %s", e)
446446
}
447447
// support two formats for expose, original format <portnum>/[<proto>]
448448
// or <startport-endport>/[<proto>]
@@ -451,7 +451,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
451451
// if expose a port, the start and end port are the same
452452
start, end, err := nat.ParsePortRange(port)
453453
if err != nil {
454-
return nil, errors.Errorf("invalid range format for --expose: %s, error: %s", e, err)
454+
return nil, fmt.Errorf("invalid range format for --expose: %s, error: %w", e, err)
455455
}
456456
for i := start; i <= end; i++ {
457457
p, err := nat.NewPort(proto, strconv.FormatUint(i, 10))
@@ -505,22 +505,22 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
505505

506506
pidMode := container.PidMode(copts.pidMode)
507507
if !pidMode.Valid() {
508-
return nil, errors.Errorf("--pid: invalid PID mode")
508+
return nil, errors.New("--pid: invalid PID mode")
509509
}
510510

511511
utsMode := container.UTSMode(copts.utsMode)
512512
if !utsMode.Valid() {
513-
return nil, errors.Errorf("--uts: invalid UTS mode")
513+
return nil, errors.New("--uts: invalid UTS mode")
514514
}
515515

516516
usernsMode := container.UsernsMode(copts.usernsMode)
517517
if !usernsMode.Valid() {
518-
return nil, errors.Errorf("--userns: invalid USER mode")
518+
return nil, errors.New("--userns: invalid USER mode")
519519
}
520520

521521
cgroupnsMode := container.CgroupnsMode(copts.cgroupnsMode)
522522
if !cgroupnsMode.Valid() {
523-
return nil, errors.Errorf("--cgroupns: invalid CGROUP mode")
523+
return nil, errors.New("--cgroupns: invalid CGROUP mode")
524524
}
525525

526526
restartPolicy, err := opts.ParseRestartPolicy(copts.restartPolicy)
@@ -555,7 +555,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
555555
copts.healthStartInterval != 0
556556
if copts.noHealthcheck {
557557
if haveHealthSettings {
558-
return nil, errors.Errorf("--no-healthcheck conflicts with --health-* options")
558+
return nil, errors.New("--no-healthcheck conflicts with --health-* options")
559559
}
560560
healthConfig = &container.HealthConfig{Test: []string{"NONE"}}
561561
} else if haveHealthSettings {
@@ -564,13 +564,13 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
564564
probe = []string{"CMD-SHELL", copts.healthCmd}
565565
}
566566
if copts.healthInterval < 0 {
567-
return nil, errors.Errorf("--health-interval cannot be negative")
567+
return nil, errors.New("--health-interval cannot be negative")
568568
}
569569
if copts.healthTimeout < 0 {
570-
return nil, errors.Errorf("--health-timeout cannot be negative")
570+
return nil, errors.New("--health-timeout cannot be negative")
571571
}
572572
if copts.healthRetries < 0 {
573-
return nil, errors.Errorf("--health-retries cannot be negative")
573+
return nil, errors.New("--health-retries cannot be negative")
574574
}
575575
if copts.healthStartPeriod < 0 {
576576
return nil, errors.New("--health-start-period cannot be negative")
@@ -703,7 +703,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
703703
}
704704

705705
if copts.autoRemove && !hostConfig.RestartPolicy.IsNone() {
706-
return nil, errors.Errorf("conflicting options: cannot specify both --restart and --rm")
706+
return nil, errors.New("conflicting options: cannot specify both --restart and --rm")
707707
}
708708

709709
// 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
786786
return nil, err
787787
}
788788
if _, ok := endpoints[n.Target]; ok {
789-
return nil, invalidParameter(errors.Errorf("network %q is specified multiple times", n.Target))
789+
return nil, invalidParameter(fmt.Errorf("network %q is specified multiple times", n.Target))
790790
}
791791

792792
// For backward compatibility: if no custom options are provided for the network,
@@ -884,7 +884,7 @@ func parseNetworkAttachmentOpt(ep opts.NetworkAttachmentOpts) (*network.Endpoint
884884
}
885885
if ep.MacAddress != "" {
886886
if _, err := opts.ValidateMACAddress(ep.MacAddress); err != nil {
887-
return nil, errors.Errorf("%s is not a valid mac address", ep.MacAddress)
887+
return nil, fmt.Errorf("%s is not a valid mac address", ep.MacAddress)
888888
}
889889
epConfig.MacAddress = ep.MacAddress
890890
}
@@ -899,7 +899,7 @@ func convertToStandardNotation(ports []string) ([]string, error) {
899899
for _, param := range strings.Split(publish, ",") {
900900
k, v, ok := strings.Cut(param, "=")
901901
if !ok || k == "" {
902-
return optsList, errors.Errorf("invalid publish opts format (should be name=value but got '%s')", param)
902+
return optsList, fmt.Errorf("invalid publish opts format (should be name=value but got '%s')", param)
903903
}
904904
params[k] = v
905905
}
@@ -914,7 +914,7 @@ func convertToStandardNotation(ports []string) ([]string, error) {
914914
func parseLoggingOpts(loggingDriver string, loggingOpts []string) (map[string]string, error) {
915915
loggingOptsMap := opts.ConvertKVStringsToMap(loggingOpts)
916916
if loggingDriver == "none" && len(loggingOpts) > 0 {
917-
return map[string]string{}, errors.Errorf("invalid logging opts for driver %s", loggingDriver)
917+
return map[string]string{}, fmt.Errorf("invalid logging opts for driver %s", loggingDriver)
918918
}
919919
return loggingOptsMap, nil
920920
}
@@ -928,7 +928,7 @@ func parseSecurityOpts(securityOpts []string) ([]string, error) {
928928
}
929929
if (!ok || v == "") && k != "no-new-privileges" {
930930
// "no-new-privileges" is the only option that does not require a value.
931-
return securityOpts, errors.Errorf("Invalid --security-opt: %q", opt)
931+
return securityOpts, fmt.Errorf("invalid --security-opt: %q", opt)
932932
}
933933
if k == "seccomp" {
934934
switch v {
@@ -939,11 +939,11 @@ func parseSecurityOpts(securityOpts []string) ([]string, error) {
939939
// content if it's valid JSON.
940940
f, err := os.ReadFile(v)
941941
if err != nil {
942-
return securityOpts, errors.Errorf("opening seccomp profile (%s) failed: %v", v, err)
942+
return securityOpts, fmt.Errorf("opening seccomp profile (%s) failed: %w", v, err)
943943
}
944944
b := bytes.NewBuffer(nil)
945945
if err := json.Compact(b, f); err != nil {
946-
return securityOpts, errors.Errorf("compacting json for seccomp profile (%s) failed: %v", v, err)
946+
return securityOpts, fmt.Errorf("compacting json for seccomp profile (%s) failed: %w", v, err)
947947
}
948948
securityOpts[key] = fmt.Sprintf("seccomp=%s", b.Bytes())
949949
}
@@ -978,7 +978,7 @@ func parseStorageOpts(storageOpts []string) (map[string]string, error) {
978978
for _, option := range storageOpts {
979979
k, v, ok := strings.Cut(option, "=")
980980
if !ok {
981-
return nil, errors.Errorf("invalid storage option")
981+
return nil, errors.New("invalid storage option")
982982
}
983983
m[k] = v
984984
}
@@ -993,7 +993,7 @@ func parseDevice(device, serverOS string) (container.DeviceMapping, error) {
993993
case "windows":
994994
return parseWindowsDevice(device)
995995
}
996-
return container.DeviceMapping{}, errors.Errorf("unknown server OS: %s", serverOS)
996+
return container.DeviceMapping{}, fmt.Errorf("unknown server OS: %s", serverOS)
997997
}
998998

999999
// parseLinuxDevice parses a device mapping string to a container.DeviceMapping struct
@@ -1017,7 +1017,7 @@ func parseLinuxDevice(device string) (container.DeviceMapping, error) {
10171017
case 1:
10181018
src = arr[0]
10191019
default:
1020-
return container.DeviceMapping{}, errors.Errorf("invalid device specification: %s", device)
1020+
return container.DeviceMapping{}, fmt.Errorf("invalid device specification: %s", device)
10211021
}
10221022

10231023
if dst == "" {
@@ -1047,7 +1047,7 @@ func validateDeviceCgroupRule(val string) (string, error) {
10471047
return val, nil
10481048
}
10491049

1050-
return val, errors.Errorf("invalid device cgroup format '%s'", val)
1050+
return val, fmt.Errorf("invalid device cgroup format '%s'", val)
10511051
}
10521052

10531053
// validDeviceMode checks if the mode for device is valid or not.
@@ -1079,7 +1079,7 @@ func validateDevice(val string, serverOS string) (string, error) {
10791079
// Windows does validation entirely server-side
10801080
return val, nil
10811081
}
1082-
return "", errors.Errorf("unknown server OS: %s", serverOS)
1082+
return "", fmt.Errorf("unknown server OS: %s", serverOS)
10831083
}
10841084

10851085
// validateLinuxPath is the implementation of validateDevice knowing that the
@@ -1094,12 +1094,12 @@ func validateLinuxPath(val string, validator func(string) bool) (string, error)
10941094
var mode string
10951095

10961096
if strings.Count(val, ":") > 2 {
1097-
return val, errors.Errorf("bad format for path: %s", val)
1097+
return val, fmt.Errorf("bad format for path: %s", val)
10981098
}
10991099

11001100
split := strings.SplitN(val, ":", 3)
11011101
if split[0] == "" {
1102-
return val, errors.Errorf("bad format for path: %s", val)
1102+
return val, fmt.Errorf("bad format for path: %s", val)
11031103
}
11041104
switch len(split) {
11051105
case 1:
@@ -1118,13 +1118,13 @@ func validateLinuxPath(val string, validator func(string) bool) (string, error)
11181118
containerPath = split[1]
11191119
mode = split[2]
11201120
if isValid := validator(split[2]); !isValid {
1121-
return val, errors.Errorf("bad mode specified: %s", mode)
1121+
return val, fmt.Errorf("bad mode specified: %s", mode)
11221122
}
11231123
val = fmt.Sprintf("%s:%s:%s", split[0], containerPath, mode)
11241124
}
11251125

11261126
if !path.IsAbs(containerPath) {
1127-
return val, errors.Errorf("%s is not an absolute path", containerPath)
1127+
return val, fmt.Errorf("%s is not an absolute path", containerPath)
11281128
}
11291129
return val, nil
11301130
}
@@ -1137,5 +1137,5 @@ func validateAttach(val string) (string, error) {
11371137
return s, nil
11381138
}
11391139
}
1140-
return val, errors.Errorf("valid streams are STDIN, STDOUT and STDERR")
1140+
return val, errors.New("valid streams are STDIN, STDOUT and STDERR")
11411141
}

cli/command/container/port.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/docker/cli/cli/command/completion"
1414
"github.com/fvbommel/sortorder"
1515
"github.com/moby/moby/api/types/container"
16-
"github.com/pkg/errors"
1716
"github.com/spf13/cobra"
1817
)
1918

@@ -66,11 +65,11 @@ func runPort(ctx context.Context, dockerCli command.Cli, opts *portOptions) erro
6665
proto = "tcp"
6766
}
6867
if _, err = strconv.ParseUint(port, 10, 16); err != nil {
69-
return errors.Wrapf(err, "Error: invalid port (%s)", port)
68+
return fmt.Errorf("invalid port (%s): %w", port, err)
7069
}
7170
frontends, exists := c.NetworkSettings.Ports[container.PortRangeProto(port+"/"+proto)]
7271
if !exists || len(frontends) == 0 {
73-
return errors.Errorf("Error: No public port '%s' published for %s", opts.port, opts.container)
72+
return fmt.Errorf("no public port '%s' published for %s", opts.port, opts.container)
7473
}
7574
for _, frontend := range frontends {
7675
out = append(out, net.JoinHostPort(frontend.HostIP, frontend.HostPort))

cli/command/container/prune.go

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

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

78
"github.com/docker/cli/cli"
@@ -10,7 +11,6 @@ import (
1011
"github.com/docker/cli/internal/prompt"
1112
"github.com/docker/cli/opts"
1213
"github.com/docker/go-units"
13-
"github.com/pkg/errors"
1414
"github.com/spf13/cobra"
1515
)
1616

cli/command/container/run.go

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

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"strings"
@@ -15,7 +16,6 @@ import (
1516
"github.com/moby/moby/client"
1617
"github.com/moby/sys/signal"
1718
"github.com/moby/term"
18-
"github.com/pkg/errors"
1919
"github.com/sirupsen/logrus"
2020
"github.com/spf13/cobra"
2121
"github.com/spf13/pflag"

0 commit comments

Comments
 (0)