Skip to content

Commit 58bb45c

Browse files
authored
Merge pull request #6336 from thaJeztah/internalize_formatters
cli/command/*: deprecate formatting-related functions and types
2 parents b0d1d94 + 95c9b1b commit 58bb45c

36 files changed

+365
-171
lines changed

cli/command/checkpoint/formatter.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,29 @@ const (
1111
)
1212

1313
// NewFormat returns a format for use with a checkpoint Context
14+
//
15+
// Deprecated: this function was only used internally and will be removed in the next release.
1416
func NewFormat(source string) formatter.Format {
17+
return newFormat(source)
18+
}
19+
20+
// newFormat returns a format for use with a checkpointContext.
21+
func newFormat(source string) formatter.Format {
1522
if source == formatter.TableFormatKey {
1623
return defaultCheckpointFormat
1724
}
1825
return formatter.Format(source)
1926
}
2027

2128
// FormatWrite writes formatted checkpoints using the Context
22-
func FormatWrite(ctx formatter.Context, checkpoints []checkpoint.Summary) error {
29+
//
30+
// Deprecated: this function was only used internally and will be removed in the next release.
31+
func FormatWrite(fmtCtx formatter.Context, checkpoints []checkpoint.Summary) error {
32+
return formatWrite(fmtCtx, checkpoints)
33+
}
34+
35+
// formatWrite writes formatted checkpoints using the Context
36+
func formatWrite(fmtCtx formatter.Context, checkpoints []checkpoint.Summary) error {
2337
render := func(format func(subContext formatter.SubContext) error) error {
2438
for _, cp := range checkpoints {
2539
if err := format(&checkpointContext{c: cp}); err != nil {
@@ -28,7 +42,7 @@ func FormatWrite(ctx formatter.Context, checkpoints []checkpoint.Summary) error
2842
}
2943
return nil
3044
}
31-
return ctx.Write(newCheckpointContext(), render)
45+
return fmtCtx.Write(newCheckpointContext(), render)
3246
}
3347

3448
type checkpointContext struct {

cli/command/checkpoint/formatter_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ func TestCheckpointContextFormatWrite(t *testing.T) {
1515
expected string
1616
}{
1717
{
18-
formatter.Context{Format: NewFormat(defaultCheckpointFormat)},
18+
formatter.Context{Format: newFormat(defaultCheckpointFormat)},
1919
`CHECKPOINT NAME
2020
checkpoint-1
2121
checkpoint-2
2222
checkpoint-3
2323
`,
2424
},
2525
{
26-
formatter.Context{Format: NewFormat("{{.Name}}")},
26+
formatter.Context{Format: newFormat("{{.Name}}")},
2727
`checkpoint-1
2828
checkpoint-2
2929
checkpoint-3
3030
`,
3131
},
3232
{
33-
formatter.Context{Format: NewFormat("{{.Name}}:")},
33+
formatter.Context{Format: newFormat("{{.Name}}:")},
3434
`checkpoint-1:
3535
checkpoint-2:
3636
checkpoint-3:
@@ -41,7 +41,7 @@ checkpoint-3:
4141
for _, testcase := range cases {
4242
out := bytes.NewBufferString("")
4343
testcase.context.Output = out
44-
err := FormatWrite(testcase.context, []checkpoint.Summary{
44+
err := formatWrite(testcase.context, []checkpoint.Summary{
4545
{Name: "checkpoint-1"},
4646
{Name: "checkpoint-2"},
4747
{Name: "checkpoint-3"},

cli/command/checkpoint/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func runList(ctx context.Context, dockerCli command.Cli, container string, opts
4545

4646
cpCtx := formatter.Context{
4747
Output: dockerCli.Out(),
48-
Format: NewFormat(formatter.TableFormatKey),
48+
Format: newFormat(formatter.TableFormatKey),
4949
}
50-
return FormatWrite(cpCtx, checkpoints)
50+
return formatWrite(cpCtx, checkpoints)
5151
}

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
}

cli/command/image/formatter_history.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ const (
2020
)
2121

2222
// NewHistoryFormat returns a format for rendering an HistoryContext
23+
//
24+
// Deprecated: this function was only used internally and will be removed in the next release.
2325
func NewHistoryFormat(source string, quiet bool, human bool) formatter.Format {
26+
return newHistoryFormat(source, quiet, human)
27+
}
28+
29+
// newHistoryFormat returns a format for rendering a historyContext.
30+
func newHistoryFormat(source string, quiet bool, human bool) formatter.Format {
2431
if source == formatter.TableFormatKey {
2532
switch {
2633
case quiet:
@@ -36,10 +43,17 @@ func NewHistoryFormat(source string, quiet bool, human bool) formatter.Format {
3643
}
3744

3845
// HistoryWrite writes the context
39-
func HistoryWrite(ctx formatter.Context, human bool, histories []image.HistoryResponseItem) error {
46+
//
47+
// Deprecated: this function was only used internally and will be removed in the next release.
48+
func HistoryWrite(fmtCtx formatter.Context, human bool, histories []image.HistoryResponseItem) error {
49+
return historyWrite(fmtCtx, human, histories)
50+
}
51+
52+
// historyWrite writes the context
53+
func historyWrite(fmtCtx formatter.Context, human bool, histories []image.HistoryResponseItem) error {
4054
render := func(format func(subContext formatter.SubContext) error) error {
4155
for _, history := range histories {
42-
historyCtx := &historyContext{trunc: ctx.Trunc, h: history, human: human}
56+
historyCtx := &historyContext{trunc: fmtCtx.Trunc, h: history, human: human}
4357
if err := format(historyCtx); err != nil {
4458
return err
4559
}
@@ -55,7 +69,7 @@ func HistoryWrite(ctx formatter.Context, human bool, histories []image.HistoryRe
5569
"Size": formatter.SizeHeader,
5670
"Comment": commentHeader,
5771
}
58-
return ctx.Write(historyCtx, render)
72+
return fmtCtx.Write(historyCtx, render)
5973
}
6074

6175
type historyContext struct {

cli/command/image/formatter_history_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,15 @@ imageID6 17 years ago /bin/bash echo 183MB
237237
}{
238238
{
239239
formatter.Context{
240-
Format: NewHistoryFormat("table", false, true),
240+
Format: newHistoryFormat("table", false, true),
241241
Trunc: true,
242242
Output: out,
243243
},
244244
expectedTrunc,
245245
},
246246
{
247247
formatter.Context{
248-
Format: NewHistoryFormat("table", false, true),
248+
Format: newHistoryFormat("table", false, true),
249249
Trunc: false,
250250
Output: out,
251251
},
@@ -255,7 +255,7 @@ imageID6 17 years ago /bin/bash echo 183MB
255255

256256
for _, tc := range cases {
257257
t.Run(string(tc.context.Format), func(t *testing.T) {
258-
err := HistoryWrite(tc.context, true, histories)
258+
err := historyWrite(tc.context, true, histories)
259259
assert.NilError(t, err)
260260
assert.Equal(t, out.String(), tc.expected)
261261
// Clean buffer

cli/command/image/history.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func runHistory(ctx context.Context, dockerCli command.Cli, opts historyOptions)
7777

7878
historyCtx := formatter.Context{
7979
Output: dockerCli.Out(),
80-
Format: NewHistoryFormat(format, opts.quiet, opts.human),
80+
Format: newHistoryFormat(format, opts.quiet, opts.human),
8181
Trunc: !opts.noTrunc,
8282
}
83-
return HistoryWrite(historyCtx, opts.human, history)
83+
return historyWrite(historyCtx, opts.human, history)
8484
}

0 commit comments

Comments
 (0)