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>
This commit is contained in:
@ -53,7 +53,7 @@ func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions)
|
||||
|
||||
// Additional separator between the inspection output of each image
|
||||
if index < len(opts.remotes)-1 {
|
||||
fmt.Fprint(dockerCLI.Out(), "\n\n")
|
||||
_, _ = fmt.Fprint(dockerCLI.Out(), "\n\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -12,34 +12,34 @@ import (
|
||||
"github.com/theupdateframework/notary/client"
|
||||
)
|
||||
|
||||
func prettyPrintTrustInfo(ctx context.Context, cli command.Cli, remote string) error {
|
||||
signatureRows, adminRolesWithSigs, delegationRoles, err := lookupTrustInfo(ctx, cli, remote)
|
||||
func prettyPrintTrustInfo(ctx context.Context, dockerCLI command.Cli, remote string) error {
|
||||
signatureRows, adminRolesWithSigs, delegationRoles, err := lookupTrustInfo(ctx, dockerCLI, remote)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(signatureRows) > 0 {
|
||||
fmt.Fprintf(cli.Out(), "\nSignatures for %s\n\n", remote)
|
||||
_, _ = fmt.Fprintf(dockerCLI.Out(), "\nSignatures for %s\n\n", remote)
|
||||
|
||||
if err := printSignatures(cli.Out(), signatureRows); err != nil {
|
||||
if err := printSignatures(dockerCLI.Out(), signatureRows); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(cli.Out(), "\nNo signatures for %s\n\n", remote)
|
||||
_, _ = fmt.Fprintf(dockerCLI.Out(), "\nNo signatures for %s\n\n", remote)
|
||||
}
|
||||
signerRoleToKeyIDs := getDelegationRoleToKeyMap(delegationRoles)
|
||||
|
||||
// If we do not have additional signers, do not display
|
||||
if len(signerRoleToKeyIDs) > 0 {
|
||||
fmt.Fprintf(cli.Out(), "\nList of signers and their keys for %s\n\n", remote)
|
||||
if err := printSignerInfo(cli.Out(), signerRoleToKeyIDs); err != nil {
|
||||
_, _ = fmt.Fprintf(dockerCLI.Out(), "\nList of signers and their keys for %s\n\n", remote)
|
||||
if err := printSignerInfo(dockerCLI.Out(), signerRoleToKeyIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// This will always have the root and targets information
|
||||
fmt.Fprintf(cli.Out(), "\nAdministrative keys for %s\n\n", remote)
|
||||
printSortedAdminKeys(cli.Out(), adminRolesWithSigs)
|
||||
_, _ = fmt.Fprintf(dockerCLI.Out(), "\nAdministrative keys for %s\n\n", remote)
|
||||
printSortedAdminKeys(dockerCLI.Out(), adminRolesWithSigs)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ func printSortedAdminKeys(out io.Writer, adminRoles []client.RoleWithSignatures)
|
||||
sort.Slice(adminRoles, func(i, j int) bool { return adminRoles[i].Name > adminRoles[j].Name })
|
||||
for _, adminRole := range adminRoles {
|
||||
if formattedAdminRole := formatAdminRole(adminRole); formattedAdminRole != "" {
|
||||
fmt.Fprintf(out, " %s", formattedAdminRole)
|
||||
_, _ = fmt.Fprintf(out, " %s", formattedAdminRole)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,7 +78,7 @@ func validateAndGenerateKey(streams command.Streams, keyName string, workingDir
|
||||
if err := validateKeyArgs(keyName, workingDir); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(streams.Out(), "Generating key for %s...\n", keyName)
|
||||
_, _ = fmt.Fprintf(streams.Out(), "Generating key for %s...\n", keyName)
|
||||
// Automatically load the private key to local storage for use
|
||||
privKeyFileStore, err := trustmanager.NewKeyFileStore(trust.GetTrustDirectory(), freshPassRetGetter())
|
||||
if err != nil {
|
||||
@ -87,7 +87,7 @@ func validateAndGenerateKey(streams command.Streams, keyName string, workingDir
|
||||
|
||||
pubPEM, err := generateKeyAndOutputPubPEM(keyName, privKeyFileStore)
|
||||
if err != nil {
|
||||
fmt.Fprint(streams.Out(), err.Error())
|
||||
_, _ = fmt.Fprint(streams.Out(), err)
|
||||
return errors.Wrapf(err, "failed to generate key for %s", keyName)
|
||||
}
|
||||
|
||||
@ -96,7 +96,7 @@ func validateAndGenerateKey(streams command.Streams, keyName string, workingDir
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(streams.Out(), "Successfully generated and loaded private key. Corresponding public key available: %s\n", writtenPubFile)
|
||||
_, _ = fmt.Fprintln(streams.Out(), "Successfully generated and loaded private key. Corresponding public key available:", writtenPubFile)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -54,7 +54,7 @@ func loadPrivKey(streams command.Streams, keyPath string, options keyLoadOptions
|
||||
}
|
||||
privKeyImporters := []trustmanager.Importer{keyFileStore}
|
||||
|
||||
fmt.Fprintf(streams.Out(), "Loading key from \"%s\"...\n", keyPath)
|
||||
_, _ = fmt.Fprintf(streams.Out(), "Loading key from \"%s\"...\n", keyPath)
|
||||
|
||||
// Always use a fresh passphrase retriever for each import
|
||||
passRet := trust.GetPassphraseRetriever(streams.In(), streams.Out())
|
||||
@ -65,7 +65,7 @@ func loadPrivKey(streams command.Streams, keyPath string, options keyLoadOptions
|
||||
if err := loadPrivKeyBytesToStore(keyBytes, privKeyImporters, keyPath, options.keyName, passRet); err != nil {
|
||||
return errors.Wrapf(err, "error importing key from %s", keyPath)
|
||||
}
|
||||
fmt.Fprintf(streams.Out(), "Successfully imported key from %s\n", keyPath)
|
||||
_, _ = fmt.Fprintln(streams.Out(), "Successfully imported key from", keyPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@ -75,8 +75,8 @@ func runSignImage(ctx context.Context, dockerCLI command.Cli, options signOption
|
||||
return trust.NotaryError(imgRefAndAuth.Reference().Name(), err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(dockerCLI.Out(), "Created signer: %s\n", imgRefAndAuth.AuthConfig().Username)
|
||||
fmt.Fprintf(dockerCLI.Out(), "Finished initializing signed repository for %s\n", imageName)
|
||||
_, _ = fmt.Fprintln(dockerCLI.Out(), "Created signer:", imgRefAndAuth.AuthConfig().Username)
|
||||
_, _ = fmt.Fprintln(dockerCLI.Out(), "Finished initializing signed repository for", imageName)
|
||||
default:
|
||||
return trust.NotaryError(imgRefAndAuth.RepoInfo().Name.Name(), err)
|
||||
}
|
||||
@ -91,18 +91,17 @@ func runSignImage(ctx context.Context, dockerCLI command.Cli, options signOption
|
||||
if err := checkLocalImageExistence(ctx, dockerCLI.Client(), imageName); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(dockerCLI.Err(), "Signing and pushing trust data for local image %s, may overwrite remote trust data\n", imageName)
|
||||
_, _ = fmt.Fprintf(dockerCLI.Err(), "Signing and pushing trust data for local image %s, may overwrite remote trust data\n", imageName)
|
||||
|
||||
authConfig := command.ResolveAuthConfig(dockerCLI.ConfigFile(), imgRefAndAuth.RepoInfo().Index)
|
||||
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options := imagetypes.PushOptions{
|
||||
return image.TrustedPush(ctx, dockerCLI, imgRefAndAuth.RepoInfo(), imgRefAndAuth.Reference(), *imgRefAndAuth.AuthConfig(), imagetypes.PushOptions{
|
||||
RegistryAuth: encodedAuth,
|
||||
PrivilegeFunc: requestPrivilege,
|
||||
}
|
||||
return image.TrustedPush(ctx, dockerCLI, imgRefAndAuth.RepoInfo(), imgRefAndAuth.Reference(), *imgRefAndAuth.AuthConfig(), options)
|
||||
})
|
||||
default:
|
||||
return err
|
||||
}
|
||||
@ -112,7 +111,7 @@ func runSignImage(ctx context.Context, dockerCLI command.Cli, options signOption
|
||||
|
||||
func signAndPublishToTarget(out io.Writer, imgRefAndAuth trust.ImageRefAndAuth, notaryRepo notaryclient.Repository, target notaryclient.Target) error {
|
||||
tag := imgRefAndAuth.Tag()
|
||||
fmt.Fprintf(out, "Signing and pushing trust metadata for %s\n", imgRefAndAuth.Name())
|
||||
_, _ = fmt.Fprintln(out, "Signing and pushing trust metadata for", imgRefAndAuth.Name())
|
||||
existingSigInfo, err := getExistingSignatureInfoForReleasedTag(notaryRepo, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -125,7 +124,7 @@ func signAndPublishToTarget(out io.Writer, imgRefAndAuth trust.ImageRefAndAuth,
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to sign %s:%s", imgRefAndAuth.RepoInfo().Name.Name(), tag)
|
||||
}
|
||||
fmt.Fprintf(out, "Successfully signed %s:%s\n", imgRefAndAuth.RepoInfo().Name.Name(), tag)
|
||||
_, _ = fmt.Fprintf(out, "Successfully signed %s:%s\n", imgRefAndAuth.RepoInfo().Name.Name(), tag)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -188,7 +187,7 @@ func getExistingSignatureInfoForReleasedTag(notaryRepo notaryclient.Repository,
|
||||
func prettyPrintExistingSignatureInfo(out io.Writer, existingSigInfo trustTagRow) {
|
||||
sort.Strings(existingSigInfo.Signers)
|
||||
joinedSigners := strings.Join(existingSigInfo.Signers, ", ")
|
||||
fmt.Fprintf(out, "Existing signatures for tag %s digest %s from:\n%s\n", existingSigInfo.SignedTag, existingSigInfo.Digest, joinedSigners)
|
||||
_, _ = fmt.Fprintf(out, "Existing signatures for tag %s digest %s from:\n%s\n", existingSigInfo.SignedTag, existingSigInfo.Digest, joinedSigners)
|
||||
}
|
||||
|
||||
func initNotaryRepoWithSigners(notaryRepo notaryclient.Repository, newSigner data.RoleName) error {
|
||||
|
||||
@ -65,12 +65,12 @@ func addSigner(ctx context.Context, dockerCLI command.Cli, options signerAddOpti
|
||||
}
|
||||
var errRepos []string
|
||||
for _, repoName := range options.repos {
|
||||
fmt.Fprintf(dockerCLI.Out(), "Adding signer \"%s\" to %s...\n", signerName, repoName)
|
||||
_, _ = fmt.Fprintf(dockerCLI.Out(), "Adding signer \"%s\" to %s...\n", signerName, repoName)
|
||||
if err := addSignerToRepo(ctx, dockerCLI, signerName, repoName, signerPubKeys); err != nil {
|
||||
fmt.Fprintln(dockerCLI.Err(), err.Error()+"\n")
|
||||
_, _ = fmt.Fprintln(dockerCLI.Err(), err.Error()+"\n")
|
||||
errRepos = append(errRepos, repoName)
|
||||
} else {
|
||||
fmt.Fprintf(dockerCLI.Out(), "Successfully added signer: %s to %s\n\n", signerName, repoName)
|
||||
_, _ = fmt.Fprintf(dockerCLI.Out(), "Successfully added signer: %s to %s\n\n", signerName, repoName)
|
||||
}
|
||||
}
|
||||
if len(errRepos) > 0 {
|
||||
@ -93,11 +93,11 @@ func addSignerToRepo(ctx context.Context, dockerCLI command.Cli, signerName stri
|
||||
if _, err = notaryRepo.ListTargets(); err != nil {
|
||||
switch err.(type) {
|
||||
case client.ErrRepoNotInitialized, client.ErrRepositoryNotExist:
|
||||
fmt.Fprintf(dockerCLI.Out(), "Initializing signed repository for %s...\n", repoName)
|
||||
_, _ = fmt.Fprintf(dockerCLI.Out(), "Initializing signed repository for %s...\n", repoName)
|
||||
if err := getOrGenerateRootKeyAndInitRepo(notaryRepo); err != nil {
|
||||
return trust.NotaryError(repoName, err)
|
||||
}
|
||||
fmt.Fprintf(dockerCLI.Out(), "Successfully initialized %q\n", repoName)
|
||||
_, _ = fmt.Fprintf(dockerCLI.Out(), "Successfully initialized %q\n", repoName)
|
||||
default:
|
||||
return trust.NotaryError(repoName, err)
|
||||
}
|
||||
|
||||
@ -41,9 +41,9 @@ func newSignerRemoveCommand(dockerCli command.Cli) *cobra.Command {
|
||||
func removeSigner(ctx context.Context, dockerCLI command.Cli, options signerRemoveOptions) error {
|
||||
var errRepos []string
|
||||
for _, repo := range options.repos {
|
||||
fmt.Fprintf(dockerCLI.Out(), "Removing signer \"%s\" from %s...\n", options.signer, repo)
|
||||
_, _ = fmt.Fprintf(dockerCLI.Out(), "Removing signer \"%s\" from %s...\n", options.signer, repo)
|
||||
if _, err := removeSingleSigner(ctx, dockerCLI, repo, options.signer, options.forceYes); err != nil {
|
||||
fmt.Fprintln(dockerCLI.Err(), err.Error()+"\n")
|
||||
_, _ = fmt.Fprintln(dockerCLI.Err(), err.Error()+"\n")
|
||||
errRepos = append(errRepos, repo)
|
||||
}
|
||||
}
|
||||
@ -150,7 +150,7 @@ func removeSingleSigner(ctx context.Context, dockerCLI command.Cli, repoName, si
|
||||
return false, err
|
||||
}
|
||||
|
||||
fmt.Fprintf(dockerCLI.Out(), "Successfully removed %s from %s\n\n", signerName, repoName)
|
||||
_, _ = fmt.Fprintf(dockerCLI.Out(), "Successfully removed %s from %s\n\n", signerName, repoName)
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user