1
0
mirror of https://github.com/docker/cli.git synced 2026-01-13 18:22:35 +03:00

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>
This commit is contained in:
Sebastiaan van Stijn
2025-08-21 14:46:59 +02:00
parent bf47419852
commit 83371c2014
3 changed files with 25 additions and 11 deletions

View File

@@ -16,8 +16,15 @@ const (
automatedHeader = "AUTOMATED"
)
// NewSearchFormat returns a Format for rendering using a network Context
// NewSearchFormat returns a Format for rendering using a search Context
//
// Deprecated: this function was only used internally and will be removed in the next release.
func NewSearchFormat(source string) formatter.Format {
return newFormat(source)
}
// newFormat returns a Format for rendering using a searchContext.
func newFormat(source string) formatter.Format {
switch source {
case "", formatter.TableFormatKey:
return defaultSearchTableFormat
@@ -26,10 +33,17 @@ func NewSearchFormat(source string) formatter.Format {
}
// SearchWrite writes the context
func SearchWrite(ctx formatter.Context, results []registrytypes.SearchResult) error {
//
// Deprecated: this function was only used internally and will be removed in the next release.
func SearchWrite(fmtCtx formatter.Context, results []registrytypes.SearchResult) error {
return formatWrite(fmtCtx, results)
}
// formatWrite writes the context.
func formatWrite(fmtCtx formatter.Context, results []registrytypes.SearchResult) error {
render := func(format func(subContext formatter.SubContext) error) error {
for _, result := range results {
searchCtx := &searchContext{trunc: ctx.Trunc, s: result}
searchCtx := &searchContext{trunc: fmtCtx.Trunc, s: result}
if err := format(searchCtx); err != nil {
return err
}
@@ -43,7 +57,7 @@ func SearchWrite(ctx formatter.Context, results []registrytypes.SearchResult) er
"StarCount": starsHeader,
"IsOfficial": officialHeader,
}
return ctx.Write(&searchCtx, render)
return fmtCtx.Write(&searchCtx, render)
}
type searchContext struct {

View File

@@ -157,12 +157,12 @@ func TestSearchContextWrite(t *testing.T) {
},
{
doc: "Table format",
format: NewSearchFormat("table"),
format: newFormat("table"),
expected: string(golden.Get(t, "search-context-write-table.golden")),
},
{
doc: "Table format, single column",
format: NewSearchFormat("table {{.Name}}"),
format: newFormat("table {{.Name}}"),
expected: `NAME
result1
result2
@@ -170,14 +170,14 @@ result2
},
{
doc: "Custom format, single field",
format: NewSearchFormat("{{.Name}}"),
format: newFormat("{{.Name}}"),
expected: `result1
result2
`,
},
{
doc: "Custom Format, two columns",
format: NewSearchFormat("{{.Name}} {{.StarCount}}"),
format: newFormat("{{.Name}} {{.StarCount}}"),
expected: `result1 5000
result2 5
`,
@@ -192,7 +192,7 @@ result2 5
for _, tc := range cases {
t.Run(tc.doc, func(t *testing.T) {
var out bytes.Buffer
err := SearchWrite(formatter.Context{Format: tc.format, Output: &out}, results)
err := formatWrite(formatter.Context{Format: tc.format, Output: &out}, results)
if tc.expectedErr != "" {
assert.Check(t, is.Error(err, tc.expectedErr))
} else {

View File

@@ -74,10 +74,10 @@ func runSearch(ctx context.Context, dockerCli command.Cli, options searchOptions
searchCtx := formatter.Context{
Output: dockerCli.Out(),
Format: NewSearchFormat(options.format),
Format: newFormat(options.format),
Trunc: !options.noTrunc,
}
return SearchWrite(searchCtx, results)
return formatWrite(searchCtx, results)
}
// authConfigKey is the key used to store credentials for Docker Hub. It is