github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/trust/inspect_pretty.go (about) 1 package trust 2 3 import ( 4 "context" 5 "fmt" 6 "io" 7 "sort" 8 9 "github.com/docker/cli/cli/command" 10 "github.com/docker/cli/cli/command/formatter" 11 "github.com/fvbommel/sortorder" 12 "github.com/theupdateframework/notary/client" 13 ) 14 15 func prettyPrintTrustInfo(ctx context.Context, cli command.Cli, remote string) error { 16 signatureRows, adminRolesWithSigs, delegationRoles, err := lookupTrustInfo(ctx, cli, remote) 17 if err != nil { 18 return err 19 } 20 21 if len(signatureRows) > 0 { 22 fmt.Fprintf(cli.Out(), "\nSignatures for %s\n\n", remote) 23 24 if err := printSignatures(cli.Out(), signatureRows); err != nil { 25 return err 26 } 27 } else { 28 fmt.Fprintf(cli.Out(), "\nNo signatures for %s\n\n", remote) 29 } 30 signerRoleToKeyIDs := getDelegationRoleToKeyMap(delegationRoles) 31 32 // If we do not have additional signers, do not display 33 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 { 36 return err 37 } 38 } 39 40 // 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) 43 return nil 44 } 45 46 func printSortedAdminKeys(out io.Writer, adminRoles []client.RoleWithSignatures) { 47 sort.Slice(adminRoles, func(i, j int) bool { return adminRoles[i].Name > adminRoles[j].Name }) 48 for _, adminRole := range adminRoles { 49 if formattedAdminRole := formatAdminRole(adminRole); formattedAdminRole != "" { 50 fmt.Fprintf(out, " %s", formattedAdminRole) 51 } 52 } 53 } 54 55 // pretty print with ordered rows 56 func printSignatures(out io.Writer, signatureRows []trustTagRow) error { 57 trustTagCtx := formatter.Context{ 58 Output: out, 59 Format: NewTrustTagFormat(), 60 } 61 // convert the formatted type before printing 62 formattedTags := []SignedTagInfo{} 63 for _, sigRow := range signatureRows { 64 formattedSigners := sigRow.Signers 65 if len(formattedSigners) == 0 { 66 formattedSigners = append(formattedSigners, fmt.Sprintf("(%s)", releasedRoleName)) 67 } 68 formattedTags = append(formattedTags, SignedTagInfo{ 69 Name: sigRow.SignedTag, 70 Digest: sigRow.Digest, 71 Signers: formattedSigners, 72 }) 73 } 74 return TagWrite(trustTagCtx, formattedTags) 75 } 76 77 func printSignerInfo(out io.Writer, roleToKeyIDs map[string][]string) error { 78 signerInfoCtx := formatter.Context{ 79 Output: out, 80 Format: NewSignerInfoFormat(), 81 Trunc: true, 82 } 83 formattedSignerInfo := []SignerInfo{} 84 for name, keyIDs := range roleToKeyIDs { 85 formattedSignerInfo = append(formattedSignerInfo, SignerInfo{ 86 Name: name, 87 Keys: keyIDs, 88 }) 89 } 90 sort.Slice(formattedSignerInfo, func(i, j int) bool { 91 return sortorder.NaturalLess(formattedSignerInfo[i].Name, formattedSignerInfo[j].Name) 92 }) 93 return SignerInfoWrite(signerInfoCtx, formattedSignerInfo) 94 }