github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/command/trust/formatter.go (about) 1 package trust 2 3 import ( 4 "sort" 5 "strings" 6 7 "github.com/docker/docker/pkg/stringid" 8 "github.com/khulnasoft/cli/cli/command/formatter" 9 ) 10 11 const ( 12 defaultTrustTagTableFormat = "table {{.SignedTag}}\t{{.Digest}}\t{{.Signers}}" 13 signedTagNameHeader = "SIGNED TAG" 14 trustedDigestHeader = "DIGEST" 15 signersHeader = "SIGNERS" 16 defaultSignerInfoTableFormat = "table {{.Signer}}\t{{.Keys}}" 17 signerNameHeader = "SIGNER" 18 keysHeader = "KEYS" 19 ) 20 21 // SignedTagInfo represents all formatted information needed to describe a signed tag: 22 // Name: name of the signed tag 23 // Digest: hex encoded digest of the contents 24 // Signers: list of entities who signed the tag 25 type SignedTagInfo struct { 26 Name string 27 Digest string 28 Signers []string 29 } 30 31 // SignerInfo represents all formatted information needed to describe a signer: 32 // Name: name of the signer role 33 // Keys: the keys associated with the signer 34 type SignerInfo struct { 35 Name string 36 Keys []string 37 } 38 39 // NewTrustTagFormat returns a Format for rendering using a trusted tag Context 40 func NewTrustTagFormat() formatter.Format { 41 return defaultTrustTagTableFormat 42 } 43 44 // NewSignerInfoFormat returns a Format for rendering a signer role info Context 45 func NewSignerInfoFormat() formatter.Format { 46 return defaultSignerInfoTableFormat 47 } 48 49 // TagWrite writes the context 50 func TagWrite(ctx formatter.Context, signedTagInfoList []SignedTagInfo) error { 51 render := func(format func(subContext formatter.SubContext) error) error { 52 for _, signedTag := range signedTagInfoList { 53 if err := format(&trustTagContext{s: signedTag}); err != nil { 54 return err 55 } 56 } 57 return nil 58 } 59 trustTagCtx := trustTagContext{} 60 trustTagCtx.Header = formatter.SubHeaderContext{ 61 "SignedTag": signedTagNameHeader, 62 "Digest": trustedDigestHeader, 63 "Signers": signersHeader, 64 } 65 return ctx.Write(&trustTagCtx, render) 66 } 67 68 type trustTagContext struct { 69 formatter.HeaderContext 70 s SignedTagInfo 71 } 72 73 // SignedTag returns the name of the signed tag 74 func (c *trustTagContext) SignedTag() string { 75 return c.s.Name 76 } 77 78 // Digest returns the hex encoded digest associated with this signed tag 79 func (c *trustTagContext) Digest() string { 80 return c.s.Digest 81 } 82 83 // Signers returns the sorted list of entities who signed this tag 84 func (c *trustTagContext) Signers() string { 85 sort.Strings(c.s.Signers) 86 return strings.Join(c.s.Signers, ", ") 87 } 88 89 // SignerInfoWrite writes the context 90 func SignerInfoWrite(ctx formatter.Context, signerInfoList []SignerInfo) error { 91 render := func(format func(subContext formatter.SubContext) error) error { 92 for _, signerInfo := range signerInfoList { 93 if err := format(&signerInfoContext{ 94 trunc: ctx.Trunc, 95 s: signerInfo, 96 }); err != nil { 97 return err 98 } 99 } 100 return nil 101 } 102 signerInfoCtx := signerInfoContext{} 103 signerInfoCtx.Header = formatter.SubHeaderContext{ 104 "Signer": signerNameHeader, 105 "Keys": keysHeader, 106 } 107 return ctx.Write(&signerInfoCtx, render) 108 } 109 110 type signerInfoContext struct { 111 formatter.HeaderContext 112 trunc bool 113 s SignerInfo 114 } 115 116 // Keys returns the sorted list of keys associated with the signer 117 func (c *signerInfoContext) Keys() string { 118 sort.Strings(c.s.Keys) 119 truncatedKeys := []string{} 120 if c.trunc { 121 for _, keyID := range c.s.Keys { 122 truncatedKeys = append(truncatedKeys, stringid.TruncateID(keyID)) 123 } 124 return strings.Join(truncatedKeys, ", ") 125 } 126 return strings.Join(c.s.Keys, ", ") 127 } 128 129 // Signer returns the name of the signer 130 func (c *signerInfoContext) Signer() string { 131 return c.s.Name 132 }