1
0
mirror of https://github.com/docker/cli.git synced 2026-01-13 18:22:35 +03:00

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>
This commit is contained in:
Sebastiaan van Stijn
2025-08-21 13:46:16 +02:00
parent b0d1d94711
commit e3903a1ac8
3 changed files with 31 additions and 17 deletions

View File

@@ -17,8 +17,15 @@ const (
internalHeader = "INTERNAL"
)
// NewFormat returns a Format for rendering using a network Context
// NewFormat returns a Format for rendering using a network Context.
//
// Deprecated: this function was only used internally and will be removed in the next release.
func NewFormat(source string, quiet bool) formatter.Format {
return newFormat(source, quiet)
}
// newFormat returns a [formatter.Format] for rendering a networkContext.
func newFormat(source string, quiet bool) formatter.Format {
switch source {
case formatter.TableFormatKey:
if quiet {
@@ -35,10 +42,17 @@ func NewFormat(source string, quiet bool) formatter.Format {
}
// FormatWrite writes the context
func FormatWrite(ctx formatter.Context, networks []network.Summary) error {
//
// Deprecated: this function was only used internally and will be removed in the next release.
func FormatWrite(fmtCtx formatter.Context, networks []network.Summary) error {
return formatWrite(fmtCtx, networks)
}
// formatWrite writes the context.
func formatWrite(fmtCtx formatter.Context, networks []network.Summary) error {
render := func(format func(subContext formatter.SubContext) error) error {
for _, nw := range networks {
networkCtx := &networkContext{trunc: ctx.Trunc, n: nw}
networkCtx := &networkContext{trunc: fmtCtx.Trunc, n: nw}
if err := format(networkCtx); err != nil {
return err
}
@@ -57,7 +71,7 @@ func FormatWrite(ctx formatter.Context, networks []network.Summary) error {
"Labels": formatter.LabelsHeader,
"CreatedAt": formatter.CreatedAtHeader,
}
return ctx.Write(&networkCtx, render)
return fmtCtx.Write(&networkCtx, render)
}
type networkContext struct {

View File

@@ -91,27 +91,27 @@ func TestNetworkContextWrite(t *testing.T) {
},
// Table format
{
formatter.Context{Format: NewFormat("table", false)},
formatter.Context{Format: newFormat("table", false)},
`NETWORK ID NAME DRIVER SCOPE
networkID1 foobar_baz foo local
networkID2 foobar_bar bar local
`,
},
{
formatter.Context{Format: NewFormat("table", true)},
formatter.Context{Format: newFormat("table", true)},
`networkID1
networkID2
`,
},
{
formatter.Context{Format: NewFormat("table {{.Name}}", false)},
formatter.Context{Format: newFormat("table {{.Name}}", false)},
`NAME
foobar_baz
foobar_bar
`,
},
{
formatter.Context{Format: NewFormat("table {{.Name}}", true)},
formatter.Context{Format: newFormat("table {{.Name}}", true)},
`NAME
foobar_baz
foobar_bar
@@ -119,7 +119,7 @@ foobar_bar
},
// Raw Format
{
formatter.Context{Format: NewFormat("raw", false)},
formatter.Context{Format: newFormat("raw", false)},
`network_id: networkID1
name: foobar_baz
driver: foo
@@ -133,21 +133,21 @@ scope: local
`,
},
{
formatter.Context{Format: NewFormat("raw", true)},
formatter.Context{Format: newFormat("raw", true)},
`network_id: networkID1
network_id: networkID2
`,
},
// Custom Format
{
formatter.Context{Format: NewFormat("{{.Name}}", false)},
formatter.Context{Format: newFormat("{{.Name}}", false)},
`foobar_baz
foobar_bar
`,
},
// Custom Format with CreatedAt
{
formatter.Context{Format: NewFormat("{{.Name}} {{.CreatedAt}}", false)},
formatter.Context{Format: newFormat("{{.Name}} {{.CreatedAt}}", false)},
`foobar_baz 2016-01-01 00:00:00 +0000 UTC
foobar_bar 2017-01-01 00:00:00 +0000 UTC
`,
@@ -166,7 +166,7 @@ foobar_bar 2017-01-01 00:00:00 +0000 UTC
t.Run(string(tc.context.Format), func(t *testing.T) {
var out bytes.Buffer
tc.context.Output = &out
err := FormatWrite(tc.context, networks)
err := formatWrite(tc.context, networks)
if err != nil {
assert.Error(t, err, tc.expected)
} else {
@@ -187,7 +187,7 @@ func TestNetworkContextWriteJSON(t *testing.T) {
}
out := bytes.NewBufferString("")
err := FormatWrite(formatter.Context{Format: "{{json .}}", Output: out}, networks)
err := formatWrite(formatter.Context{Format: "{{json .}}", Output: out}, networks)
if err != nil {
t.Fatal(err)
}
@@ -206,7 +206,7 @@ func TestNetworkContextWriteJSONField(t *testing.T) {
{ID: "networkID2", Name: "foobar_bar"},
}
out := bytes.NewBufferString("")
err := FormatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, networks)
err := formatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, networks)
if err != nil {
t.Fatal(err)
}

View File

@@ -67,8 +67,8 @@ func runList(ctx context.Context, dockerCli command.Cli, options listOptions) er
networksCtx := formatter.Context{
Output: dockerCli.Out(),
Format: NewFormat(format, options.quiet),
Format: newFormat(format, options.quiet),
Trunc: !options.noTrunc,
}
return FormatWrite(networksCtx, networkResources)
return formatWrite(networksCtx, networkResources)
}