Skip to content

Commit bf47419

Browse files
committed
cli/command/plugin: deprecate NewFormat, FormatWrite
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 123ef81 commit bf47419

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

‎cli/command/plugin/formatter.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ enabled: {{.Enabled}}
2121
)
2222

2323
// NewFormat returns a Format for rendering using a plugin Context
24+
//
25+
// Deprecated: this function was only used internally and will be removed in the next release.
2426
func NewFormat(source string, quiet bool) formatter.Format {
27+
return newFormat(source, quiet)
28+
}
29+
30+
// newFormat returns a Format for rendering using a pluginContext.
31+
func newFormat(source string, quiet bool) formatter.Format {
2532
switch source {
2633
case formatter.TableFormatKey:
2734
if quiet {
@@ -38,10 +45,17 @@ func NewFormat(source string, quiet bool) formatter.Format {
3845
}
3946

4047
// FormatWrite writes the context
41-
func FormatWrite(ctx formatter.Context, plugins []*plugin.Plugin) error {
48+
//
49+
// Deprecated: this function was only used internally and will be removed in the next release.
50+
func FormatWrite(fmtCtx formatter.Context, plugins []*plugin.Plugin) error {
51+
return formatWrite(fmtCtx, plugins)
52+
}
53+
54+
// formatWrite writes the context
55+
func formatWrite(fmtCtx formatter.Context, plugins []*plugin.Plugin) error {
4256
render := func(format func(subContext formatter.SubContext) error) error {
4357
for _, p := range plugins {
44-
pluginCtx := &pluginContext{trunc: ctx.Trunc, p: *p}
58+
pluginCtx := &pluginContext{trunc: fmtCtx.Trunc, p: *p}
4559
if err := format(pluginCtx); err != nil {
4660
return err
4761
}
@@ -56,7 +70,7 @@ func FormatWrite(ctx formatter.Context, plugins []*plugin.Plugin) error {
5670
"Enabled": enabledHeader,
5771
"PluginReference": formatter.ImageHeader,
5872
}
59-
return ctx.Write(&pluginCtx, render)
73+
return fmtCtx.Write(&pluginCtx, render)
6074
}
6175

6276
type pluginContext struct {

‎cli/command/plugin/formatter_test.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,38 +86,38 @@ func TestPluginContextWrite(t *testing.T) {
8686
},
8787
{
8888
doc: "table format",
89-
context: formatter.Context{Format: NewFormat("table", false)},
89+
context: formatter.Context{Format: newFormat("table", false)},
9090
expected: `ID NAME DESCRIPTION ENABLED
9191
pluginID1 foobar_baz description 1 true
9292
pluginID2 foobar_bar description 2 false
9393
`,
9494
},
9595
{
9696
doc: "table format, quiet",
97-
context: formatter.Context{Format: NewFormat("table", true)},
97+
context: formatter.Context{Format: newFormat("table", true)},
9898
expected: `pluginID1
9999
pluginID2
100100
`,
101101
},
102102
{
103103
doc: "table format name col",
104-
context: formatter.Context{Format: NewFormat("table {{.Name}}", false)},
104+
context: formatter.Context{Format: newFormat("table {{.Name}}", false)},
105105
expected: `NAME
106106
foobar_baz
107107
foobar_bar
108108
`,
109109
},
110110
{
111111
doc: "table format name col, quiet",
112-
context: formatter.Context{Format: NewFormat("table {{.Name}}", true)},
112+
context: formatter.Context{Format: newFormat("table {{.Name}}", true)},
113113
expected: `NAME
114114
foobar_baz
115115
foobar_bar
116116
`,
117117
},
118118
{
119119
doc: "raw format",
120-
context: formatter.Context{Format: NewFormat("raw", false)},
120+
context: formatter.Context{Format: newFormat("raw", false)},
121121
expected: `plugin_id: pluginID1
122122
name: foobar_baz
123123
description: description 1
@@ -132,14 +132,14 @@ enabled: false
132132
},
133133
{
134134
doc: "raw format, quiet",
135-
context: formatter.Context{Format: NewFormat("raw", true)},
135+
context: formatter.Context{Format: newFormat("raw", true)},
136136
expected: `plugin_id: pluginID1
137137
plugin_id: pluginID2
138138
`,
139139
},
140140
{
141141
doc: "custom format",
142-
context: formatter.Context{Format: NewFormat("{{.Name}}", false)},
142+
context: formatter.Context{Format: newFormat("{{.Name}}", false)},
143143
expected: `foobar_baz
144144
foobar_bar
145145
`,
@@ -156,7 +156,7 @@ foobar_bar
156156
var out bytes.Buffer
157157
tc.context.Output = &out
158158

159-
err := FormatWrite(tc.context, plugins)
159+
err := formatWrite(tc.context, plugins)
160160
if err != nil {
161161
assert.Error(t, err, tc.expected)
162162
} else {
@@ -177,7 +177,7 @@ func TestPluginContextWriteJSON(t *testing.T) {
177177
}
178178

179179
out := bytes.NewBufferString("")
180-
err := FormatWrite(formatter.Context{Format: "{{json .}}", Output: out}, plugins)
180+
err := formatWrite(formatter.Context{Format: "{{json .}}", Output: out}, plugins)
181181
if err != nil {
182182
t.Fatal(err)
183183
}
@@ -196,7 +196,7 @@ func TestPluginContextWriteJSONField(t *testing.T) {
196196
{ID: "pluginID2", Name: "foobar_bar"},
197197
}
198198
out := bytes.NewBufferString("")
199-
err := FormatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, plugins)
199+
err := formatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, plugins)
200200
if err != nil {
201201
t.Fatal(err)
202202
}

‎cli/command/plugin/list.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func runList(ctx context.Context, dockerCli command.Cli, options listOptions) er
6666

6767
pluginsCtx := formatter.Context{
6868
Output: dockerCli.Out(),
69-
Format: NewFormat(format, options.quiet),
69+
Format: newFormat(format, options.quiet),
7070
Trunc: !options.noTrunc,
7171
}
72-
return FormatWrite(pluginsCtx, plugins)
72+
return formatWrite(pluginsCtx, plugins)
7373
}

0 commit comments

Comments
 (0)