Skip to content

Commit 4133271

Browse files
committed
cli/command/trust: deprecate formatting-related functions and types
It's part of the presentation logic of the cli, and only used internally. We can consider providing utilities for these, but better as part of separate packages. This deprecates the following types and functions: - `SignedTagInfo` - `SignerInfo` - `NewTrustTagFormat` - `NewSignerInfoFormat` - `TagWrite` - `SignerInfoWrite` Signed-off-by: Sebastiaan van Stijn <[email protected]> (cherry picked from commit 95c9b1b) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 0e17907 commit 4133271

File tree

3 files changed

+69
-36
lines changed

3 files changed

+69
-36
lines changed

cli/command/trust/formatter.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ const (
2121
// Name: name of the signed tag
2222
// Digest: hex encoded digest of the contents
2323
// Signers: list of entities who signed the tag
24-
type SignedTagInfo struct {
24+
//
25+
// Deprecated: this type was only used internally and will be removed in the next release.
26+
type SignedTagInfo = signedTagInfo
27+
28+
// signedTagInfo represents all formatted information needed to describe a signed tag:
29+
// Name: name of the signed tag
30+
// Digest: hex encoded digest of the contents
31+
// Signers: list of entities who signed the tag
32+
type signedTagInfo struct {
2533
Name string
2634
Digest string
2735
Signers []string
@@ -30,23 +38,41 @@ type SignedTagInfo struct {
3038
// SignerInfo represents all formatted information needed to describe a signer:
3139
// Name: name of the signer role
3240
// Keys: the keys associated with the signer
33-
type SignerInfo struct {
41+
//
42+
// Deprecated: this type was only used internally and will be removed in the next release.
43+
type SignerInfo = signerInfo
44+
45+
// signerInfo represents all formatted information needed to describe a signer:
46+
// Name: name of the signer role
47+
// Keys: the keys associated with the signer
48+
type signerInfo struct {
3449
Name string
3550
Keys []string
3651
}
3752

3853
// NewTrustTagFormat returns a Format for rendering using a trusted tag Context
54+
//
55+
// Deprecated: this function was only used internally and will be removed in the next release.
3956
func NewTrustTagFormat() formatter.Format {
4057
return defaultTrustTagTableFormat
4158
}
4259

4360
// NewSignerInfoFormat returns a Format for rendering a signer role info Context
61+
//
62+
// Deprecated: this function was only used internally and will be removed in the next release.
4463
func NewSignerInfoFormat() formatter.Format {
4564
return defaultSignerInfoTableFormat
4665
}
4766

4867
// TagWrite writes the context
49-
func TagWrite(ctx formatter.Context, signedTagInfoList []SignedTagInfo) error {
68+
//
69+
// Deprecated: this function was only used internally and will be removed in the next release.
70+
func TagWrite(fmtCtx formatter.Context, signedTagInfoList []signedTagInfo) error {
71+
return tagWrite(fmtCtx, signedTagInfoList)
72+
}
73+
74+
// tagWrite writes the context
75+
func tagWrite(fmtCtx formatter.Context, signedTagInfoList []signedTagInfo) error {
5076
render := func(format func(subContext formatter.SubContext) error) error {
5177
for _, signedTag := range signedTagInfoList {
5278
if err := format(&trustTagContext{s: signedTag}); err != nil {
@@ -61,12 +87,12 @@ func TagWrite(ctx formatter.Context, signedTagInfoList []SignedTagInfo) error {
6187
"Digest": trustedDigestHeader,
6288
"Signers": signersHeader,
6389
}
64-
return ctx.Write(&trustTagCtx, render)
90+
return fmtCtx.Write(&trustTagCtx, render)
6591
}
6692

6793
type trustTagContext struct {
6894
formatter.HeaderContext
69-
s SignedTagInfo
95+
s signedTagInfo
7096
}
7197

7298
// SignedTag returns the name of the signed tag
@@ -86,11 +112,18 @@ func (c *trustTagContext) Signers() string {
86112
}
87113

88114
// SignerInfoWrite writes the context
89-
func SignerInfoWrite(ctx formatter.Context, signerInfoList []SignerInfo) error {
115+
//
116+
// Deprecated: this function was only used internally and will be removed in the next release.
117+
func SignerInfoWrite(fmtCtx formatter.Context, signerInfoList []signerInfo) error {
118+
return signerInfoWrite(fmtCtx, signerInfoList)
119+
}
120+
121+
// signerInfoWrite writes the context.
122+
func signerInfoWrite(fmtCtx formatter.Context, signerInfoList []signerInfo) error {
90123
render := func(format func(subContext formatter.SubContext) error) error {
91124
for _, signerInfo := range signerInfoList {
92125
if err := format(&signerInfoContext{
93-
trunc: ctx.Trunc,
126+
trunc: fmtCtx.Trunc,
94127
s: signerInfo,
95128
}); err != nil {
96129
return err
@@ -103,13 +136,13 @@ func SignerInfoWrite(ctx formatter.Context, signerInfoList []SignerInfo) error {
103136
"Signer": signerNameHeader,
104137
"Keys": keysHeader,
105138
}
106-
return ctx.Write(&signerInfoCtx, render)
139+
return fmtCtx.Write(&signerInfoCtx, render)
107140
}
108141

109142
type signerInfoContext struct {
110143
formatter.HeaderContext
111144
trunc bool
112-
s SignerInfo
145+
s signerInfo
113146
}
114147

115148
// Keys returns the sorted list of keys associated with the signer

cli/command/trust/formatter_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestTrustTag(t *testing.T) {
2323
}{
2424
{
2525
trustTagContext{
26-
s: SignedTagInfo{
26+
s: signedTagInfo{
2727
Name: trustedTag,
2828
Digest: digest,
2929
Signers: nil,
@@ -34,7 +34,7 @@ func TestTrustTag(t *testing.T) {
3434
},
3535
{
3636
trustTagContext{
37-
s: SignedTagInfo{
37+
s: signedTagInfo{
3838
Name: trustedTag,
3939
Digest: digest,
4040
Signers: nil,
@@ -46,7 +46,7 @@ func TestTrustTag(t *testing.T) {
4646
// Empty signers makes a row with empty string
4747
{
4848
trustTagContext{
49-
s: SignedTagInfo{
49+
s: signedTagInfo{
5050
Name: trustedTag,
5151
Digest: digest,
5252
Signers: nil,
@@ -57,7 +57,7 @@ func TestTrustTag(t *testing.T) {
5757
},
5858
{
5959
trustTagContext{
60-
s: SignedTagInfo{
60+
s: signedTagInfo{
6161
Name: trustedTag,
6262
Digest: digest,
6363
Signers: []string{"alice", "bob", "claire"},
@@ -69,7 +69,7 @@ func TestTrustTag(t *testing.T) {
6969
// alphabetic signing on Signers
7070
{
7171
trustTagContext{
72-
s: SignedTagInfo{
72+
s: signedTagInfo{
7373
Name: trustedTag,
7474
Digest: digest,
7575
Signers: []string{"claire", "bob", "alice"},
@@ -110,7 +110,7 @@ func TestTrustTagContextWrite(t *testing.T) {
110110
// Table Format
111111
{
112112
formatter.Context{
113-
Format: NewTrustTagFormat(),
113+
Format: defaultTrustTagTableFormat,
114114
},
115115
`SIGNED TAG DIGEST SIGNERS
116116
tag1 deadbeef alice
@@ -120,7 +120,7 @@ tag3 bbbbbbbb
120120
},
121121
}
122122

123-
signedTags := []SignedTagInfo{
123+
signedTags := []signedTagInfo{
124124
{Name: "tag1", Digest: "deadbeef", Signers: []string{"alice"}},
125125
{Name: "tag2", Digest: "aaaaaaaa", Signers: []string{"alice", "bob"}},
126126
{Name: "tag3", Digest: "bbbbbbbb", Signers: []string{}},
@@ -131,7 +131,7 @@ tag3 bbbbbbbb
131131
var out bytes.Buffer
132132
tc.context.Output = &out
133133

134-
if err := TagWrite(tc.context, signedTags); err != nil {
134+
if err := tagWrite(tc.context, signedTags); err != nil {
135135
assert.Error(t, err, tc.expected)
136136
} else {
137137
assert.Equal(t, out.String(), tc.expected)
@@ -140,24 +140,24 @@ tag3 bbbbbbbb
140140
}
141141
}
142142

143-
// With no trust data, the TagWrite will print an empty table:
143+
// With no trust data, the formatWrite will print an empty table:
144144
// it's up to the caller to decide whether or not to print this versus an error
145145
func TestTrustTagContextEmptyWrite(t *testing.T) {
146146
emptyCase := struct {
147147
context formatter.Context
148148
expected string
149149
}{
150150
formatter.Context{
151-
Format: NewTrustTagFormat(),
151+
Format: defaultTrustTagTableFormat,
152152
},
153153
`SIGNED TAG DIGEST SIGNERS
154154
`,
155155
}
156156

157-
emptySignedTags := []SignedTagInfo{}
157+
emptySignedTags := []signedTagInfo{}
158158
out := bytes.NewBufferString("")
159159
emptyCase.context.Output = out
160-
err := TagWrite(emptyCase.context, emptySignedTags)
160+
err := tagWrite(emptyCase.context, emptySignedTags)
161161
assert.NilError(t, err)
162162
assert.Check(t, is.Equal(emptyCase.expected, out.String()))
163163
}
@@ -168,15 +168,15 @@ func TestSignerInfoContextEmptyWrite(t *testing.T) {
168168
expected string
169169
}{
170170
formatter.Context{
171-
Format: NewSignerInfoFormat(),
171+
Format: defaultSignerInfoTableFormat,
172172
},
173173
`SIGNER KEYS
174174
`,
175175
}
176-
emptySignerInfo := []SignerInfo{}
176+
emptySignerInfo := []signerInfo{}
177177
out := bytes.NewBufferString("")
178178
emptyCase.context.Output = out
179-
err := SignerInfoWrite(emptyCase.context, emptySignerInfo)
179+
err := signerInfoWrite(emptyCase.context, emptySignerInfo)
180180
assert.NilError(t, err)
181181
assert.Check(t, is.Equal(emptyCase.expected, out.String()))
182182
}
@@ -202,7 +202,7 @@ func TestSignerInfoContextWrite(t *testing.T) {
202202
// Table Format
203203
{
204204
formatter.Context{
205-
Format: NewSignerInfoFormat(),
205+
Format: defaultSignerInfoTableFormat,
206206
Trunc: true,
207207
},
208208
`SIGNER KEYS
@@ -214,7 +214,7 @@ eve foobarbazqux, key31, key32
214214
// No truncation
215215
{
216216
formatter.Context{
217-
Format: NewSignerInfoFormat(),
217+
Format: defaultSignerInfoTableFormat,
218218
},
219219
`SIGNER KEYS
220220
alice key11, key12
@@ -224,7 +224,7 @@ eve foobarbazquxquux, key31, key32
224224
},
225225
}
226226

227-
signerInfo := []SignerInfo{
227+
signerInfo := []signerInfo{
228228
{Name: "alice", Keys: []string{"key11", "key12"}},
229229
{Name: "bob", Keys: []string{"key21"}},
230230
{Name: "eve", Keys: []string{"key31", "key32", "foobarbazquxquux"}},
@@ -234,7 +234,7 @@ eve foobarbazquxquux, key31, key32
234234
var out bytes.Buffer
235235
tc.context.Output = &out
236236

237-
if err := SignerInfoWrite(tc.context, signerInfo); err != nil {
237+
if err := signerInfoWrite(tc.context, signerInfo); err != nil {
238238
assert.Error(t, err, tc.expected)
239239
} else {
240240
assert.Equal(t, out.String(), tc.expected)

cli/command/trust/inspect_pretty.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,39 @@ func printSortedAdminKeys(out io.Writer, adminRoles []client.RoleWithSignatures)
5656
func printSignatures(out io.Writer, signatureRows []trustTagRow) error {
5757
trustTagCtx := formatter.Context{
5858
Output: out,
59-
Format: NewTrustTagFormat(),
59+
Format: defaultTrustTagTableFormat,
6060
}
6161
// convert the formatted type before printing
62-
formattedTags := []SignedTagInfo{}
62+
formattedTags := []signedTagInfo{}
6363
for _, sigRow := range signatureRows {
6464
formattedSigners := sigRow.Signers
6565
if len(formattedSigners) == 0 {
6666
formattedSigners = append(formattedSigners, fmt.Sprintf("(%s)", releasedRoleName))
6767
}
68-
formattedTags = append(formattedTags, SignedTagInfo{
68+
formattedTags = append(formattedTags, signedTagInfo{
6969
Name: sigRow.SignedTag,
7070
Digest: sigRow.Digest,
7171
Signers: formattedSigners,
7272
})
7373
}
74-
return TagWrite(trustTagCtx, formattedTags)
74+
return tagWrite(trustTagCtx, formattedTags)
7575
}
7676

7777
func printSignerInfo(out io.Writer, roleToKeyIDs map[string][]string) error {
7878
signerInfoCtx := formatter.Context{
7979
Output: out,
80-
Format: NewSignerInfoFormat(),
80+
Format: defaultSignerInfoTableFormat,
8181
Trunc: true,
8282
}
83-
formattedSignerInfo := []SignerInfo{}
83+
formattedSignerInfo := []signerInfo{}
8484
for name, keyIDs := range roleToKeyIDs {
85-
formattedSignerInfo = append(formattedSignerInfo, SignerInfo{
85+
formattedSignerInfo = append(formattedSignerInfo, signerInfo{
8686
Name: name,
8787
Keys: keyIDs,
8888
})
8989
}
9090
sort.Slice(formattedSignerInfo, func(i, j int) bool {
9191
return sortorder.NaturalLess(formattedSignerInfo[i].Name, formattedSignerInfo[j].Name)
9292
})
93-
return SignerInfoWrite(signerInfoCtx, formattedSignerInfo)
93+
return signerInfoWrite(signerInfoCtx, formattedSignerInfo)
9494
}

0 commit comments

Comments
 (0)