Skip to content

Commit c3ee82f

Browse files
committed
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 <[email protected]>
1 parent 9f453d3 commit c3ee82f

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

cli/command/task/formatter.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ const (
2323
)
2424

2525
// NewTaskFormat returns a Format for rendering using a task Context
26+
//
27+
// Deprecated: this function was only used internally and will be removed in the next release.
2628
func NewTaskFormat(source string, quiet bool) formatter.Format {
29+
return newTaskFormat(source, quiet)
30+
}
31+
32+
// newTaskFormat returns a Format for rendering using a taskContext.
33+
func newTaskFormat(source string, quiet bool) formatter.Format {
2734
switch source {
2835
case formatter.TableFormatKey:
2936
if quiet {
@@ -40,10 +47,17 @@ func NewTaskFormat(source string, quiet bool) formatter.Format {
4047
}
4148

4249
// FormatWrite writes the context
43-
func FormatWrite(ctx formatter.Context, tasks []swarm.Task, names map[string]string, nodes map[string]string) error {
50+
//
51+
// Deprecated: this function was only used internally and will be removed in the next release.
52+
func FormatWrite(fmtCtx formatter.Context, tasks []swarm.Task, names map[string]string, nodes map[string]string) error {
53+
return formatWrite(fmtCtx, tasks, names, nodes)
54+
}
55+
56+
// formatWrite writes the context.
57+
func formatWrite(fmtCtx formatter.Context, tasks []swarm.Task, names map[string]string, nodes map[string]string) error {
4458
render := func(format func(subContext formatter.SubContext) error) error {
4559
for _, task := range tasks {
46-
taskCtx := &taskContext{trunc: ctx.Trunc, task: task, name: names[task.ID], node: nodes[task.ID]}
60+
taskCtx := &taskContext{trunc: fmtCtx.Trunc, task: task, name: names[task.ID], node: nodes[task.ID]}
4761
if err := format(taskCtx); err != nil {
4862
return err
4963
}
@@ -61,7 +75,7 @@ func FormatWrite(ctx formatter.Context, tasks []swarm.Task, names map[string]str
6175
"Error": formatter.ErrorHeader,
6276
"Ports": formatter.PortsHeader,
6377
}
64-
return ctx.Write(&taskCtx, render)
78+
return fmtCtx.Write(&taskCtx, render)
6579
}
6680

6781
type taskContext struct {

cli/command/task/formatter_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,30 @@ func TestTaskContextWrite(t *testing.T) {
2727
`template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
2828
},
2929
{
30-
formatter.Context{Format: NewTaskFormat("table", true)},
30+
formatter.Context{Format: newTaskFormat("table", true)},
3131
`taskID1
3232
taskID2
3333
`,
3434
},
3535
{
36-
formatter.Context{Format: NewTaskFormat("table {{.Name}}\t{{.Node}}\t{{.Ports}}", false)},
36+
formatter.Context{Format: newTaskFormat("table {{.Name}}\t{{.Node}}\t{{.Ports}}", false)},
3737
string(golden.Get(t, "task-context-write-table-custom.golden")),
3838
},
3939
{
40-
formatter.Context{Format: NewTaskFormat("table {{.Name}}", true)},
40+
formatter.Context{Format: newTaskFormat("table {{.Name}}", true)},
4141
`NAME
4242
foobar_baz
4343
foobar_bar
4444
`,
4545
},
4646
{
47-
formatter.Context{Format: NewTaskFormat("raw", true)},
47+
formatter.Context{Format: newTaskFormat("raw", true)},
4848
`id: taskID1
4949
id: taskID2
5050
`,
5151
},
5252
{
53-
formatter.Context{Format: NewTaskFormat("{{.Name}} {{.Node}}", false)},
53+
formatter.Context{Format: newTaskFormat("{{.Name}} {{.Node}}", false)},
5454
`foobar_baz foo1
5555
foobar_bar foo2
5656
`,
@@ -75,7 +75,7 @@ foobar_bar foo2
7575
var out bytes.Buffer
7676
tc.context.Output = &out
7777

78-
if err := FormatWrite(tc.context, tasks, names, nodes); err != nil {
78+
if err := formatWrite(tc.context, tasks, names, nodes); err != nil {
7979
assert.Error(t, err, tc.expected)
8080
} else {
8181
assert.Equal(t, out.String(), tc.expected)
@@ -94,7 +94,7 @@ func TestTaskContextWriteJSONField(t *testing.T) {
9494
"taskID2": "foobar_bar",
9595
}
9696
out := bytes.NewBufferString("")
97-
err := FormatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, tasks, names, map[string]string{})
97+
err := formatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, tasks, names, map[string]string{})
9898
if err != nil {
9999
t.Fatal(err)
100100
}

cli/command/task/print.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func Print(ctx context.Context, dockerCli command.Cli, tasks []swarm.Task, resol
5050

5151
tasksCtx := formatter.Context{
5252
Output: dockerCli.Out(),
53-
Format: NewTaskFormat(format, quiet),
53+
Format: newTaskFormat(format, quiet),
5454
Trunc: trunc,
5555
}
5656

@@ -75,7 +75,7 @@ func Print(ctx context.Context, dockerCli command.Cli, tasks []swarm.Task, resol
7575
nodes[task.ID] = nodeValue
7676
}
7777

78-
return FormatWrite(tasksCtx, tasks, names, nodes)
78+
return formatWrite(tasksCtx, tasks, names, nodes)
7979
}
8080

8181
// generateTaskNames generates names for the given tasks, and returns a copy of

0 commit comments

Comments
 (0)