Skip to content

Commit 70f1147

Browse files
committed
cli/command/trust: use stdlib errors
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent a8f11a2 commit 70f1147

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed

cli/command/trust/key_generate.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/docker/cli/cli/command"
1212
"github.com/docker/cli/cli/trust"
1313
"github.com/docker/cli/internal/lazyregexp"
14-
"github.com/pkg/errors"
1514
"github.com/spf13/cobra"
1615
"github.com/theupdateframework/notary"
1716
"github.com/theupdateframework/notary/trustmanager"
@@ -88,7 +87,7 @@ func validateAndGenerateKey(streams command.Streams, keyName string, workingDir
8887
pubPEM, err := generateKeyAndOutputPubPEM(keyName, privKeyFileStore)
8988
if err != nil {
9089
_, _ = fmt.Fprint(streams.Out(), err)
91-
return errors.Wrapf(err, "failed to generate key for %s", keyName)
90+
return fmt.Errorf("failed to generate key for %s: %w", keyName, err)
9291
}
9392

9493
// Output the public key to a file in the CWD or specified dir
@@ -126,7 +125,7 @@ func writePubKeyPEMToDir(pubPEM pem.Block, keyName, workingDir string) (string,
126125
pubFileName := strings.Join([]string{keyName, "pub"}, ".")
127126
pubFilePath := filepath.Join(workingDir, pubFileName)
128127
if err := os.WriteFile(pubFilePath, pem.EncodeToMemory(&pubPEM), notary.PrivNoExecPerms); err != nil {
129-
return "", errors.Wrapf(err, "failed to write public key to %s", pubFilePath)
128+
return "", fmt.Errorf("failed to write public key to %s: %w", pubFilePath, err)
130129
}
131130
return pubFilePath, nil
132131
}

cli/command/trust/key_load.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package trust
33
import (
44
"bytes"
55
"encoding/pem"
6+
"errors"
67
"fmt"
78
"io"
89
"os"
@@ -11,7 +12,6 @@ import (
1112
"github.com/docker/cli/cli"
1213
"github.com/docker/cli/cli/command"
1314
"github.com/docker/cli/cli/trust"
14-
"github.com/pkg/errors"
1515
"github.com/spf13/cobra"
1616
"github.com/theupdateframework/notary"
1717
"github.com/theupdateframework/notary/storage"
@@ -60,10 +60,10 @@ func loadPrivKey(streams command.Streams, keyPath string, options keyLoadOptions
6060
passRet := trust.GetPassphraseRetriever(streams.In(), streams.Out())
6161
keyBytes, err := getPrivKeyBytesFromPath(keyPath)
6262
if err != nil {
63-
return errors.Wrapf(err, "refusing to load key from %s", keyPath)
63+
return fmt.Errorf("refusing to load key from %s: %w", keyPath, err)
6464
}
6565
if err := loadPrivKeyBytesToStore(keyBytes, privKeyImporters, keyPath, options.keyName, passRet); err != nil {
66-
return errors.Wrapf(err, "error importing key from %s", keyPath)
66+
return fmt.Errorf("error importing key from %s: %w", keyPath, err)
6767
}
6868
_, _ = fmt.Fprintln(streams.Out(), "Successfully imported key from", keyPath)
6969
return nil
@@ -95,7 +95,7 @@ func loadPrivKeyBytesToStore(privKeyBytes []byte, privKeyImporters []trustmanage
9595
return fmt.Errorf("provided file %s is not a supported private key - to add a signer's public key use docker trust signer add", keyPath)
9696
}
9797
if privKeyBytes, err = decodePrivKeyIfNecessary(privKeyBytes, passRet); err != nil {
98-
return errors.Wrapf(err, "cannot load key from provided file %s", keyPath)
98+
return fmt.Errorf("cannot load key from provided file %s: %w", keyPath, err)
9999
}
100100
// Make a reader, rewind the file pointer
101101
return trustmanager.ImportKeys(bytes.NewReader(privKeyBytes), privKeyImporters, keyName, "", passRet)

cli/command/trust/revoke.go

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

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

78
"github.com/docker/cli/cli"
89
"github.com/docker/cli/cli/command"
910
"github.com/docker/cli/cli/command/image"
1011
"github.com/docker/cli/cli/trust"
1112
"github.com/docker/cli/internal/prompt"
12-
"github.com/pkg/errors"
1313
"github.com/spf13/cobra"
1414
"github.com/theupdateframework/notary/client"
1515
"github.com/theupdateframework/notary/tuf/data"
@@ -63,7 +63,7 @@ func revokeTrust(ctx context.Context, dockerCLI command.Cli, remote string, opti
6363
}
6464
defer clearChangeList(notaryRepo)
6565
if err := revokeSignature(notaryRepo, tag); err != nil {
66-
return errors.Wrapf(err, "could not remove signature for %s", remote)
66+
return fmt.Errorf("could not remove signature for %s: %w", remote, err)
6767
}
6868
_, _ = fmt.Fprintf(dockerCLI.Out(), "Successfully deleted signature for %s\n", remote)
6969
return nil

cli/command/trust/sign.go

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

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"path"
@@ -16,7 +17,6 @@ import (
1617
imagetypes "github.com/moby/moby/api/types/image"
1718
registrytypes "github.com/moby/moby/api/types/registry"
1819
"github.com/moby/moby/client"
19-
"github.com/pkg/errors"
2020
"github.com/spf13/cobra"
2121
notaryclient "github.com/theupdateframework/notary/client"
2222
"github.com/theupdateframework/notary/tuf/data"
@@ -127,7 +127,7 @@ func signAndPublishToTarget(out io.Writer, imgRefAndAuth trust.ImageRefAndAuth,
127127
err = notaryRepo.Publish()
128128
}
129129
if err != nil {
130-
return errors.Wrapf(err, "failed to sign %s:%s", imgRefAndAuth.RepoInfo().Name.Name(), tag)
130+
return fmt.Errorf("failed to sign %s:%s: %w", imgRefAndAuth.RepoInfo().Name.Name(), tag, err)
131131
}
132132
_, _ = fmt.Fprintf(out, "Successfully signed %s:%s\n", imgRefAndAuth.RepoInfo().Name.Name(), tag)
133133
return nil
@@ -212,7 +212,7 @@ func initNotaryRepoWithSigners(notaryRepo notaryclient.Repository, newSigner dat
212212
return err
213213
}
214214
if err := addStagedSigner(notaryRepo, newSigner, []data.PublicKey{signerKey}); err != nil {
215-
return errors.Wrapf(err, "could not add signer to repo: %s", strings.TrimPrefix(newSigner.String(), "targets/"))
215+
return fmt.Errorf("could not add signer to repo: %s: %w", strings.TrimPrefix(newSigner.String(), "targets/"), err)
216216
}
217217

218218
return notaryRepo.Publish()

cli/command/trust/signer_add.go

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

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"os"
@@ -14,7 +15,6 @@ import (
1415
"github.com/docker/cli/cli/trust"
1516
"github.com/docker/cli/internal/lazyregexp"
1617
"github.com/docker/cli/opts"
17-
"github.com/pkg/errors"
1818
"github.com/spf13/cobra"
1919
"github.com/theupdateframework/notary/client"
2020
"github.com/theupdateframework/notary/tuf/data"
@@ -106,7 +106,7 @@ func addSignerToRepo(ctx context.Context, dockerCLI command.Cli, signerName stri
106106
newSignerRoleName := data.RoleName(path.Join(data.CanonicalTargetsRole.String(), signerName))
107107

108108
if err := addStagedSigner(notaryRepo, newSignerRoleName, signerPubKeys); err != nil {
109-
return errors.Wrapf(err, "could not add signer to repo: %s", strings.TrimPrefix(newSignerRoleName.String(), "targets/"))
109+
return fmt.Errorf("could not add signer to repo: %s: %w", strings.TrimPrefix(newSignerRoleName.String(), "targets/"), err)
110110
}
111111

112112
return notaryRepo.Publish()
@@ -118,20 +118,20 @@ func ingestPublicKeys(pubKeyPaths []string) ([]data.PublicKey, error) {
118118
// Read public key bytes from PEM file, limit to 1 KiB
119119
pubKeyFile, err := os.OpenFile(pubKeyPath, os.O_RDONLY, 0o666)
120120
if err != nil {
121-
return nil, errors.Wrap(err, "unable to read public key from file")
121+
return nil, fmt.Errorf("unable to read public key from file: %w", err)
122122
}
123123
defer pubKeyFile.Close()
124124
// limit to
125125
l := io.LimitReader(pubKeyFile, 1<<20)
126126
pubKeyBytes, err := io.ReadAll(l)
127127
if err != nil {
128-
return nil, errors.Wrap(err, "unable to read public key from file")
128+
return nil, fmt.Errorf("unable to read public key from file: %w", err)
129129
}
130130

131131
// Parse PEM bytes into type PublicKey
132132
pubKey, err := tufutils.ParsePEMPublicKey(pubKeyBytes)
133133
if err != nil {
134-
return nil, errors.Wrapf(err, "could not parse public key from file: %s", pubKeyPath)
134+
return nil, fmt.Errorf("could not parse public key from file: %s: %w", pubKeyPath, err)
135135
}
136136
pubKeys = append(pubKeys, pubKey)
137137
}

cli/command/trust/signer_remove.go

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

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

@@ -10,7 +11,6 @@ import (
1011
"github.com/docker/cli/cli/command/image"
1112
"github.com/docker/cli/cli/trust"
1213
"github.com/docker/cli/internal/prompt"
13-
"github.com/pkg/errors"
1414
"github.com/spf13/cobra"
1515
"github.com/theupdateframework/notary/client"
1616
"github.com/theupdateframework/notary/tuf/data"
@@ -49,7 +49,7 @@ func removeSigner(ctx context.Context, dockerCLI command.Cli, options signerRemo
4949
}
5050
}
5151
if len(errRepos) > 0 {
52-
return errors.Errorf("error removing signer from: %s", strings.Join(errRepos, ", "))
52+
return fmt.Errorf("error removing signer from: %s", strings.Join(errRepos, ", "))
5353
}
5454
return nil
5555
}
@@ -98,15 +98,15 @@ func removeSingleSigner(ctx context.Context, dockerCLI command.Cli, repoName, si
9898

9999
signerDelegation := data.RoleName("targets/" + signerName)
100100
if signerDelegation == releasesRoleTUFName {
101-
return false, errors.Errorf("releases is a reserved keyword and cannot be removed")
101+
return false, errors.New("releases is a reserved keyword and cannot be removed")
102102
}
103103
notaryRepo, err := newNotaryClient(dockerCLI, imgRefAndAuth, trust.ActionsPushAndPull)
104104
if err != nil {
105105
return false, trust.NotaryError(imgRefAndAuth.Reference().Name(), err)
106106
}
107107
delegationRoles, err := notaryRepo.GetDelegationRoles()
108108
if err != nil {
109-
return false, errors.Wrapf(err, "error retrieving signers for %s", repoName)
109+
return false, fmt.Errorf("error retrieving signers for %s: %w", repoName, err)
110110
}
111111
var role data.Role
112112
for _, delRole := range delegationRoles {
@@ -116,7 +116,7 @@ func removeSingleSigner(ctx context.Context, dockerCLI command.Cli, repoName, si
116116
}
117117
}
118118
if role.Name == "" {
119-
return false, errors.Errorf("no signer %s for repository %s", signerName, repoName)
119+
return false, fmt.Errorf("no signer %s for repository %s", signerName, repoName)
120120
}
121121
allRoles, err := notaryRepo.ListRoles()
122122
if err != nil {

0 commit comments

Comments
 (0)