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

cli/command/secret: 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>
This commit is contained in:
Sebastiaan van Stijn
2025-08-21 14:52:00 +02:00
parent 83371c2014
commit f3088e37a0
4 changed files with 35 additions and 14 deletions

View File

@@ -29,7 +29,14 @@ Updated at: {{.UpdatedAt}}`
)
// NewFormat returns a Format for rendering using a secret Context
//
// Deprecated: this function was only used internally and will be removed in the next release.
func NewFormat(source string, quiet bool) formatter.Format {
return newFormat(source, quiet)
}
// newFormat returns a Format for rendering using a secretContext.
func newFormat(source string, quiet bool) formatter.Format {
switch source {
case formatter.PrettyFormatKey:
return secretInspectPrettyTemplate
@@ -43,7 +50,14 @@ func NewFormat(source string, quiet bool) formatter.Format {
}
// FormatWrite writes the context
func FormatWrite(ctx formatter.Context, secrets []swarm.Secret) error {
//
// Deprecated: this function was only used internally and will be removed in the next release.
func FormatWrite(fmtCtx formatter.Context, secrets []swarm.Secret) error {
return formatWrite(fmtCtx, secrets)
}
// formatWrite writes the context
func formatWrite(fmtCtx formatter.Context, secrets []swarm.Secret) error {
render := func(format func(subContext formatter.SubContext) error) error {
for _, secret := range secrets {
secretCtx := &secretContext{s: secret}
@@ -53,7 +67,7 @@ func FormatWrite(ctx formatter.Context, secrets []swarm.Secret) error {
}
return nil
}
return ctx.Write(newSecretContext(), render)
return fmtCtx.Write(newSecretContext(), render)
}
func newSecretContext() *secretContext {
@@ -122,9 +136,16 @@ func (c *secretContext) Label(name string) string {
}
// InspectFormatWrite renders the context for a list of secrets
func InspectFormatWrite(ctx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
if ctx.Format != secretInspectPrettyTemplate {
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 inspect.GetRefFunc) error {
return inspectFormatWrite(fmtCtx, refs, getRef)
}
// inspectFormatWrite renders the context for a list of secrets.
func inspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
if fmtCtx.Format != secretInspectPrettyTemplate {
return inspect.Inspect(fmtCtx.Output, refs, string(fmtCtx.Format), getRef)
}
render := func(format func(subContext formatter.SubContext) error) error {
for _, ref := range refs {
@@ -142,7 +163,7 @@ func InspectFormatWrite(ctx formatter.Context, refs []string, getRef inspect.Get
}
return nil
}
return ctx.Write(&secretInspectContext{}, render)
return fmtCtx.Write(&secretInspectContext{}, render)
}
type secretInspectContext struct {

View File

@@ -27,21 +27,21 @@ func TestSecretContextFormatWrite(t *testing.T) {
},
// Table format
{
formatter.Context{Format: NewFormat("table", false)},
formatter.Context{Format: newFormat("table", false)},
`ID NAME DRIVER CREATED UPDATED
1 passwords Less than a second ago Less than a second ago
2 id_rsa Less than a second ago Less than a second ago
`,
},
{
formatter.Context{Format: NewFormat("table {{.Name}}", true)},
formatter.Context{Format: newFormat("table {{.Name}}", true)},
`NAME
passwords
id_rsa
`,
},
{
formatter.Context{Format: NewFormat("{{.ID}}-{{.Name}}", false)},
formatter.Context{Format: newFormat("{{.ID}}-{{.Name}}", false)},
`1-passwords
2-id_rsa
`,
@@ -65,7 +65,7 @@ id_rsa
var out bytes.Buffer
tc.context.Output = &out
if err := FormatWrite(tc.context, secrets); err != nil {
if err := formatWrite(tc.context, secrets); err != nil {
assert.Error(t, err, tc.expected)
} else {
assert.Equal(t, out.String(), tc.expected)

View File

@@ -61,10 +61,10 @@ func runSecretInspect(ctx context.Context, dockerCli command.Cli, opts inspectOp
secretCtx := formatter.Context{
Output: dockerCli.Out(),
Format: NewFormat(f, false),
Format: newFormat(f, false),
}
if err := InspectFormatWrite(secretCtx, opts.names, getRef); err != nil {
if err := inspectFormatWrite(secretCtx, opts.names, getRef); err != nil {
return cli.StatusError{StatusCode: 1, Status: err.Error()}
}
return nil

View File

@@ -66,7 +66,7 @@ func runSecretList(ctx context.Context, dockerCli command.Cli, options listOptio
secretCtx := formatter.Context{
Output: dockerCli.Out(),
Format: NewFormat(format, options.quiet),
Format: newFormat(format, options.quiet),
}
return FormatWrite(secretCtx, secrets)
return formatWrite(secretCtx, secrets)
}