Skip to content

Commit 299aae0

Browse files
committed
cli/command/trust: minor cleanups: use Println, rename vars
- use Println to print newline instead of custom format - use dockerCLI with Go's standard camelCase casing. - suppress some errors to make my IDE and linters happier Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 5cfc89c commit 299aae0

7 files changed

Lines changed: 32 additions & 33 deletions

File tree

cli/command/trust/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions)
5353

5454
// Additional separator between the inspection output of each image
5555
if index < len(opts.remotes)-1 {
56-
fmt.Fprint(dockerCLI.Out(), "\n\n")
56+
_, _ = fmt.Fprint(dockerCLI.Out(), "\n\n")
5757
}
5858
}
5959

cli/command/trust/inspect_pretty.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,42 @@ import (
1212
"github.com/theupdateframework/notary/client"
1313
)
1414

15-
func prettyPrintTrustInfo(ctx context.Context, cli command.Cli, remote string) error {
16-
signatureRows, adminRolesWithSigs, delegationRoles, err := lookupTrustInfo(ctx, cli, remote)
15+
func prettyPrintTrustInfo(ctx context.Context, dockerCLI command.Cli, remote string) error {
16+
signatureRows, adminRolesWithSigs, delegationRoles, err := lookupTrustInfo(ctx, dockerCLI, remote)
1717
if err != nil {
1818
return err
1919
}
2020

2121
if len(signatureRows) > 0 {
22-
fmt.Fprintf(cli.Out(), "\nSignatures for %s\n\n", remote)
22+
_, _ = fmt.Fprintf(dockerCLI.Out(), "\nSignatures for %s\n\n", remote)
2323

24-
if err := printSignatures(cli.Out(), signatureRows); err != nil {
24+
if err := printSignatures(dockerCLI.Out(), signatureRows); err != nil {
2525
return err
2626
}
2727
} else {
28-
fmt.Fprintf(cli.Out(), "\nNo signatures for %s\n\n", remote)
28+
_, _ = fmt.Fprintf(dockerCLI.Out(), "\nNo signatures for %s\n\n", remote)
2929
}
3030
signerRoleToKeyIDs := getDelegationRoleToKeyMap(delegationRoles)
3131

3232
// If we do not have additional signers, do not display
3333
if len(signerRoleToKeyIDs) > 0 {
34-
fmt.Fprintf(cli.Out(), "\nList of signers and their keys for %s\n\n", remote)
35-
if err := printSignerInfo(cli.Out(), signerRoleToKeyIDs); err != nil {
34+
_, _ = fmt.Fprintf(dockerCLI.Out(), "\nList of signers and their keys for %s\n\n", remote)
35+
if err := printSignerInfo(dockerCLI.Out(), signerRoleToKeyIDs); err != nil {
3636
return err
3737
}
3838
}
3939

4040
// This will always have the root and targets information
41-
fmt.Fprintf(cli.Out(), "\nAdministrative keys for %s\n\n", remote)
42-
printSortedAdminKeys(cli.Out(), adminRolesWithSigs)
41+
_, _ = fmt.Fprintf(dockerCLI.Out(), "\nAdministrative keys for %s\n\n", remote)
42+
printSortedAdminKeys(dockerCLI.Out(), adminRolesWithSigs)
4343
return nil
4444
}
4545

4646
func printSortedAdminKeys(out io.Writer, adminRoles []client.RoleWithSignatures) {
4747
sort.Slice(adminRoles, func(i, j int) bool { return adminRoles[i].Name > adminRoles[j].Name })
4848
for _, adminRole := range adminRoles {
4949
if formattedAdminRole := formatAdminRole(adminRole); formattedAdminRole != "" {
50-
fmt.Fprintf(out, " %s", formattedAdminRole)
50+
_, _ = fmt.Fprintf(out, " %s", formattedAdminRole)
5151
}
5252
}
5353
}

cli/command/trust/key_generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func validateAndGenerateKey(streams command.Streams, keyName string, workingDir
7878
if err := validateKeyArgs(keyName, workingDir); err != nil {
7979
return err
8080
}
81-
fmt.Fprintf(streams.Out(), "Generating key for %s...\n", keyName)
81+
_, _ = fmt.Fprintf(streams.Out(), "Generating key for %s...\n", keyName)
8282
// Automatically load the private key to local storage for use
8383
privKeyFileStore, err := trustmanager.NewKeyFileStore(trust.GetTrustDirectory(), freshPassRetGetter())
8484
if err != nil {
@@ -87,7 +87,7 @@ func validateAndGenerateKey(streams command.Streams, keyName string, workingDir
8787

8888
pubPEM, err := generateKeyAndOutputPubPEM(keyName, privKeyFileStore)
8989
if err != nil {
90-
fmt.Fprint(streams.Out(), err.Error())
90+
_, _ = fmt.Fprint(streams.Out(), err)
9191
return errors.Wrapf(err, "failed to generate key for %s", keyName)
9292
}
9393

@@ -96,7 +96,7 @@ func validateAndGenerateKey(streams command.Streams, keyName string, workingDir
9696
if err != nil {
9797
return err
9898
}
99-
fmt.Fprintf(streams.Out(), "Successfully generated and loaded private key. Corresponding public key available: %s\n", writtenPubFile)
99+
_, _ = fmt.Fprintln(streams.Out(), "Successfully generated and loaded private key. Corresponding public key available:", writtenPubFile)
100100

101101
return nil
102102
}

cli/command/trust/key_load.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func loadPrivKey(streams command.Streams, keyPath string, options keyLoadOptions
5454
}
5555
privKeyImporters := []trustmanager.Importer{keyFileStore}
5656

57-
fmt.Fprintf(streams.Out(), "Loading key from \"%s\"...\n", keyPath)
57+
_, _ = fmt.Fprintf(streams.Out(), "Loading key from \"%s\"...\n", keyPath)
5858

5959
// Always use a fresh passphrase retriever for each import
6060
passRet := trust.GetPassphraseRetriever(streams.In(), streams.Out())
@@ -65,7 +65,7 @@ func loadPrivKey(streams command.Streams, keyPath string, options keyLoadOptions
6565
if err := loadPrivKeyBytesToStore(keyBytes, privKeyImporters, keyPath, options.keyName, passRet); err != nil {
6666
return errors.Wrapf(err, "error importing key from %s", keyPath)
6767
}
68-
fmt.Fprintf(streams.Out(), "Successfully imported key from %s\n", keyPath)
68+
_, _ = fmt.Fprintln(streams.Out(), "Successfully imported key from", keyPath)
6969
return nil
7070
}
7171

cli/command/trust/sign.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func runSignImage(ctx context.Context, dockerCLI command.Cli, options signOption
7575
return trust.NotaryError(imgRefAndAuth.Reference().Name(), err)
7676
}
7777

78-
fmt.Fprintf(dockerCLI.Out(), "Created signer: %s\n", imgRefAndAuth.AuthConfig().Username)
79-
fmt.Fprintf(dockerCLI.Out(), "Finished initializing signed repository for %s\n", imageName)
78+
_, _ = fmt.Fprintln(dockerCLI.Out(), "Created signer:", imgRefAndAuth.AuthConfig().Username)
79+
_, _ = fmt.Fprintln(dockerCLI.Out(), "Finished initializing signed repository for", imageName)
8080
default:
8181
return trust.NotaryError(imgRefAndAuth.RepoInfo().Name.Name(), err)
8282
}
@@ -91,18 +91,17 @@ func runSignImage(ctx context.Context, dockerCLI command.Cli, options signOption
9191
if err := checkLocalImageExistence(ctx, dockerCLI.Client(), imageName); err != nil {
9292
return err
9393
}
94-
fmt.Fprintf(dockerCLI.Err(), "Signing and pushing trust data for local image %s, may overwrite remote trust data\n", imageName)
94+
_, _ = fmt.Fprintf(dockerCLI.Err(), "Signing and pushing trust data for local image %s, may overwrite remote trust data\n", imageName)
9595

9696
authConfig := command.ResolveAuthConfig(dockerCLI.ConfigFile(), imgRefAndAuth.RepoInfo().Index)
9797
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
9898
if err != nil {
9999
return err
100100
}
101-
options := imagetypes.PushOptions{
101+
return image.TrustedPush(ctx, dockerCLI, imgRefAndAuth.RepoInfo(), imgRefAndAuth.Reference(), *imgRefAndAuth.AuthConfig(), imagetypes.PushOptions{
102102
RegistryAuth: encodedAuth,
103103
PrivilegeFunc: requestPrivilege,
104-
}
105-
return image.TrustedPush(ctx, dockerCLI, imgRefAndAuth.RepoInfo(), imgRefAndAuth.Reference(), *imgRefAndAuth.AuthConfig(), options)
104+
})
106105
default:
107106
return err
108107
}
@@ -112,7 +111,7 @@ func runSignImage(ctx context.Context, dockerCLI command.Cli, options signOption
112111

113112
func signAndPublishToTarget(out io.Writer, imgRefAndAuth trust.ImageRefAndAuth, notaryRepo notaryclient.Repository, target notaryclient.Target) error {
114113
tag := imgRefAndAuth.Tag()
115-
fmt.Fprintf(out, "Signing and pushing trust metadata for %s\n", imgRefAndAuth.Name())
114+
_, _ = fmt.Fprintln(out, "Signing and pushing trust metadata for", imgRefAndAuth.Name())
116115
existingSigInfo, err := getExistingSignatureInfoForReleasedTag(notaryRepo, tag)
117116
if err != nil {
118117
return err
@@ -125,7 +124,7 @@ func signAndPublishToTarget(out io.Writer, imgRefAndAuth trust.ImageRefAndAuth,
125124
if err != nil {
126125
return errors.Wrapf(err, "failed to sign %s:%s", imgRefAndAuth.RepoInfo().Name.Name(), tag)
127126
}
128-
fmt.Fprintf(out, "Successfully signed %s:%s\n", imgRefAndAuth.RepoInfo().Name.Name(), tag)
127+
_, _ = fmt.Fprintf(out, "Successfully signed %s:%s\n", imgRefAndAuth.RepoInfo().Name.Name(), tag)
129128
return nil
130129
}
131130

@@ -188,7 +187,7 @@ func getExistingSignatureInfoForReleasedTag(notaryRepo notaryclient.Repository,
188187
func prettyPrintExistingSignatureInfo(out io.Writer, existingSigInfo trustTagRow) {
189188
sort.Strings(existingSigInfo.Signers)
190189
joinedSigners := strings.Join(existingSigInfo.Signers, ", ")
191-
fmt.Fprintf(out, "Existing signatures for tag %s digest %s from:\n%s\n", existingSigInfo.SignedTag, existingSigInfo.Digest, joinedSigners)
190+
_, _ = fmt.Fprintf(out, "Existing signatures for tag %s digest %s from:\n%s\n", existingSigInfo.SignedTag, existingSigInfo.Digest, joinedSigners)
192191
}
193192

194193
func initNotaryRepoWithSigners(notaryRepo notaryclient.Repository, newSigner data.RoleName) error {

cli/command/trust/signer_add.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ func addSigner(ctx context.Context, dockerCLI command.Cli, options signerAddOpti
6565
}
6666
var errRepos []string
6767
for _, repoName := range options.repos {
68-
fmt.Fprintf(dockerCLI.Out(), "Adding signer \"%s\" to %s...\n", signerName, repoName)
68+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Adding signer \"%s\" to %s...\n", signerName, repoName)
6969
if err := addSignerToRepo(ctx, dockerCLI, signerName, repoName, signerPubKeys); err != nil {
70-
fmt.Fprintln(dockerCLI.Err(), err.Error()+"\n")
70+
_, _ = fmt.Fprintln(dockerCLI.Err(), err.Error()+"\n")
7171
errRepos = append(errRepos, repoName)
7272
} else {
73-
fmt.Fprintf(dockerCLI.Out(), "Successfully added signer: %s to %s\n\n", signerName, repoName)
73+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Successfully added signer: %s to %s\n\n", signerName, repoName)
7474
}
7575
}
7676
if len(errRepos) > 0 {
@@ -93,11 +93,11 @@ func addSignerToRepo(ctx context.Context, dockerCLI command.Cli, signerName stri
9393
if _, err = notaryRepo.ListTargets(); err != nil {
9494
switch err.(type) {
9595
case client.ErrRepoNotInitialized, client.ErrRepositoryNotExist:
96-
fmt.Fprintf(dockerCLI.Out(), "Initializing signed repository for %s...\n", repoName)
96+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Initializing signed repository for %s...\n", repoName)
9797
if err := getOrGenerateRootKeyAndInitRepo(notaryRepo); err != nil {
9898
return trust.NotaryError(repoName, err)
9999
}
100-
fmt.Fprintf(dockerCLI.Out(), "Successfully initialized %q\n", repoName)
100+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Successfully initialized %q\n", repoName)
101101
default:
102102
return trust.NotaryError(repoName, err)
103103
}

cli/command/trust/signer_remove.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ func newSignerRemoveCommand(dockerCli command.Cli) *cobra.Command {
4141
func removeSigner(ctx context.Context, dockerCLI command.Cli, options signerRemoveOptions) error {
4242
var errRepos []string
4343
for _, repo := range options.repos {
44-
fmt.Fprintf(dockerCLI.Out(), "Removing signer \"%s\" from %s...\n", options.signer, repo)
44+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Removing signer \"%s\" from %s...\n", options.signer, repo)
4545
if _, err := removeSingleSigner(ctx, dockerCLI, repo, options.signer, options.forceYes); err != nil {
46-
fmt.Fprintln(dockerCLI.Err(), err.Error()+"\n")
46+
_, _ = fmt.Fprintln(dockerCLI.Err(), err.Error()+"\n")
4747
errRepos = append(errRepos, repo)
4848
}
4949
}
@@ -150,7 +150,7 @@ func removeSingleSigner(ctx context.Context, dockerCLI command.Cli, repoName, si
150150
return false, err
151151
}
152152

153-
fmt.Fprintf(dockerCLI.Out(), "Successfully removed %s from %s\n\n", signerName, repoName)
153+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Successfully removed %s from %s\n\n", signerName, repoName)
154154

155155
return true, nil
156156
}

0 commit comments

Comments
 (0)