Skip to content

Commit 9f453d3

Browse files
committed
cli/command/service: deprecate NewFormat, 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 <[email protected]>
1 parent f3088e3 commit 9f453d3

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

cli/command/service/formatter.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,14 @@ Ports:
196196
`
197197

198198
// NewFormat returns a Format for rendering using a Context
199+
//
200+
// Deprecated: this function was only used internally and will be removed in the next release.
199201
func NewFormat(source string) formatter.Format {
202+
return newFormat(source)
203+
}
204+
205+
// newFormat returns a Format for rendering using a Context.
206+
func newFormat(source string) formatter.Format {
200207
switch source {
201208
case formatter.PrettyFormatKey:
202209
return serviceInspectPrettyTemplate
@@ -218,9 +225,16 @@ func resolveNetworks(service swarm.Service, getNetwork inspect.GetRefFunc) map[s
218225
}
219226

220227
// InspectFormatWrite renders the context for a list of services
221-
func InspectFormatWrite(ctx formatter.Context, refs []string, getRef, getNetwork inspect.GetRefFunc) error {
222-
if ctx.Format != serviceInspectPrettyTemplate {
223-
return inspect.Inspect(ctx.Output, refs, string(ctx.Format), getRef)
228+
//
229+
// Deprecated: this function was only used internally and will be removed in the next release.
230+
func InspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef, getNetwork inspect.GetRefFunc) error {
231+
return inspectFormatWrite(fmtCtx, refs, getRef, getNetwork)
232+
}
233+
234+
// inspectFormatWrite renders the context for a list of services
235+
func inspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef, getNetwork inspect.GetRefFunc) error {
236+
if fmtCtx.Format != serviceInspectPrettyTemplate {
237+
return inspect.Inspect(fmtCtx.Output, refs, string(fmtCtx.Format), getRef)
224238
}
225239
render := func(format func(subContext formatter.SubContext) error) error {
226240
for _, ref := range refs {
@@ -238,7 +252,7 @@ func InspectFormatWrite(ctx formatter.Context, refs []string, getRef, getNetwork
238252
}
239253
return nil
240254
}
241-
return ctx.Write(&serviceInspectContext{}, render)
255+
return fmtCtx.Write(&serviceInspectContext{}, render)
242256
}
243257

244258
type serviceInspectContext struct {

cli/command/service/inspect.go

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

9898
serviceCtx := formatter.Context{
9999
Output: dockerCli.Out(),
100-
Format: NewFormat(f),
100+
Format: newFormat(f),
101101
}
102102

103-
if err := InspectFormatWrite(serviceCtx, opts.refs, getRef, getNetwork); err != nil {
103+
if err := inspectFormatWrite(serviceCtx, opts.refs, getRef, getNetwork); err != nil {
104104
return cli.StatusError{StatusCode: 1, Status: err.Error()}
105105
}
106106
return nil

cli/command/service/inspect_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func formatServiceInspect(t *testing.T, format formatter.Format, now time.Time)
131131
Format: format,
132132
}
133133

134-
err := InspectFormatWrite(ctx, []string{"de179gar9d0o7ltdybungplod"},
134+
err := inspectFormatWrite(ctx, []string{"de179gar9d0o7ltdybungplod"},
135135
func(ref string) (any, []byte, error) {
136136
return s, nil, nil
137137
},
@@ -149,12 +149,12 @@ func formatServiceInspect(t *testing.T, format formatter.Format, now time.Time)
149149
}
150150

151151
func TestPrettyPrint(t *testing.T) {
152-
s := formatServiceInspect(t, NewFormat("pretty"), time.Now())
152+
s := formatServiceInspect(t, newFormat("pretty"), time.Now())
153153
golden.Assert(t, s, "service-inspect-pretty.golden")
154154
}
155155

156156
func TestPrettyPrintWithNoUpdateConfig(t *testing.T) {
157-
s := formatServiceInspect(t, NewFormat("pretty"), time.Now())
157+
s := formatServiceInspect(t, newFormat("pretty"), time.Now())
158158
if strings.Contains(s, "UpdateStatus") {
159159
t.Fatal("Pretty print failed before parsing UpdateStatus")
160160
}
@@ -167,8 +167,8 @@ func TestJSONFormatWithNoUpdateConfig(t *testing.T) {
167167
now := time.Now()
168168
// s1: [{"ID":..}]
169169
// s2: {"ID":..}
170-
s1 := formatServiceInspect(t, NewFormat(""), now)
171-
s2 := formatServiceInspect(t, NewFormat("{{json .}}"), now)
170+
s1 := formatServiceInspect(t, newFormat(""), now)
171+
s2 := formatServiceInspect(t, newFormat("{{json .}}"), now)
172172
var m1Wrap []map[string]any
173173
if err := json.Unmarshal([]byte(s1), &m1Wrap); err != nil {
174174
t.Fatal(err)
@@ -185,7 +185,7 @@ func TestJSONFormatWithNoUpdateConfig(t *testing.T) {
185185
}
186186

187187
func TestPrettyPrintWithConfigsAndSecrets(t *testing.T) {
188-
s := formatServiceInspect(t, NewFormat("pretty"), time.Now())
188+
s := formatServiceInspect(t, newFormat("pretty"), time.Now())
189189
assert.Check(t, is.Contains(s, "Log Driver:"), "Pretty print missing Log Driver")
190190
assert.Check(t, is.Contains(s, "Configs:"), "Pretty print missing configs")
191191
assert.Check(t, is.Contains(s, "Secrets:"), "Pretty print missing secrets")

0 commit comments

Comments
 (0)