Skip to content

Commit 0bbdda8

Browse files
authored
Merge pull request #6341 from thaJeztah/28.x_backport_internalize_formatters
[28.x backport] cli/command/*: deprecate formatting-related functions and types
2 parents b52ab17 + 4133271 commit 0bbdda8

39 files changed

+396
-187
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/container/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func runDiff(ctx context.Context, dockerCLI command.Cli, containerID string) err
3939
}
4040
diffCtx := formatter.Context{
4141
Output: dockerCLI.Out(),
42-
Format: NewDiffFormat("{{.Type}} {{.Path}}"),
42+
Format: newDiffFormat("{{.Type}} {{.Path}}"),
4343
}
44-
return DiffFormatWrite(diffCtx, changes)
44+
return diffFormatWrite(diffCtx, changes)
4545
}

cli/command/container/formatter_diff.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,37 @@ const (
1313
)
1414

1515
// NewDiffFormat returns a format for use with a diff Context
16+
//
17+
// Deprecated: this function was only used internally and will be removed in the next release.
1618
func NewDiffFormat(source string) formatter.Format {
19+
return newDiffFormat(source)
20+
}
21+
22+
// newDiffFormat returns a format for use with a diff [formatter.Context].
23+
func newDiffFormat(source string) formatter.Format {
1724
if source == formatter.TableFormatKey {
1825
return defaultDiffTableFormat
1926
}
2027
return formatter.Format(source)
2128
}
2229

2330
// DiffFormatWrite writes formatted diff using the Context
24-
func DiffFormatWrite(ctx formatter.Context, changes []container.FilesystemChange) error {
25-
render := func(format func(subContext formatter.SubContext) error) error {
31+
//
32+
// Deprecated: this function was only used internally and will be removed in the next release.
33+
func DiffFormatWrite(fmtCtx formatter.Context, changes []container.FilesystemChange) error {
34+
return diffFormatWrite(fmtCtx, changes)
35+
}
36+
37+
// diffFormatWrite writes formatted diff using the [formatter.Context].
38+
func diffFormatWrite(fmtCtx formatter.Context, changes []container.FilesystemChange) error {
39+
return fmtCtx.Write(newDiffContext(), func(format func(subContext formatter.SubContext) error) error {
2640
for _, change := range changes {
2741
if err := format(&diffContext{c: change}); err != nil {
2842
return err
2943
}
3044
}
3145
return nil
32-
}
33-
return ctx.Write(newDiffContext(), render)
46+
})
3447
}
3548

3649
type diffContext struct {
@@ -39,12 +52,14 @@ type diffContext struct {
3952
}
4053

4154
func newDiffContext() *diffContext {
42-
diffCtx := diffContext{}
43-
diffCtx.Header = formatter.SubHeaderContext{
44-
"Type": changeTypeHeader,
45-
"Path": pathHeader,
55+
return &diffContext{
56+
HeaderContext: formatter.HeaderContext{
57+
Header: formatter.SubHeaderContext{
58+
"Type": changeTypeHeader,
59+
"Path": pathHeader,
60+
},
61+
},
4662
}
47-
return &diffCtx
4863
}
4964

5065
func (d *diffContext) MarshalJSON() ([]byte, error) {

cli/command/container/formatter_diff_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ func TestDiffContextFormatWrite(t *testing.T) {
1616
expected string
1717
}{
1818
{
19-
formatter.Context{Format: NewDiffFormat("table")},
19+
formatter.Context{Format: newDiffFormat("table")},
2020
`CHANGE TYPE PATH
2121
C /var/log/app.log
2222
A /usr/app/app.js
2323
D /usr/app/old_app.js
2424
`,
2525
},
2626
{
27-
formatter.Context{Format: NewDiffFormat("table {{.Path}}")},
27+
formatter.Context{Format: newDiffFormat("table {{.Path}}")},
2828
`PATH
2929
/var/log/app.log
3030
/usr/app/app.js
3131
/usr/app/old_app.js
3232
`,
3333
},
3434
{
35-
formatter.Context{Format: NewDiffFormat("{{.Type}}: {{.Path}}")},
35+
formatter.Context{Format: newDiffFormat("{{.Type}}: {{.Path}}")},
3636
`C: /var/log/app.log
3737
A: /usr/app/app.js
3838
D: /usr/app/old_app.js
@@ -50,7 +50,7 @@ D: /usr/app/old_app.js
5050
t.Run(string(tc.context.Format), func(t *testing.T) {
5151
out := bytes.NewBufferString("")
5252
tc.context.Output = out
53-
err := DiffFormatWrite(tc.context, diffs)
53+
err := diffFormatWrite(tc.context, diffs)
5454
if err != nil {
5555
assert.Error(t, err, tc.expected)
5656
} else {

0 commit comments

Comments
 (0)