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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-21 14:58:51 +02:00
parent f3088e37a0
commit 9f453d3fea
3 changed files with 26 additions and 12 deletions

View File

@ -196,7 +196,14 @@ Ports:
`
// NewFormat returns a Format for rendering using a Context
//
// Deprecated: this function was only used internally and will be removed in the next release.
func NewFormat(source string) formatter.Format {
return newFormat(source)
}
// newFormat returns a Format for rendering using a Context.
func newFormat(source string) formatter.Format {
switch source {
case formatter.PrettyFormatKey:
return serviceInspectPrettyTemplate
@ -218,9 +225,16 @@ func resolveNetworks(service swarm.Service, getNetwork inspect.GetRefFunc) map[s
}
// InspectFormatWrite renders the context for a list of services
func InspectFormatWrite(ctx formatter.Context, refs []string, getRef, getNetwork inspect.GetRefFunc) error {
if ctx.Format != serviceInspectPrettyTemplate {
return inspect.Inspect(ctx.Output, refs, string(ctx.Format), getRef)
//
// Deprecated: this function was only used internally and will be removed in the next release.
func InspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef, getNetwork inspect.GetRefFunc) error {
return inspectFormatWrite(fmtCtx, refs, getRef, getNetwork)
}
// inspectFormatWrite renders the context for a list of services
func inspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef, getNetwork inspect.GetRefFunc) error {
if fmtCtx.Format != serviceInspectPrettyTemplate {
return inspect.Inspect(fmtCtx.Output, refs, string(fmtCtx.Format), getRef)
}
render := func(format func(subContext formatter.SubContext) error) error {
for _, ref := range refs {
@ -238,7 +252,7 @@ func InspectFormatWrite(ctx formatter.Context, refs []string, getRef, getNetwork
}
return nil
}
return ctx.Write(&serviceInspectContext{}, render)
return fmtCtx.Write(&serviceInspectContext{}, render)
}
type serviceInspectContext struct {

View File

@ -97,10 +97,10 @@ func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions)
serviceCtx := formatter.Context{
Output: dockerCli.Out(),
Format: NewFormat(f),
Format: newFormat(f),
}
if err := InspectFormatWrite(serviceCtx, opts.refs, getRef, getNetwork); err != nil {
if err := inspectFormatWrite(serviceCtx, opts.refs, getRef, getNetwork); err != nil {
return cli.StatusError{StatusCode: 1, Status: err.Error()}
}
return nil

View File

@ -131,7 +131,7 @@ func formatServiceInspect(t *testing.T, format formatter.Format, now time.Time)
Format: format,
}
err := InspectFormatWrite(ctx, []string{"de179gar9d0o7ltdybungplod"},
err := inspectFormatWrite(ctx, []string{"de179gar9d0o7ltdybungplod"},
func(ref string) (any, []byte, error) {
return s, nil, nil
},
@ -149,12 +149,12 @@ func formatServiceInspect(t *testing.T, format formatter.Format, now time.Time)
}
func TestPrettyPrint(t *testing.T) {
s := formatServiceInspect(t, NewFormat("pretty"), time.Now())
s := formatServiceInspect(t, newFormat("pretty"), time.Now())
golden.Assert(t, s, "service-inspect-pretty.golden")
}
func TestPrettyPrintWithNoUpdateConfig(t *testing.T) {
s := formatServiceInspect(t, NewFormat("pretty"), time.Now())
s := formatServiceInspect(t, newFormat("pretty"), time.Now())
if strings.Contains(s, "UpdateStatus") {
t.Fatal("Pretty print failed before parsing UpdateStatus")
}
@ -167,8 +167,8 @@ func TestJSONFormatWithNoUpdateConfig(t *testing.T) {
now := time.Now()
// s1: [{"ID":..}]
// s2: {"ID":..}
s1 := formatServiceInspect(t, NewFormat(""), now)
s2 := formatServiceInspect(t, NewFormat("{{json .}}"), now)
s1 := formatServiceInspect(t, newFormat(""), now)
s2 := formatServiceInspect(t, newFormat("{{json .}}"), now)
var m1Wrap []map[string]any
if err := json.Unmarshal([]byte(s1), &m1Wrap); err != nil {
t.Fatal(err)
@ -185,7 +185,7 @@ func TestJSONFormatWithNoUpdateConfig(t *testing.T) {
}
func TestPrettyPrintWithConfigsAndSecrets(t *testing.T) {
s := formatServiceInspect(t, NewFormat("pretty"), time.Now())
s := formatServiceInspect(t, newFormat("pretty"), time.Now())
assert.Check(t, is.Contains(s, "Log Driver:"), "Pretty print missing Log Driver")
assert.Check(t, is.Contains(s, "Configs:"), "Pretty print missing configs")
assert.Check(t, is.Contains(s, "Secrets:"), "Pretty print missing secrets")