Skip to content

Commit aa13ee6

Browse files
committed
List/Format refactoring
Signed-off-by: Simon Ferquel <simon.ferquel@docker.com>
1 parent c0c24d9 commit aa13ee6

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

cli/command/context/list.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/docker/cli/cli"
1010
"github.com/docker/cli/cli/command"
11-
ctxformatter "github.com/docker/cli/cli/command/context/formatter"
1211
"github.com/docker/cli/cli/command/formatter"
1312
"github.com/docker/cli/cli/context/docker"
1413
"github.com/docker/cli/cli/context/kubernetes"
@@ -32,7 +31,7 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
3231
}
3332

3433
flags := cmd.Flags()
35-
flags.StringVar(&opts.format, "format", "", "Pretty-print contexts using a Go template")
34+
flags.StringVar(&opts.format, "format", formatter.TableFormatKey, "Pretty-print contexts using a Go template")
3635
return cmd
3736
}
3837

@@ -42,7 +41,7 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
4241
if err != nil {
4342
return err
4443
}
45-
var contexts []*ctxformatter.ClientContext
44+
var contexts []*formatter.ClientContext
4645
for name, rawMeta := range contextMap {
4746
meta, err := command.GetDockerContext(rawMeta)
4847
if err != nil {
@@ -57,7 +56,7 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
5756
if kubernetesEndpoint != nil {
5857
kubEndpointText = fmt.Sprintf("%s (%s)", kubernetesEndpoint.Host, kubernetesEndpoint.DefaultNamespace)
5958
}
60-
desc := ctxformatter.ClientContext{
59+
desc := formatter.ClientContext{
6160
Name: name,
6261
Current: name == curContext,
6362
Description: meta.Description,
@@ -73,14 +72,10 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
7372
return format(dockerCli, opts, contexts)
7473
}
7574

76-
func format(dockerCli command.Cli, opts *listOptions, contexts []*ctxformatter.ClientContext) error {
77-
format := opts.format
78-
if format == "" {
79-
format = ctxformatter.ClientContextTableFormat
80-
}
75+
func format(dockerCli command.Cli, opts *listOptions, contexts []*formatter.ClientContext) error {
8176
contextCtx := formatter.Context{
8277
Output: dockerCli.Out(),
83-
Format: formatter.Format(format),
78+
Format: formatter.NewClientContextFormat(opts.format),
8479
}
85-
return ctxformatter.ClientContextWrite(contextCtx, contexts)
80+
return formatter.ClientContextWrite(contextCtx, contexts)
8681
}
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package formatter
22

3-
import (
4-
"github.com/docker/cli/cli/command/formatter"
5-
)
6-
73
const (
84
// ClientContextTableFormat is the default client context format
95
ClientContextTableFormat = "table {{.NameWithCurrent}}\t{{.Description}}\t{{.DockerEndpoint}}\t{{.KubernetesEndpoint}}\t{{.StackOrchestrator}}"
@@ -13,6 +9,14 @@ const (
139
stackOrchestrastorHeader = "ORCHESTRATOR"
1410
)
1511

12+
// NewClientContextFormat returns a Format for rendering using a Context
13+
func NewClientContextFormat(source string) Format {
14+
if source == TableFormatKey {
15+
return Format(ClientContextTableFormat)
16+
}
17+
return Format(source)
18+
}
19+
1620
// ClientContext is a context for display
1721
type ClientContext struct {
1822
Name string
@@ -24,8 +28,8 @@ type ClientContext struct {
2428
}
2529

2630
// ClientContextWrite writes formatted contexts using the Context
27-
func ClientContextWrite(ctx formatter.Context, contexts []*ClientContext) error {
28-
render := func(format func(subContext formatter.SubContext) error) error {
31+
func ClientContextWrite(ctx Context, contexts []*ClientContext) error {
32+
render := func(format func(subContext SubContext) error) error {
2933
for _, context := range contexts {
3034
if err := format(&clientContextContext{c: context}); err != nil {
3135
return err
@@ -37,15 +41,15 @@ func ClientContextWrite(ctx formatter.Context, contexts []*ClientContext) error
3741
}
3842

3943
type clientContextContext struct {
40-
formatter.HeaderContext
44+
HeaderContext
4145
c *ClientContext
4246
}
4347

4448
func newClientContextContext() *clientContextContext {
4549
ctx := clientContextContext{}
46-
ctx.Header = formatter.SubHeaderContext{
47-
"NameWithCurrent": formatter.NameHeader,
48-
"Description": formatter.DescriptionHeader,
50+
ctx.Header = SubHeaderContext{
51+
"NameWithCurrent": NameHeader,
52+
"Description": DescriptionHeader,
4953
"DockerEndpoint": dockerEndpointHeader,
5054
"KubernetesEndpoint": kubernetesEndpointHeader,
5155
"StackOrchestrator": stackOrchestrastorHeader,
@@ -54,7 +58,7 @@ func newClientContextContext() *clientContextContext {
5458
}
5559

5660
func (c *clientContextContext) MarshalJSON() ([]byte, error) {
57-
return formatter.MarshalJSON(c)
61+
return MarshalJSON(c)
5862
}
5963

6064
func (c *clientContextContext) NameWithCurrent() string {

0 commit comments

Comments
 (0)