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

cli/command/task: deprecate NewTaskFormat, 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 15:09:44 +02:00
parent 9f453d3fea
commit c3ee82fdc3
3 changed files with 26 additions and 12 deletions

View File

@@ -23,7 +23,14 @@ const (
)
// NewTaskFormat returns a Format for rendering using a task Context
//
// Deprecated: this function was only used internally and will be removed in the next release.
func NewTaskFormat(source string, quiet bool) formatter.Format {
return newTaskFormat(source, quiet)
}
// newTaskFormat returns a Format for rendering using a taskContext.
func newTaskFormat(source string, quiet bool) formatter.Format {
switch source {
case formatter.TableFormatKey:
if quiet {
@@ -40,10 +47,17 @@ func NewTaskFormat(source string, quiet bool) formatter.Format {
}
// FormatWrite writes the context
func FormatWrite(ctx formatter.Context, tasks []swarm.Task, names map[string]string, nodes map[string]string) error {
//
// Deprecated: this function was only used internally and will be removed in the next release.
func FormatWrite(fmtCtx formatter.Context, tasks []swarm.Task, names map[string]string, nodes map[string]string) error {
return formatWrite(fmtCtx, tasks, names, nodes)
}
// formatWrite writes the context.
func formatWrite(fmtCtx formatter.Context, tasks []swarm.Task, names map[string]string, nodes map[string]string) error {
render := func(format func(subContext formatter.SubContext) error) error {
for _, task := range tasks {
taskCtx := &taskContext{trunc: ctx.Trunc, task: task, name: names[task.ID], node: nodes[task.ID]}
taskCtx := &taskContext{trunc: fmtCtx.Trunc, task: task, name: names[task.ID], node: nodes[task.ID]}
if err := format(taskCtx); err != nil {
return err
}
@@ -61,7 +75,7 @@ func FormatWrite(ctx formatter.Context, tasks []swarm.Task, names map[string]str
"Error": formatter.ErrorHeader,
"Ports": formatter.PortsHeader,
}
return ctx.Write(&taskCtx, render)
return fmtCtx.Write(&taskCtx, render)
}
type taskContext struct {

View File

@@ -27,30 +27,30 @@ func TestTaskContextWrite(t *testing.T) {
`template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
},
{
formatter.Context{Format: NewTaskFormat("table", true)},
formatter.Context{Format: newTaskFormat("table", true)},
`taskID1
taskID2
`,
},
{
formatter.Context{Format: NewTaskFormat("table {{.Name}}\t{{.Node}}\t{{.Ports}}", false)},
formatter.Context{Format: newTaskFormat("table {{.Name}}\t{{.Node}}\t{{.Ports}}", false)},
string(golden.Get(t, "task-context-write-table-custom.golden")),
},
{
formatter.Context{Format: NewTaskFormat("table {{.Name}}", true)},
formatter.Context{Format: newTaskFormat("table {{.Name}}", true)},
`NAME
foobar_baz
foobar_bar
`,
},
{
formatter.Context{Format: NewTaskFormat("raw", true)},
formatter.Context{Format: newTaskFormat("raw", true)},
`id: taskID1
id: taskID2
`,
},
{
formatter.Context{Format: NewTaskFormat("{{.Name}} {{.Node}}", false)},
formatter.Context{Format: newTaskFormat("{{.Name}} {{.Node}}", false)},
`foobar_baz foo1
foobar_bar foo2
`,
@@ -75,7 +75,7 @@ foobar_bar foo2
var out bytes.Buffer
tc.context.Output = &out
if err := FormatWrite(tc.context, tasks, names, nodes); err != nil {
if err := formatWrite(tc.context, tasks, names, nodes); err != nil {
assert.Error(t, err, tc.expected)
} else {
assert.Equal(t, out.String(), tc.expected)
@@ -94,7 +94,7 @@ func TestTaskContextWriteJSONField(t *testing.T) {
"taskID2": "foobar_bar",
}
out := bytes.NewBufferString("")
err := FormatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, tasks, names, map[string]string{})
err := formatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, tasks, names, map[string]string{})
if err != nil {
t.Fatal(err)
}

View File

@@ -50,7 +50,7 @@ func Print(ctx context.Context, dockerCli command.Cli, tasks []swarm.Task, resol
tasksCtx := formatter.Context{
Output: dockerCli.Out(),
Format: NewTaskFormat(format, quiet),
Format: newTaskFormat(format, quiet),
Trunc: trunc,
}
@@ -75,7 +75,7 @@ func Print(ctx context.Context, dockerCli command.Cli, tasks []swarm.Task, resol
nodes[task.ID] = nodeValue
}
return FormatWrite(tasksCtx, tasks, names, nodes)
return formatWrite(tasksCtx, tasks, names, nodes)
}
// generateTaskNames generates names for the given tasks, and returns a copy of