Skip to content

Commit bd0546a

Browse files
authored
Merge pull request #6252 from thaJeztah/less_pkg_errors
reduce uses of pkg/errors
2 parents 44eba13 + 27a7947 commit bd0546a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+260
-259
lines changed

cli/cobra.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/fvbommel/sortorder"
1313
"github.com/moby/term"
1414
"github.com/morikuni/aec"
15-
"github.com/pkg/errors"
1615
"github.com/spf13/cobra"
1716
"github.com/spf13/pflag"
1817
)
@@ -204,7 +203,7 @@ var helpCommand = &cobra.Command{
204203
RunE: func(c *cobra.Command, args []string) error {
205204
cmd, args, e := c.Root().Find(args)
206205
if cmd == nil || e != nil || len(args) > 0 {
207-
return errors.Errorf("unknown help topic: %v", strings.Join(args, " "))
206+
return fmt.Errorf("unknown help topic: %v", strings.Join(args, " "))
208207
}
209208
helpFunc := cmd.HelpFunc()
210209
helpFunc(cmd, args)

cli/command/cli.go

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

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"io"
1011
"os"
@@ -26,7 +27,6 @@ import (
2627
"github.com/moby/moby/api/types/build"
2728
"github.com/moby/moby/api/types/swarm"
2829
"github.com/moby/moby/client"
29-
"github.com/pkg/errors"
3030
"github.com/spf13/cobra"
3131
)
3232

@@ -168,7 +168,7 @@ func (cli *DockerCli) BuildKitEnabled() (bool, error) {
168168
if v := os.Getenv("DOCKER_BUILDKIT"); v != "" {
169169
enabled, err := strconv.ParseBool(v)
170170
if err != nil {
171-
return false, errors.Wrap(err, "DOCKER_BUILDKIT environment variable expects boolean value")
171+
return false, fmt.Errorf("DOCKER_BUILDKIT environment variable expects boolean value: %w", err)
172172
}
173173
return enabled, nil
174174
}
@@ -299,7 +299,7 @@ func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile.
299299
}
300300
endpoint, err := resolveDockerEndpoint(contextStore, resolveContextName(opts, configFile))
301301
if err != nil {
302-
return nil, errors.Wrap(err, "unable to resolve docker endpoint")
302+
return nil, fmt.Errorf("unable to resolve docker endpoint: %w", err)
303303
}
304304
return newAPIClientFromEndpoint(endpoint, configFile)
305305
}
@@ -478,7 +478,7 @@ func (cli *DockerCli) initialize() error {
478478
cli.init.Do(func() {
479479
cli.dockerEndpoint, cli.initErr = cli.getDockerEndPoint()
480480
if cli.initErr != nil {
481-
cli.initErr = errors.Wrap(cli.initErr, "unable to resolve docker endpoint")
481+
cli.initErr = fmt.Errorf("unable to resolve docker endpoint: %w", cli.initErr)
482482
return
483483
}
484484
if cli.client == nil {
@@ -546,7 +546,7 @@ func getServerHost(hosts []string, defaultToTLS bool) (string, error) {
546546
case 1:
547547
return dopts.ParseHost(defaultToTLS, hosts[0])
548548
default:
549-
return "", errors.New("Specify only one -H")
549+
return "", errors.New("specify only one -H")
550550
}
551551
}
552552

cli/command/cli_options.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package command
33
import (
44
"context"
55
"encoding/csv"
6+
"fmt"
67
"io"
78
"net/http"
89
"os"
@@ -12,7 +13,6 @@ import (
1213
"github.com/docker/cli/cli/streams"
1314
"github.com/moby/moby/client"
1415
"github.com/moby/term"
15-
"github.com/pkg/errors"
1616
)
1717

1818
// CLIOption is a functional argument to apply options to a [DockerCli]. These
@@ -189,7 +189,7 @@ func withCustomHeadersFromEnv() client.Opt {
189189
csvReader := csv.NewReader(strings.NewReader(value))
190190
fields, err := csvReader.Read()
191191
if err != nil {
192-
return invalidParameter(errors.Errorf(
192+
return invalidParameter(fmt.Errorf(
193193
"failed to parse custom headers from %s environment variable: value must be formatted as comma-separated key=value pairs",
194194
envOverrideHTTPHeaders,
195195
))
@@ -206,7 +206,7 @@ func withCustomHeadersFromEnv() client.Opt {
206206
k = strings.TrimSpace(k)
207207

208208
if k == "" {
209-
return invalidParameter(errors.Errorf(
209+
return invalidParameter(fmt.Errorf(
210210
`failed to set custom headers from %s environment variable: value contains a key=value pair with an empty key: '%s'`,
211211
envOverrideHTTPHeaders, kv,
212212
))
@@ -217,7 +217,7 @@ func withCustomHeadersFromEnv() client.Opt {
217217
// from an environment variable with the same name). In the meantime,
218218
// produce an error to prevent users from depending on this.
219219
if !hasValue {
220-
return invalidParameter(errors.Errorf(
220+
return invalidParameter(fmt.Errorf(
221221
`failed to set custom headers from %s environment variable: missing "=" in key=value pair: '%s'`,
222222
envOverrideHTTPHeaders, kv,
223223
))

cli/command/config/create.go

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

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

@@ -11,7 +12,6 @@ import (
1112
"github.com/docker/cli/opts"
1213
"github.com/moby/moby/api/types/swarm"
1314
"github.com/moby/sys/sequential"
14-
"github.com/pkg/errors"
1515
"github.com/spf13/cobra"
1616
)
1717

@@ -53,7 +53,7 @@ func RunConfigCreate(ctx context.Context, dockerCLI command.Cli, options CreateO
5353

5454
configData, err := readConfigData(dockerCLI.In(), options.File)
5555
if err != nil {
56-
return errors.Errorf("Error reading content from %q: %v", options.File, err)
56+
return fmt.Errorf("error reading content from %q: %v", options.File, err)
5757
}
5858

5959
spec := swarm.ConfigSpec{

cli/command/container/attach.go

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

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

78
"github.com/docker/cli/cli"
@@ -10,7 +11,6 @@ import (
1011
"github.com/moby/moby/api/types/container"
1112
"github.com/moby/moby/client"
1213
"github.com/moby/sys/signal"
13-
"github.com/pkg/errors"
1414
"github.com/sirupsen/logrus"
1515
"github.com/spf13/cobra"
1616
)
@@ -28,13 +28,13 @@ func inspectContainerAndCheckState(ctx context.Context, apiClient client.APIClie
2828
return nil, err
2929
}
3030
if !c.State.Running {
31-
return nil, errors.New("You cannot attach to a stopped container, start it first")
31+
return nil, errors.New("cannot attach to a stopped container, start it first")
3232
}
3333
if c.State.Paused {
34-
return nil, errors.New("You cannot attach to a paused container, unpause it first")
34+
return nil, errors.New("cannot attach to a paused container, unpause it first")
3535
}
3636
if c.State.Restarting {
37-
return nil, errors.New("You cannot attach to a restarting container, wait until it is running")
37+
return nil, errors.New("cannot attach to a restarting container, wait until it is running")
3838
}
3939

4040
return &c, nil

cli/command/container/attach_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestNewAttachCommandErrors(t *testing.T) {
2929
{
3030
name: "client-stopped",
3131
args: []string{"5cb5bb5e4a3b"},
32-
expectedError: "You cannot attach to a stopped container",
32+
expectedError: "cannot attach to a stopped container",
3333
containerInspectFunc: func(containerID string) (container.InspectResponse, error) {
3434
return container.InspectResponse{
3535
ContainerJSONBase: &container.ContainerJSONBase{
@@ -43,7 +43,7 @@ func TestNewAttachCommandErrors(t *testing.T) {
4343
{
4444
name: "client-paused",
4545
args: []string{"5cb5bb5e4a3b"},
46-
expectedError: "You cannot attach to a paused container",
46+
expectedError: "cannot attach to a paused container",
4747
containerInspectFunc: func(containerID string) (container.InspectResponse, error) {
4848
return container.InspectResponse{
4949
ContainerJSONBase: &container.ContainerJSONBase{
@@ -58,7 +58,7 @@ func TestNewAttachCommandErrors(t *testing.T) {
5858
{
5959
name: "client-restarting",
6060
args: []string{"5cb5bb5e4a3b"},
61-
expectedError: "You cannot attach to a restarting container",
61+
expectedError: "cannot attach to a restarting container",
6262
containerInspectFunc: func(containerID string) (container.InspectResponse, error) {
6363
return container.InspectResponse{
6464
ContainerJSONBase: &container.ContainerJSONBase{

cli/command/container/cp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package container
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"fmt"
78
"io"
89
"os"
@@ -19,7 +20,6 @@ import (
1920
"github.com/moby/go-archive"
2021
"github.com/moby/moby/api/types/container"
2122
"github.com/morikuni/aec"
22-
"github.com/pkg/errors"
2323
"github.com/spf13/cobra"
2424
)
2525

@@ -331,7 +331,7 @@ func copyToContainer(ctx context.Context, dockerCLI command.Cli, copyConfig cpCo
331331

332332
// Validate the destination path
333333
if err := command.ValidateOutputPathFileMode(dstStat.Mode); err != nil {
334-
return errors.Wrapf(err, `destination "%s:%s" must be a directory or a regular file`, copyConfig.container, dstPath)
334+
return fmt.Errorf(`destination "%s:%s" must be a directory or a regular file: %w`, copyConfig.container, dstPath, err)
335335
}
336336

337337
// Ignore any error and assume that the parent directory of the destination
@@ -354,7 +354,7 @@ func copyToContainer(ctx context.Context, dockerCLI command.Cli, copyConfig cpCo
354354
content = os.Stdin
355355
resolvedDstPath = dstInfo.Path
356356
if !dstInfo.IsDir {
357-
return errors.Errorf("destination \"%s:%s\" must be a directory", copyConfig.container, dstPath)
357+
return fmt.Errorf(`destination "%s:%s" must be a directory`, copyConfig.container, dstPath)
358358
}
359359
} else {
360360
// Prepare source copy info.

cli/command/container/create.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"archive/tar"
55
"bytes"
66
"context"
7+
"errors"
78
"fmt"
89
"io"
910
"net/netip"
@@ -30,7 +31,6 @@ import (
3031
"github.com/moby/moby/api/types/versions"
3132
"github.com/moby/moby/client"
3233
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
33-
"github.com/pkg/errors"
3434
"github.com/spf13/cobra"
3535
"github.com/spf13/pflag"
3636
)
@@ -80,7 +80,7 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command {
8080
flags.StringVar(&options.pull, "pull", PullImageMissing, `Pull image before creating ("`+PullImageAlways+`", "|`+PullImageMissing+`", "`+PullImageNever+`")`)
8181
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Suppress the pull output")
8282
flags.BoolVarP(&options.useAPISocket, "use-api-socket", "", false, "Bind mount Docker API socket and required auth")
83-
flags.SetAnnotation("use-api-socket", "experimentalCLI", nil) // Marks flag as experimental for now.
83+
_ = flags.SetAnnotation("use-api-socket", "experimentalCLI", nil) // Mark flag as experimental for now.
8484

8585
// Add an explicit help that doesn't have a `-h` to prevent the conflict
8686
// with hostname
@@ -113,7 +113,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
113113
}
114114
}
115115
proxyConfig := dockerCli.ConfigFile().ParseProxyConfig(dockerCli.Client().DaemonHost(), opts.ConvertKVStringsToMapWithNil(copts.env.GetSlice()))
116-
newEnv := []string{}
116+
newEnv := make([]string, 0, len(proxyConfig))
117117
for k, v := range proxyConfig {
118118
if v == nil {
119119
newEnv = append(newEnv, k)
@@ -151,7 +151,9 @@ func pullImage(ctx context.Context, dockerCli command.Cli, img string, options *
151151
if err != nil {
152152
return err
153153
}
154-
defer responseBody.Close()
154+
defer func() {
155+
_ = responseBody.Close()
156+
}()
155157

156158
out := dockerCli.Err()
157159
if options.quiet {
@@ -170,13 +172,13 @@ func (cid *cidFile) Close() error {
170172
if cid.file == nil {
171173
return nil
172174
}
173-
cid.file.Close()
175+
_ = cid.file.Close()
174176

175177
if cid.written {
176178
return nil
177179
}
178180
if err := os.Remove(cid.path); err != nil {
179-
return errors.Wrapf(err, "failed to remove the CID file '%s'", cid.path)
181+
return fmt.Errorf("failed to remove the CID file '%s': %w", cid.path, err)
180182
}
181183

182184
return nil
@@ -187,7 +189,7 @@ func (cid *cidFile) Write(id string) error {
187189
return nil
188190
}
189191
if _, err := cid.file.Write([]byte(id)); err != nil {
190-
return errors.Wrap(err, "failed to write the container ID to the file")
192+
return fmt.Errorf("failed to write the container ID to the file: %w", err)
191193
}
192194
cid.written = true
193195
return nil
@@ -198,12 +200,12 @@ func newCIDFile(cidPath string) (*cidFile, error) {
198200
return &cidFile{}, nil
199201
}
200202
if _, err := os.Stat(cidPath); err == nil {
201-
return nil, errors.Errorf("container ID file found, make sure the other container isn't running or delete %s", cidPath)
203+
return nil, errors.New("container ID file found, make sure the other container isn't running or delete " + cidPath)
202204
}
203205

204206
f, err := os.Create(cidPath)
205207
if err != nil {
206-
return nil, errors.Wrap(err, "failed to create the container ID file")
208+
return nil, fmt.Errorf("failed to create the container ID file: %w", err)
207209
}
208210

209211
return &cidFile{path: cidPath, file: f}, nil
@@ -224,7 +226,9 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
224226
if err != nil {
225227
return "", err
226228
}
227-
defer containerIDFile.Close()
229+
defer func() {
230+
_ = containerIDFile.Close()
231+
}()
228232

229233
ref, err := reference.ParseAnyReference(config.Image)
230234
if err != nil {
@@ -318,7 +322,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
318322
if options.platform != "" && versions.GreaterThanOrEqualTo(dockerCli.Client().ClientVersion(), "1.41") {
319323
p, err := platforms.Parse(options.platform)
320324
if err != nil {
321-
return "", errors.Wrap(invalidParameter(err), "error parsing specified platform")
325+
return "", invalidParameter(fmt.Errorf("error parsing specified platform: %w", err))
322326
}
323327
platform = &p
324328
}
@@ -431,7 +435,7 @@ func copyDockerConfigIntoContainer(ctx context.Context, dockerAPI client.APIClie
431435
// We don't need to get super fancy with the tar creation.
432436
var tarBuf bytes.Buffer
433437
tarWriter := tar.NewWriter(&tarBuf)
434-
tarWriter.WriteHeader(&tar.Header{
438+
_ = tarWriter.WriteHeader(&tar.Header{
435439
Name: configPath,
436440
Size: int64(configBuf.Len()),
437441
Mode: 0o600,

cli/command/defaultcontextstore.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
package command
55

66
import (
7+
"errors"
8+
"fmt"
9+
710
"github.com/docker/cli/cli/context/docker"
811
"github.com/docker/cli/cli/context/store"
912
cliflags "github.com/docker/cli/cli/flags"
10-
"github.com/pkg/errors"
1113
)
1214

1315
const (
@@ -185,7 +187,7 @@ func (s *ContextStoreWithDefault) GetTLSData(contextName, endpointName, fileName
185187
return nil, err
186188
}
187189
if defaultContext.TLS.Endpoints[endpointName].Files[fileName] == nil {
188-
return nil, notFound(errors.Errorf("TLS data for %s/%s/%s does not exist", DefaultContextName, endpointName, fileName))
190+
return nil, notFound(fmt.Errorf("TLS data for %s/%s/%s does not exist", DefaultContextName, endpointName, fileName))
189191
}
190192
return defaultContext.TLS.Endpoints[endpointName].Files[fileName], nil
191193
}

0 commit comments

Comments
 (0)