Skip to content

Commit 83371c2

Browse files
committed
cli/command/registry: deprecate NewSearchFormat, SearchWrite
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. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent bf47419 commit 83371c2

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

cli/command/registry/formatter_search.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ const (
1616
automatedHeader = "AUTOMATED"
1717
)
1818

19-
// NewSearchFormat returns a Format for rendering using a network Context
19+
// NewSearchFormat returns a Format for rendering using a search Context
20+
//
21+
// Deprecated: this function was only used internally and will be removed in the next release.
2022
func NewSearchFormat(source string) formatter.Format {
23+
return newFormat(source)
24+
}
25+
26+
// newFormat returns a Format for rendering using a searchContext.
27+
func newFormat(source string) formatter.Format {
2128
switch source {
2229
case "", formatter.TableFormatKey:
2330
return defaultSearchTableFormat
@@ -26,10 +33,17 @@ func NewSearchFormat(source string) formatter.Format {
2633
}
2734

2835
// SearchWrite writes the context
29-
func SearchWrite(ctx formatter.Context, results []registrytypes.SearchResult) error {
36+
//
37+
// Deprecated: this function was only used internally and will be removed in the next release.
38+
func SearchWrite(fmtCtx formatter.Context, results []registrytypes.SearchResult) error {
39+
return formatWrite(fmtCtx, results)
40+
}
41+
42+
// formatWrite writes the context.
43+
func formatWrite(fmtCtx formatter.Context, results []registrytypes.SearchResult) error {
3044
render := func(format func(subContext formatter.SubContext) error) error {
3145
for _, result := range results {
32-
searchCtx := &searchContext{trunc: ctx.Trunc, s: result}
46+
searchCtx := &searchContext{trunc: fmtCtx.Trunc, s: result}
3347
if err := format(searchCtx); err != nil {
3448
return err
3549
}
@@ -43,7 +57,7 @@ func SearchWrite(ctx formatter.Context, results []registrytypes.SearchResult) er
4357
"StarCount": starsHeader,
4458
"IsOfficial": officialHeader,
4559
}
46-
return ctx.Write(&searchCtx, render)
60+
return fmtCtx.Write(&searchCtx, render)
4761
}
4862

4963
type searchContext struct {

cli/command/registry/formatter_search_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,27 +157,27 @@ func TestSearchContextWrite(t *testing.T) {
157157
},
158158
{
159159
doc: "Table format",
160-
format: NewSearchFormat("table"),
160+
format: newFormat("table"),
161161
expected: string(golden.Get(t, "search-context-write-table.golden")),
162162
},
163163
{
164164
doc: "Table format, single column",
165-
format: NewSearchFormat("table {{.Name}}"),
165+
format: newFormat("table {{.Name}}"),
166166
expected: `NAME
167167
result1
168168
result2
169169
`,
170170
},
171171
{
172172
doc: "Custom format, single field",
173-
format: NewSearchFormat("{{.Name}}"),
173+
format: newFormat("{{.Name}}"),
174174
expected: `result1
175175
result2
176176
`,
177177
},
178178
{
179179
doc: "Custom Format, two columns",
180-
format: NewSearchFormat("{{.Name}} {{.StarCount}}"),
180+
format: newFormat("{{.Name}} {{.StarCount}}"),
181181
expected: `result1 5000
182182
result2 5
183183
`,
@@ -192,7 +192,7 @@ result2 5
192192
for _, tc := range cases {
193193
t.Run(tc.doc, func(t *testing.T) {
194194
var out bytes.Buffer
195-
err := SearchWrite(formatter.Context{Format: tc.format, Output: &out}, results)
195+
err := formatWrite(formatter.Context{Format: tc.format, Output: &out}, results)
196196
if tc.expectedErr != "" {
197197
assert.Check(t, is.Error(err, tc.expectedErr))
198198
} else {

cli/command/registry/search.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ func runSearch(ctx context.Context, dockerCli command.Cli, options searchOptions
7474

7575
searchCtx := formatter.Context{
7676
Output: dockerCli.Out(),
77-
Format: NewSearchFormat(options.format),
77+
Format: newFormat(options.format),
7878
Trunc: !options.noTrunc,
7979
}
80-
return SearchWrite(searchCtx, results)
80+
return formatWrite(searchCtx, results)
8181
}
8282

8383
// authConfigKey is the key used to store credentials for Docker Hub. It is

0 commit comments

Comments
 (0)