-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathformatter.go
More file actions
164 lines (144 loc) · 4.78 KB
/
formatter.go
File metadata and controls
164 lines (144 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package trust
import (
"sort"
"strings"
"github.com/docker/cli/cli/command/formatter"
)
const (
defaultTrustTagTableFormat = "table {{.SignedTag}}\t{{.Digest}}\t{{.Signers}}"
signedTagNameHeader = "SIGNED TAG"
trustedDigestHeader = "DIGEST"
signersHeader = "SIGNERS"
defaultSignerInfoTableFormat = "table {{.Signer}}\t{{.Keys}}"
signerNameHeader = "SIGNER"
keysHeader = "KEYS"
)
// SignedTagInfo represents all formatted information needed to describe a signed tag:
// Name: name of the signed tag
// Digest: hex encoded digest of the contents
// Signers: list of entities who signed the tag
//
// Deprecated: this type was only used internally and will be removed in the next release.
type SignedTagInfo = signedTagInfo
// signedTagInfo represents all formatted information needed to describe a signed tag:
// Name: name of the signed tag
// Digest: hex encoded digest of the contents
// Signers: list of entities who signed the tag
type signedTagInfo struct {
Name string
Digest string
Signers []string
}
// SignerInfo represents all formatted information needed to describe a signer:
// Name: name of the signer role
// Keys: the keys associated with the signer
//
// Deprecated: this type was only used internally and will be removed in the next release.
type SignerInfo = signerInfo
// signerInfo represents all formatted information needed to describe a signer:
// Name: name of the signer role
// Keys: the keys associated with the signer
type signerInfo struct {
Name string
Keys []string
}
// NewTrustTagFormat returns a Format for rendering using a trusted tag Context
//
// Deprecated: this function was only used internally and will be removed in the next release.
func NewTrustTagFormat() formatter.Format {
return defaultTrustTagTableFormat
}
// NewSignerInfoFormat returns a Format for rendering a signer role info Context
//
// Deprecated: this function was only used internally and will be removed in the next release.
func NewSignerInfoFormat() formatter.Format {
return defaultSignerInfoTableFormat
}
// TagWrite writes the context
//
// Deprecated: this function was only used internally and will be removed in the next release.
func TagWrite(fmtCtx formatter.Context, signedTagInfoList []signedTagInfo) error {
return tagWrite(fmtCtx, signedTagInfoList)
}
// tagWrite writes the context
func tagWrite(fmtCtx formatter.Context, signedTagInfoList []signedTagInfo) error {
render := func(format func(subContext formatter.SubContext) error) error {
for _, signedTag := range signedTagInfoList {
if err := format(&trustTagContext{s: signedTag}); err != nil {
return err
}
}
return nil
}
trustTagCtx := trustTagContext{}
trustTagCtx.Header = formatter.SubHeaderContext{
"SignedTag": signedTagNameHeader,
"Digest": trustedDigestHeader,
"Signers": signersHeader,
}
return fmtCtx.Write(&trustTagCtx, render)
}
type trustTagContext struct {
formatter.HeaderContext
s signedTagInfo
}
// SignedTag returns the name of the signed tag
func (c *trustTagContext) SignedTag() string {
return c.s.Name
}
// Digest returns the hex encoded digest associated with this signed tag
func (c *trustTagContext) Digest() string {
return c.s.Digest
}
// Signers returns the sorted list of entities who signed this tag
func (c *trustTagContext) Signers() string {
sort.Strings(c.s.Signers)
return strings.Join(c.s.Signers, ", ")
}
// SignerInfoWrite writes the context
//
// Deprecated: this function was only used internally and will be removed in the next release.
func SignerInfoWrite(fmtCtx formatter.Context, signerInfoList []signerInfo) error {
return signerInfoWrite(fmtCtx, signerInfoList)
}
// signerInfoWrite writes the context.
func signerInfoWrite(fmtCtx formatter.Context, signerInfoList []signerInfo) error {
render := func(format func(subContext formatter.SubContext) error) error {
for _, signerInfo := range signerInfoList {
if err := format(&signerInfoContext{
trunc: fmtCtx.Trunc,
s: signerInfo,
}); err != nil {
return err
}
}
return nil
}
signerInfoCtx := signerInfoContext{}
signerInfoCtx.Header = formatter.SubHeaderContext{
"Signer": signerNameHeader,
"Keys": keysHeader,
}
return fmtCtx.Write(&signerInfoCtx, render)
}
type signerInfoContext struct {
formatter.HeaderContext
trunc bool
s signerInfo
}
// Keys returns the sorted list of keys associated with the signer
func (c *signerInfoContext) Keys() string {
sort.Strings(c.s.Keys)
truncatedKeys := []string{}
if c.trunc {
for _, keyID := range c.s.Keys {
truncatedKeys = append(truncatedKeys, formatter.TruncateID(keyID))
}
return strings.Join(truncatedKeys, ", ")
}
return strings.Join(c.s.Keys, ", ")
}
// Signer returns the name of the signer
func (c *signerInfoContext) Signer() string {
return c.s.Name
}