Skip to content

Commit bba053c

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

File tree

8 files changed

+531
-532
lines changed

8 files changed

+531
-532
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 {

cli/command/orchestrator.go

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,84 @@
1-
package command
2-
3-
import (
4-
"fmt"
5-
"io"
6-
"os"
7-
)
8-
9-
// Orchestrator type acts as an enum describing supported orchestrators.
10-
type Orchestrator string
11-
12-
const (
13-
// OrchestratorKubernetes orchestrator
14-
OrchestratorKubernetes = Orchestrator("kubernetes")
15-
// OrchestratorSwarm orchestrator
16-
OrchestratorSwarm = Orchestrator("swarm")
17-
// OrchestratorAll orchestrator
18-
OrchestratorAll = Orchestrator("all")
19-
orchestratorUnset = Orchestrator("unset")
20-
21-
defaultOrchestrator = OrchestratorSwarm
22-
envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR"
23-
envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR"
24-
)
25-
26-
// HasKubernetes returns true if defined orchestrator has Kubernetes capabilities.
27-
func (o Orchestrator) HasKubernetes() bool {
28-
return o == OrchestratorKubernetes || o == OrchestratorAll
29-
}
30-
31-
// HasSwarm returns true if defined orchestrator has Swarm capabilities.
32-
func (o Orchestrator) HasSwarm() bool {
33-
return o == OrchestratorSwarm || o == OrchestratorAll
34-
}
35-
36-
// HasAll returns true if defined orchestrator has both Swarm and Kubernetes capabilities.
37-
func (o Orchestrator) HasAll() bool {
38-
return o == OrchestratorAll
39-
}
40-
41-
func normalize(value string) (Orchestrator, error) {
42-
switch value {
43-
case "kubernetes":
44-
return OrchestratorKubernetes, nil
45-
case "swarm":
46-
return OrchestratorSwarm, nil
47-
case "", "unset":
48-
return orchestratorUnset, nil
49-
case "all":
50-
return OrchestratorAll, nil
51-
default:
52-
return defaultOrchestrator, fmt.Errorf("specified orchestrator %q is invalid, please use either kubernetes, swarm or all", value)
53-
}
54-
}
55-
56-
// NormalizeOrchestrator parses an orchestrator value and checks if it is valid
57-
func NormalizeOrchestrator(value string) (Orchestrator, error) {
58-
return normalize(value)
59-
}
60-
61-
// GetStackOrchestrator checks DOCKER_STACK_ORCHESTRATOR environment variable and configuration file
62-
// orchestrator value and returns user defined Orchestrator.
63-
func GetStackOrchestrator(flagValue, contextValue, globalDefault string, stderr io.Writer) (Orchestrator, error) {
64-
// Check flag
65-
if o, err := normalize(flagValue); o != orchestratorUnset {
66-
return o, err
67-
}
68-
// Check environment variable
69-
env := os.Getenv(envVarDockerStackOrchestrator)
70-
if env == "" && os.Getenv(envVarDockerOrchestrator) != "" {
71-
fmt.Fprintf(stderr, "WARNING: experimental environment variable %s is set. Please use %s instead\n", envVarDockerOrchestrator, envVarDockerStackOrchestrator)
72-
}
73-
if o, err := normalize(env); o != orchestratorUnset {
74-
return o, err
75-
}
76-
if o, err := normalize(contextValue); o != orchestratorUnset {
77-
return o, err
78-
}
79-
if o, err := normalize(globalDefault); o != orchestratorUnset {
80-
return o, err
81-
}
82-
// Nothing set, use default orchestrator
83-
return defaultOrchestrator, nil
84-
}
1+
package command
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
)
8+
9+
// Orchestrator type acts as an enum describing supported orchestrators.
10+
type Orchestrator string
11+
12+
const (
13+
// OrchestratorKubernetes orchestrator
14+
OrchestratorKubernetes = Orchestrator("kubernetes")
15+
// OrchestratorSwarm orchestrator
16+
OrchestratorSwarm = Orchestrator("swarm")
17+
// OrchestratorAll orchestrator
18+
OrchestratorAll = Orchestrator("all")
19+
orchestratorUnset = Orchestrator("unset")
20+
21+
defaultOrchestrator = OrchestratorSwarm
22+
envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR"
23+
envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR"
24+
)
25+
26+
// HasKubernetes returns true if defined orchestrator has Kubernetes capabilities.
27+
func (o Orchestrator) HasKubernetes() bool {
28+
return o == OrchestratorKubernetes || o == OrchestratorAll
29+
}
30+
31+
// HasSwarm returns true if defined orchestrator has Swarm capabilities.
32+
func (o Orchestrator) HasSwarm() bool {
33+
return o == OrchestratorSwarm || o == OrchestratorAll
34+
}
35+
36+
// HasAll returns true if defined orchestrator has both Swarm and Kubernetes capabilities.
37+
func (o Orchestrator) HasAll() bool {
38+
return o == OrchestratorAll
39+
}
40+
41+
func normalize(value string) (Orchestrator, error) {
42+
switch value {
43+
case "kubernetes":
44+
return OrchestratorKubernetes, nil
45+
case "swarm":
46+
return OrchestratorSwarm, nil
47+
case "", "unset":
48+
return orchestratorUnset, nil
49+
case "all":
50+
return OrchestratorAll, nil
51+
default:
52+
return defaultOrchestrator, fmt.Errorf("specified orchestrator %q is invalid, please use either kubernetes, swarm or all", value)
53+
}
54+
}
55+
56+
// NormalizeOrchestrator parses an orchestrator value and checks if it is valid
57+
func NormalizeOrchestrator(value string) (Orchestrator, error) {
58+
return normalize(value)
59+
}
60+
61+
// GetStackOrchestrator checks DOCKER_STACK_ORCHESTRATOR environment variable and configuration file
62+
// orchestrator value and returns user defined Orchestrator.
63+
func GetStackOrchestrator(flagValue, contextValue, globalDefault string, stderr io.Writer) (Orchestrator, error) {
64+
// Check flag
65+
if o, err := normalize(flagValue); o != orchestratorUnset {
66+
return o, err
67+
}
68+
// Check environment variable
69+
env := os.Getenv(envVarDockerStackOrchestrator)
70+
if env == "" && os.Getenv(envVarDockerOrchestrator) != "" {
71+
fmt.Fprintf(stderr, "WARNING: experimental environment variable %s is set. Please use %s instead\n", envVarDockerOrchestrator, envVarDockerStackOrchestrator)
72+
}
73+
if o, err := normalize(env); o != orchestratorUnset {
74+
return o, err
75+
}
76+
if o, err := normalize(contextValue); o != orchestratorUnset {
77+
return o, err
78+
}
79+
if o, err := normalize(globalDefault); o != orchestratorUnset {
80+
return o, err
81+
}
82+
// Nothing set, use default orchestrator
83+
return defaultOrchestrator, nil
84+
}

0 commit comments

Comments
 (0)