Skip to content

Commit e3903a1

Browse files
committed
cli/command/network: 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 b0d1d94 commit e3903a1

3 files changed

Lines changed: 31 additions & 17 deletions

File tree

cli/command/network/formatter.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ const (
1717
internalHeader = "INTERNAL"
1818
)
1919

20-
// NewFormat returns a Format for rendering using a network Context
20+
// NewFormat returns a Format for rendering using a network Context.
21+
//
22+
// Deprecated: this function was only used internally and will be removed in the next release.
2123
func NewFormat(source string, quiet bool) formatter.Format {
24+
return newFormat(source, quiet)
25+
}
26+
27+
// newFormat returns a [formatter.Format] for rendering a networkContext.
28+
func newFormat(source string, quiet bool) formatter.Format {
2229
switch source {
2330
case formatter.TableFormatKey:
2431
if quiet {
@@ -35,10 +42,17 @@ func NewFormat(source string, quiet bool) formatter.Format {
3542
}
3643

3744
// FormatWrite writes the context
38-
func FormatWrite(ctx formatter.Context, networks []network.Summary) error {
45+
//
46+
// Deprecated: this function was only used internally and will be removed in the next release.
47+
func FormatWrite(fmtCtx formatter.Context, networks []network.Summary) error {
48+
return formatWrite(fmtCtx, networks)
49+
}
50+
51+
// formatWrite writes the context.
52+
func formatWrite(fmtCtx formatter.Context, networks []network.Summary) error {
3953
render := func(format func(subContext formatter.SubContext) error) error {
4054
for _, nw := range networks {
41-
networkCtx := &networkContext{trunc: ctx.Trunc, n: nw}
55+
networkCtx := &networkContext{trunc: fmtCtx.Trunc, n: nw}
4256
if err := format(networkCtx); err != nil {
4357
return err
4458
}
@@ -57,7 +71,7 @@ func FormatWrite(ctx formatter.Context, networks []network.Summary) error {
5771
"Labels": formatter.LabelsHeader,
5872
"CreatedAt": formatter.CreatedAtHeader,
5973
}
60-
return ctx.Write(&networkCtx, render)
74+
return fmtCtx.Write(&networkCtx, render)
6175
}
6276

6377
type networkContext struct {

cli/command/network/formatter_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,35 @@ func TestNetworkContextWrite(t *testing.T) {
9191
},
9292
// Table format
9393
{
94-
formatter.Context{Format: NewFormat("table", false)},
94+
formatter.Context{Format: newFormat("table", false)},
9595
`NETWORK ID NAME DRIVER SCOPE
9696
networkID1 foobar_baz foo local
9797
networkID2 foobar_bar bar local
9898
`,
9999
},
100100
{
101-
formatter.Context{Format: NewFormat("table", true)},
101+
formatter.Context{Format: newFormat("table", true)},
102102
`networkID1
103103
networkID2
104104
`,
105105
},
106106
{
107-
formatter.Context{Format: NewFormat("table {{.Name}}", false)},
107+
formatter.Context{Format: newFormat("table {{.Name}}", false)},
108108
`NAME
109109
foobar_baz
110110
foobar_bar
111111
`,
112112
},
113113
{
114-
formatter.Context{Format: NewFormat("table {{.Name}}", true)},
114+
formatter.Context{Format: newFormat("table {{.Name}}", true)},
115115
`NAME
116116
foobar_baz
117117
foobar_bar
118118
`,
119119
},
120120
// Raw Format
121121
{
122-
formatter.Context{Format: NewFormat("raw", false)},
122+
formatter.Context{Format: newFormat("raw", false)},
123123
`network_id: networkID1
124124
name: foobar_baz
125125
driver: foo
@@ -133,21 +133,21 @@ scope: local
133133
`,
134134
},
135135
{
136-
formatter.Context{Format: NewFormat("raw", true)},
136+
formatter.Context{Format: newFormat("raw", true)},
137137
`network_id: networkID1
138138
network_id: networkID2
139139
`,
140140
},
141141
// Custom Format
142142
{
143-
formatter.Context{Format: NewFormat("{{.Name}}", false)},
143+
formatter.Context{Format: newFormat("{{.Name}}", false)},
144144
`foobar_baz
145145
foobar_bar
146146
`,
147147
},
148148
// Custom Format with CreatedAt
149149
{
150-
formatter.Context{Format: NewFormat("{{.Name}} {{.CreatedAt}}", false)},
150+
formatter.Context{Format: newFormat("{{.Name}} {{.CreatedAt}}", false)},
151151
`foobar_baz 2016-01-01 00:00:00 +0000 UTC
152152
foobar_bar 2017-01-01 00:00:00 +0000 UTC
153153
`,
@@ -166,7 +166,7 @@ foobar_bar 2017-01-01 00:00:00 +0000 UTC
166166
t.Run(string(tc.context.Format), func(t *testing.T) {
167167
var out bytes.Buffer
168168
tc.context.Output = &out
169-
err := FormatWrite(tc.context, networks)
169+
err := formatWrite(tc.context, networks)
170170
if err != nil {
171171
assert.Error(t, err, tc.expected)
172172
} else {
@@ -187,7 +187,7 @@ func TestNetworkContextWriteJSON(t *testing.T) {
187187
}
188188

189189
out := bytes.NewBufferString("")
190-
err := FormatWrite(formatter.Context{Format: "{{json .}}", Output: out}, networks)
190+
err := formatWrite(formatter.Context{Format: "{{json .}}", Output: out}, networks)
191191
if err != nil {
192192
t.Fatal(err)
193193
}
@@ -206,7 +206,7 @@ func TestNetworkContextWriteJSONField(t *testing.T) {
206206
{ID: "networkID2", Name: "foobar_bar"},
207207
}
208208
out := bytes.NewBufferString("")
209-
err := FormatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, networks)
209+
err := formatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, networks)
210210
if err != nil {
211211
t.Fatal(err)
212212
}

cli/command/network/list.go

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

6868
networksCtx := formatter.Context{
6969
Output: dockerCli.Out(),
70-
Format: NewFormat(format, options.quiet),
70+
Format: newFormat(format, options.quiet),
7171
Trunc: !options.noTrunc,
7272
}
73-
return FormatWrite(networksCtx, networkResources)
73+
return formatWrite(networksCtx, networkResources)
7474
}

0 commit comments

Comments
 (0)