Skip to content

Commit 123ef81

Browse files
committed
cli/command/node: deprecate NewFormat, FormatWrite, InspectFormatWrite
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 e626f77 commit 123ef81

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

‎cli/command/node/formatter.go‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ TLS Info:
8080
)
8181

8282
// NewFormat returns a Format for rendering using a node Context
83+
//
84+
// Deprecated: this function was only used internally and will be removed in the next release.
8385
func NewFormat(source string, quiet bool) formatter.Format {
86+
return newFormat(source, quiet)
87+
}
88+
89+
// newFormat returns a Format for rendering using a nodeContext.
90+
func newFormat(source string, quiet bool) formatter.Format {
8491
switch source {
8592
case formatter.PrettyFormatKey:
8693
return nodeInspectPrettyTemplate
@@ -99,7 +106,14 @@ func NewFormat(source string, quiet bool) formatter.Format {
99106
}
100107

101108
// FormatWrite writes the context
102-
func FormatWrite(ctx formatter.Context, nodes []swarm.Node, info system.Info) error {
109+
//
110+
// Deprecated: this function was only used internally and will be removed in the next release.
111+
func FormatWrite(fmtCtx formatter.Context, nodes []swarm.Node, info system.Info) error {
112+
return formatWrite(fmtCtx, nodes, info)
113+
}
114+
115+
// formatWrite writes the context.
116+
func formatWrite(fmtCtx formatter.Context, nodes []swarm.Node, info system.Info) error {
103117
render := func(format func(subContext formatter.SubContext) error) error {
104118
for _, node := range nodes {
105119
nodeCtx := &nodeContext{n: node, info: info}
@@ -120,7 +134,7 @@ func FormatWrite(ctx formatter.Context, nodes []swarm.Node, info system.Info) er
120134
"EngineVersion": engineVersionHeader,
121135
"TLSStatus": tlsStatusHeader,
122136
}
123-
return ctx.Write(&nodeCtx, render)
137+
return fmtCtx.Write(&nodeCtx, render)
124138
}
125139

126140
type nodeContext struct {
@@ -180,9 +194,16 @@ func (c *nodeContext) EngineVersion() string {
180194
}
181195

182196
// InspectFormatWrite renders the context for a list of nodes
183-
func InspectFormatWrite(ctx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
184-
if ctx.Format != nodeInspectPrettyTemplate {
185-
return inspect.Inspect(ctx.Output, refs, string(ctx.Format), getRef)
197+
//
198+
// Deprecated: this function was only used internally and will be removed in the next release.
199+
func InspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
200+
return inspectFormatWrite(fmtCtx, refs, getRef)
201+
}
202+
203+
// inspectFormatWrite renders the context for a list of nodes.
204+
func inspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
205+
if fmtCtx.Format != nodeInspectPrettyTemplate {
206+
return inspect.Inspect(fmtCtx.Output, refs, string(fmtCtx.Format), getRef)
186207
}
187208
render := func(format func(subContext formatter.SubContext) error) error {
188209
for _, ref := range refs {
@@ -200,7 +221,7 @@ func InspectFormatWrite(ctx formatter.Context, refs []string, getRef inspect.Get
200221
}
201222
return nil
202223
}
203-
return ctx.Write(&nodeInspectContext{}, render)
224+
return fmtCtx.Write(&nodeInspectContext{}, render)
204225
}
205226

206227
type nodeInspectContext struct {

‎cli/command/node/formatter_test.go‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,23 @@ func TestNodeContextWrite(t *testing.T) {
7474
},
7575
// Table format
7676
{
77-
context: formatter.Context{Format: NewFormat("table", false)},
77+
context: formatter.Context{Format: newFormat("table", false)},
7878
expected: `ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
7979
nodeID1 foobar_baz Foo Drain Leader 18.03.0-ce
8080
nodeID2 foobar_bar Bar Active Reachable 1.2.3
8181
nodeID3 foobar_boo Boo Active ` + "\n", // (to preserve whitespace)
8282
clusterInfo: swarm.ClusterInfo{TLSInfo: swarm.TLSInfo{TrustRoot: "hi"}},
8383
},
8484
{
85-
context: formatter.Context{Format: NewFormat("table", true)},
85+
context: formatter.Context{Format: newFormat("table", true)},
8686
expected: `nodeID1
8787
nodeID2
8888
nodeID3
8989
`,
9090
clusterInfo: swarm.ClusterInfo{TLSInfo: swarm.TLSInfo{TrustRoot: "hi"}},
9191
},
9292
{
93-
context: formatter.Context{Format: NewFormat("table {{.Hostname}}", false)},
93+
context: formatter.Context{Format: newFormat("table {{.Hostname}}", false)},
9494
expected: `HOSTNAME
9595
foobar_baz
9696
foobar_bar
@@ -99,7 +99,7 @@ foobar_boo
9999
clusterInfo: swarm.ClusterInfo{TLSInfo: swarm.TLSInfo{TrustRoot: "hi"}},
100100
},
101101
{
102-
context: formatter.Context{Format: NewFormat("table {{.Hostname}}", true)},
102+
context: formatter.Context{Format: newFormat("table {{.Hostname}}", true)},
103103
expected: `HOSTNAME
104104
foobar_baz
105105
foobar_bar
@@ -108,7 +108,7 @@ foobar_boo
108108
clusterInfo: swarm.ClusterInfo{TLSInfo: swarm.TLSInfo{TrustRoot: "hi"}},
109109
},
110110
{
111-
context: formatter.Context{Format: NewFormat("table {{.ID}}\t{{.Hostname}}\t{{.TLSStatus}}", false)},
111+
context: formatter.Context{Format: newFormat("table {{.ID}}\t{{.Hostname}}\t{{.TLSStatus}}", false)},
112112
expected: `ID HOSTNAME TLS STATUS
113113
nodeID1 foobar_baz Needs Rotation
114114
nodeID2 foobar_bar Ready
@@ -117,7 +117,7 @@ nodeID3 foobar_boo Unknown
117117
clusterInfo: swarm.ClusterInfo{TLSInfo: swarm.TLSInfo{TrustRoot: "hi"}},
118118
},
119119
{ // no cluster TLS status info, TLS status for all nodes is unknown
120-
context: formatter.Context{Format: NewFormat("table {{.ID}}\t{{.Hostname}}\t{{.TLSStatus}}", false)},
120+
context: formatter.Context{Format: newFormat("table {{.ID}}\t{{.Hostname}}\t{{.TLSStatus}}", false)},
121121
expected: `ID HOSTNAME TLS STATUS
122122
nodeID1 foobar_baz Unknown
123123
nodeID2 foobar_bar Unknown
@@ -127,7 +127,7 @@ nodeID3 foobar_boo Unknown
127127
},
128128
// Raw Format
129129
{
130-
context: formatter.Context{Format: NewFormat("raw", false)},
130+
context: formatter.Context{Format: newFormat("raw", false)},
131131
expected: `node_id: nodeID1
132132
hostname: foobar_baz
133133
status: Foo
@@ -148,7 +148,7 @@ manager_status: ` + "\n\n", // to preserve whitespace
148148
clusterInfo: swarm.ClusterInfo{TLSInfo: swarm.TLSInfo{TrustRoot: "hi"}},
149149
},
150150
{
151-
context: formatter.Context{Format: NewFormat("raw", true)},
151+
context: formatter.Context{Format: newFormat("raw", true)},
152152
expected: `node_id: nodeID1
153153
node_id: nodeID2
154154
node_id: nodeID3
@@ -157,7 +157,7 @@ node_id: nodeID3
157157
},
158158
// Custom Format
159159
{
160-
context: formatter.Context{Format: NewFormat("{{.Hostname}} {{.TLSStatus}}", false)},
160+
context: formatter.Context{Format: newFormat("{{.Hostname}} {{.TLSStatus}}", false)},
161161
expected: `foobar_baz Needs Rotation
162162
foobar_bar Ready
163163
foobar_boo Unknown
@@ -205,7 +205,7 @@ foobar_boo Unknown
205205
var out bytes.Buffer
206206
tc.context.Output = &out
207207

208-
err := FormatWrite(tc.context, nodes, system.Info{Swarm: swarm.Info{Cluster: &tc.clusterInfo}})
208+
err := formatWrite(tc.context, nodes, system.Info{Swarm: swarm.Info{Cluster: &tc.clusterInfo}})
209209
if err != nil {
210210
assert.Error(t, err, tc.expected)
211211
} else {
@@ -252,7 +252,7 @@ func TestNodeContextWriteJSON(t *testing.T) {
252252
{ID: "nodeID3", Description: swarm.NodeDescription{Hostname: "foobar_boo", Engine: swarm.EngineDescription{EngineVersion: "18.03.0-ce"}}},
253253
}
254254
out := bytes.NewBufferString("")
255-
err := FormatWrite(formatter.Context{Format: "{{json .}}", Output: out}, nodes, testcase.info)
255+
err := formatWrite(formatter.Context{Format: "{{json .}}", Output: out}, nodes, testcase.info)
256256
if err != nil {
257257
t.Fatal(err)
258258
}
@@ -272,7 +272,7 @@ func TestNodeContextWriteJSONField(t *testing.T) {
272272
{ID: "nodeID2", Description: swarm.NodeDescription{Hostname: "foobar_bar"}},
273273
}
274274
out := bytes.NewBufferString("")
275-
err := FormatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, nodes, system.Info{})
275+
err := formatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, nodes, system.Info{})
276276
if err != nil {
277277
t.Fatal(err)
278278
}
@@ -317,10 +317,10 @@ func TestNodeInspectWriteContext(t *testing.T) {
317317
}
318318
out := bytes.NewBufferString("")
319319
context := formatter.Context{
320-
Format: NewFormat("pretty", false),
320+
Format: newFormat("pretty", false),
321321
Output: out,
322322
}
323-
err := InspectFormatWrite(context, []string{"nodeID1"}, func(string) (any, []byte, error) {
323+
err := inspectFormatWrite(context, []string{"nodeID1"}, func(string) (any, []byte, error) {
324324
return node, nil, nil
325325
})
326326
if err != nil {

‎cli/command/node/inspect.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions)
6666

6767
nodeCtx := formatter.Context{
6868
Output: dockerCli.Out(),
69-
Format: NewFormat(f, false),
69+
Format: newFormat(f, false),
7070
}
7171

72-
if err := InspectFormatWrite(nodeCtx, opts.nodeIds, getRef); err != nil {
72+
if err := inspectFormatWrite(nodeCtx, opts.nodeIds, getRef); err != nil {
7373
return cli.StatusError{StatusCode: 1, Status: err.Error()}
7474
}
7575
return nil

‎cli/command/node/list.go‎

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

8080
nodesCtx := formatter.Context{
8181
Output: dockerCli.Out(),
82-
Format: NewFormat(format, options.quiet),
82+
Format: newFormat(format, options.quiet),
8383
}
8484
sort.Slice(nodes, func(i, j int) bool {
8585
return sortorder.NaturalLess(nodes[i].Description.Hostname, nodes[j].Description.Hostname)
8686
})
87-
return FormatWrite(nodesCtx, nodes, info)
87+
return formatWrite(nodesCtx, nodes, info)
8888
}

0 commit comments

Comments
 (0)