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