Skip to content

Commit e626f77

Browse files
committed
cli/command/config: deprecate NewFormat, FormatWrite, InspectFormatWrite
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 d861b78 commit e626f77

4 files changed

Lines changed: 35 additions & 14 deletions

File tree

cli/command/config/formatter.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ Data:
3030
)
3131

3232
// NewFormat returns a Format for rendering using a config Context
33+
//
34+
// Deprecated: this function was only used internally and will be removed in the next release.
3335
func NewFormat(source string, quiet bool) formatter.Format {
36+
return newFormat(source, quiet)
37+
}
38+
39+
// newFormat returns a Format for rendering using a configContext.
40+
func newFormat(source string, quiet bool) formatter.Format {
3441
switch source {
3542
case formatter.PrettyFormatKey:
3643
return configInspectPrettyTemplate
@@ -44,7 +51,14 @@ func NewFormat(source string, quiet bool) formatter.Format {
4451
}
4552

4653
// FormatWrite writes the context
47-
func FormatWrite(ctx formatter.Context, configs []swarm.Config) error {
54+
//
55+
// Deprecated: this function was only used internally and will be removed in the next release.
56+
func FormatWrite(fmtCtx formatter.Context, configs []swarm.Config) error {
57+
return formatWrite(fmtCtx, configs)
58+
}
59+
60+
// formatWrite writes the context
61+
func formatWrite(fmtCtx formatter.Context, configs []swarm.Config) error {
4862
render := func(format func(subContext formatter.SubContext) error) error {
4963
for _, config := range configs {
5064
configCtx := &configContext{c: config}
@@ -54,7 +68,7 @@ func FormatWrite(ctx formatter.Context, configs []swarm.Config) error {
5468
}
5569
return nil
5670
}
57-
return ctx.Write(newConfigContext(), render)
71+
return fmtCtx.Write(newConfigContext(), render)
5872
}
5973

6074
func newConfigContext() *configContext {
@@ -115,9 +129,16 @@ func (c *configContext) Label(name string) string {
115129
}
116130

117131
// InspectFormatWrite renders the context for a list of configs
118-
func InspectFormatWrite(ctx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
119-
if ctx.Format != configInspectPrettyTemplate {
120-
return inspect.Inspect(ctx.Output, refs, string(ctx.Format), getRef)
132+
//
133+
// Deprecated: this function was only used internally and will be removed in the next release.
134+
func InspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
135+
return inspectFormatWrite(fmtCtx, refs, getRef)
136+
}
137+
138+
// inspectFormatWrite renders the context for a list of configs
139+
func inspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
140+
if fmtCtx.Format != configInspectPrettyTemplate {
141+
return inspect.Inspect(fmtCtx.Output, refs, string(fmtCtx.Format), getRef)
121142
}
122143
render := func(format func(subContext formatter.SubContext) error) error {
123144
for _, ref := range refs {
@@ -135,7 +156,7 @@ func InspectFormatWrite(ctx formatter.Context, refs []string, getRef inspect.Get
135156
}
136157
return nil
137158
}
138-
return ctx.Write(&configInspectContext{}, render)
159+
return fmtCtx.Write(&configInspectContext{}, render)
139160
}
140161

141162
type configInspectContext struct {

cli/command/config/formatter_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ func TestConfigContextFormatWrite(t *testing.T) {
2727
},
2828
// Table format
2929
{
30-
formatter.Context{Format: NewFormat("table", false)},
30+
formatter.Context{Format: newFormat("table", false)},
3131
`ID NAME CREATED UPDATED
3232
1 passwords Less than a second ago Less than a second ago
3333
2 id_rsa Less than a second ago Less than a second ago
3434
`,
3535
},
3636
{
37-
formatter.Context{Format: NewFormat("table {{.Name}}", true)},
37+
formatter.Context{Format: newFormat("table {{.Name}}", true)},
3838
`NAME
3939
passwords
4040
id_rsa
4141
`,
4242
},
4343
{
44-
formatter.Context{Format: NewFormat("{{.ID}}-{{.Name}}", false)},
44+
formatter.Context{Format: newFormat("{{.ID}}-{{.Name}}", false)},
4545
`1-passwords
4646
2-id_rsa
4747
`,
@@ -64,7 +64,7 @@ id_rsa
6464
t.Run(string(tc.context.Format), func(t *testing.T) {
6565
var out bytes.Buffer
6666
tc.context.Output = &out
67-
if err := FormatWrite(tc.context, configs); err != nil {
67+
if err := formatWrite(tc.context, configs); err != nil {
6868
assert.ErrorContains(t, err, tc.expected)
6969
} else {
7070
assert.Equal(t, out.String(), tc.expected)

cli/command/config/inspect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ func RunConfigInspect(ctx context.Context, dockerCLI command.Cli, opts InspectOp
6363

6464
configCtx := formatter.Context{
6565
Output: dockerCLI.Out(),
66-
Format: NewFormat(f, false),
66+
Format: newFormat(f, false),
6767
}
6868

69-
if err := InspectFormatWrite(configCtx, opts.Names, getRef); err != nil {
69+
if err := inspectFormatWrite(configCtx, opts.Names, getRef); err != nil {
7070
return cli.StatusError{StatusCode: 1, Status: err.Error()}
7171
}
7272
return nil

cli/command/config/ls.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func RunConfigList(ctx context.Context, dockerCLI command.Cli, options ListOptio
6868

6969
configCtx := formatter.Context{
7070
Output: dockerCLI.Out(),
71-
Format: NewFormat(format, options.Quiet),
71+
Format: newFormat(format, options.Quiet),
7272
}
73-
return FormatWrite(configCtx, configs)
73+
return formatWrite(configCtx, configs)
7474
}

0 commit comments

Comments
 (0)