Skip to content

Commit f3088e3

Browse files
committed
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 <[email protected]>
1 parent 83371c2 commit f3088e3

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

cli/command/secret/formatter.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ Updated at: {{.UpdatedAt}}`
2929
)
3030

3131
// NewFormat returns a Format for rendering using a secret Context
32+
//
33+
// Deprecated: this function was only used internally and will be removed in the next release.
3234
func NewFormat(source string, quiet bool) formatter.Format {
35+
return newFormat(source, quiet)
36+
}
37+
38+
// newFormat returns a Format for rendering using a secretContext.
39+
func newFormat(source string, quiet bool) formatter.Format {
3340
switch source {
3441
case formatter.PrettyFormatKey:
3542
return secretInspectPrettyTemplate
@@ -43,7 +50,14 @@ func NewFormat(source string, quiet bool) formatter.Format {
4350
}
4451

4552
// FormatWrite writes the context
46-
func FormatWrite(ctx formatter.Context, secrets []swarm.Secret) error {
53+
//
54+
// Deprecated: this function was only used internally and will be removed in the next release.
55+
func FormatWrite(fmtCtx formatter.Context, secrets []swarm.Secret) error {
56+
return formatWrite(fmtCtx, secrets)
57+
}
58+
59+
// formatWrite writes the context
60+
func formatWrite(fmtCtx formatter.Context, secrets []swarm.Secret) error {
4761
render := func(format func(subContext formatter.SubContext) error) error {
4862
for _, secret := range secrets {
4963
secretCtx := &secretContext{s: secret}
@@ -53,7 +67,7 @@ func FormatWrite(ctx formatter.Context, secrets []swarm.Secret) error {
5367
}
5468
return nil
5569
}
56-
return ctx.Write(newSecretContext(), render)
70+
return fmtCtx.Write(newSecretContext(), render)
5771
}
5872

5973
func newSecretContext() *secretContext {
@@ -122,9 +136,16 @@ func (c *secretContext) Label(name string) string {
122136
}
123137

124138
// InspectFormatWrite renders the context for a list of secrets
125-
func InspectFormatWrite(ctx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
126-
if ctx.Format != secretInspectPrettyTemplate {
127-
return inspect.Inspect(ctx.Output, refs, string(ctx.Format), getRef)
139+
//
140+
// Deprecated: this function was only used internally and will be removed in the next release.
141+
func InspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
142+
return inspectFormatWrite(fmtCtx, refs, getRef)
143+
}
144+
145+
// inspectFormatWrite renders the context for a list of secrets.
146+
func inspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
147+
if fmtCtx.Format != secretInspectPrettyTemplate {
148+
return inspect.Inspect(fmtCtx.Output, refs, string(fmtCtx.Format), getRef)
128149
}
129150
render := func(format func(subContext formatter.SubContext) error) error {
130151
for _, ref := range refs {
@@ -142,7 +163,7 @@ func InspectFormatWrite(ctx formatter.Context, refs []string, getRef inspect.Get
142163
}
143164
return nil
144165
}
145-
return ctx.Write(&secretInspectContext{}, render)
166+
return fmtCtx.Write(&secretInspectContext{}, render)
146167
}
147168

148169
type secretInspectContext struct {

cli/command/secret/formatter_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ func TestSecretContextFormatWrite(t *testing.T) {
2727
},
2828
// Table format
2929
{
30-
formatter.Context{Format: NewFormat("table", false)},
30+
formatter.Context{Format: newFormat("table", false)},
3131
`ID NAME DRIVER CREATED UPDATED
3232
1 passwords Less than a second ago Less than a second ago
3333
2 id_rsa Less than a second ago Less than a second ago
3434
`,
3535
},
3636
{
37-
formatter.Context{Format: NewFormat("table {{.Name}}", true)},
37+
formatter.Context{Format: newFormat("table {{.Name}}", true)},
3838
`NAME
3939
passwords
4040
id_rsa
4141
`,
4242
},
4343
{
44-
formatter.Context{Format: NewFormat("{{.ID}}-{{.Name}}", false)},
44+
formatter.Context{Format: newFormat("{{.ID}}-{{.Name}}", false)},
4545
`1-passwords
4646
2-id_rsa
4747
`,
@@ -65,7 +65,7 @@ id_rsa
6565
var out bytes.Buffer
6666
tc.context.Output = &out
6767

68-
if err := FormatWrite(tc.context, secrets); err != nil {
68+
if err := formatWrite(tc.context, secrets); err != nil {
6969
assert.Error(t, err, tc.expected)
7070
} else {
7171
assert.Equal(t, out.String(), tc.expected)

cli/command/secret/inspect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ func runSecretInspect(ctx context.Context, dockerCli command.Cli, opts inspectOp
6161

6262
secretCtx := formatter.Context{
6363
Output: dockerCli.Out(),
64-
Format: NewFormat(f, false),
64+
Format: newFormat(f, false),
6565
}
6666

67-
if err := InspectFormatWrite(secretCtx, opts.names, getRef); err != nil {
67+
if err := inspectFormatWrite(secretCtx, opts.names, getRef); err != nil {
6868
return cli.StatusError{StatusCode: 1, Status: err.Error()}
6969
}
7070
return nil

cli/command/secret/ls.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func runSecretList(ctx context.Context, dockerCli command.Cli, options listOptio
6666

6767
secretCtx := formatter.Context{
6868
Output: dockerCli.Out(),
69-
Format: NewFormat(format, options.quiet),
69+
Format: newFormat(format, options.quiet),
7070
}
71-
return FormatWrite(secretCtx, secrets)
71+
return formatWrite(secretCtx, secrets)
7272
}

0 commit comments

Comments
 (0)