Skip to content

Commit

Permalink
List/Format refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Ferquel <simon.ferquel@docker.com>
  • Loading branch information
simonferquel committed Dec 17, 2018
1 parent c0c24d9 commit bba053c
Show file tree
Hide file tree
Showing 8 changed files with 531 additions and 532 deletions.
17 changes: 6 additions & 11 deletions cli/command/context/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
ctxformatter "github.com/docker/cli/cli/command/context/formatter"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/context/docker"
"github.com/docker/cli/cli/context/kubernetes"
Expand All @@ -32,7 +31,7 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
}

flags := cmd.Flags()
flags.StringVar(&opts.format, "format", "", "Pretty-print contexts using a Go template")
flags.StringVar(&opts.format, "format", formatter.TableFormatKey, "Pretty-print contexts using a Go template")
return cmd
}

Expand All @@ -42,7 +41,7 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
if err != nil {
return err
}
var contexts []*ctxformatter.ClientContext
var contexts []*formatter.ClientContext
for name, rawMeta := range contextMap {
meta, err := command.GetDockerContext(rawMeta)
if err != nil {
Expand All @@ -57,7 +56,7 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
if kubernetesEndpoint != nil {
kubEndpointText = fmt.Sprintf("%s (%s)", kubernetesEndpoint.Host, kubernetesEndpoint.DefaultNamespace)
}
desc := ctxformatter.ClientContext{
desc := formatter.ClientContext{
Name: name,
Current: name == curContext,
Description: meta.Description,
Expand All @@ -73,14 +72,10 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
return format(dockerCli, opts, contexts)
}

func format(dockerCli command.Cli, opts *listOptions, contexts []*ctxformatter.ClientContext) error {
format := opts.format
if format == "" {
format = ctxformatter.ClientContextTableFormat
}
func format(dockerCli command.Cli, opts *listOptions, contexts []*formatter.ClientContext) error {
contextCtx := formatter.Context{
Output: dockerCli.Out(),
Format: formatter.Format(format),
Format: formatter.NewClientContextFormat(opts.format),
}
return ctxformatter.ClientContextWrite(contextCtx, contexts)
return formatter.ClientContextWrite(contextCtx, contexts)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package formatter

import (
"github.com/docker/cli/cli/command/formatter"
)

const (
// ClientContextTableFormat is the default client context format
ClientContextTableFormat = "table {{.NameWithCurrent}}\t{{.Description}}\t{{.DockerEndpoint}}\t{{.KubernetesEndpoint}}\t{{.StackOrchestrator}}"
Expand All @@ -13,6 +9,14 @@ const (
stackOrchestrastorHeader = "ORCHESTRATOR"
)

// NewClientContextFormat returns a Format for rendering using a Context
func NewClientContextFormat(source string) Format {
if source == TableFormatKey {
return Format(ClientContextTableFormat)
}
return Format(source)
}

// ClientContext is a context for display
type ClientContext struct {
Name string
Expand All @@ -24,8 +28,8 @@ type ClientContext struct {
}

// ClientContextWrite writes formatted contexts using the Context
func ClientContextWrite(ctx formatter.Context, contexts []*ClientContext) error {
render := func(format func(subContext formatter.SubContext) error) error {
func ClientContextWrite(ctx Context, contexts []*ClientContext) error {
render := func(format func(subContext SubContext) error) error {
for _, context := range contexts {
if err := format(&clientContextContext{c: context}); err != nil {
return err
Expand All @@ -37,15 +41,15 @@ func ClientContextWrite(ctx formatter.Context, contexts []*ClientContext) error
}

type clientContextContext struct {
formatter.HeaderContext
HeaderContext
c *ClientContext
}

func newClientContextContext() *clientContextContext {
ctx := clientContextContext{}
ctx.Header = formatter.SubHeaderContext{
"NameWithCurrent": formatter.NameHeader,
"Description": formatter.DescriptionHeader,
ctx.Header = SubHeaderContext{
"NameWithCurrent": NameHeader,
"Description": DescriptionHeader,
"DockerEndpoint": dockerEndpointHeader,
"KubernetesEndpoint": kubernetesEndpointHeader,
"StackOrchestrator": stackOrchestrastorHeader,
Expand All @@ -54,7 +58,7 @@ func newClientContextContext() *clientContextContext {
}

func (c *clientContextContext) MarshalJSON() ([]byte, error) {
return formatter.MarshalJSON(c)
return MarshalJSON(c)
}

func (c *clientContextContext) NameWithCurrent() string {
Expand Down
168 changes: 84 additions & 84 deletions cli/command/orchestrator.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
package command

import (
"fmt"
"io"
"os"
)

// Orchestrator type acts as an enum describing supported orchestrators.
type Orchestrator string

const (
// OrchestratorKubernetes orchestrator
OrchestratorKubernetes = Orchestrator("kubernetes")
// OrchestratorSwarm orchestrator
OrchestratorSwarm = Orchestrator("swarm")
// OrchestratorAll orchestrator
OrchestratorAll = Orchestrator("all")
orchestratorUnset = Orchestrator("unset")

defaultOrchestrator = OrchestratorSwarm
envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR"
envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR"
)

// HasKubernetes returns true if defined orchestrator has Kubernetes capabilities.
func (o Orchestrator) HasKubernetes() bool {
return o == OrchestratorKubernetes || o == OrchestratorAll
}

// HasSwarm returns true if defined orchestrator has Swarm capabilities.
func (o Orchestrator) HasSwarm() bool {
return o == OrchestratorSwarm || o == OrchestratorAll
}

// HasAll returns true if defined orchestrator has both Swarm and Kubernetes capabilities.
func (o Orchestrator) HasAll() bool {
return o == OrchestratorAll
}

func normalize(value string) (Orchestrator, error) {
switch value {
case "kubernetes":
return OrchestratorKubernetes, nil
case "swarm":
return OrchestratorSwarm, nil
case "", "unset":
return orchestratorUnset, nil
case "all":
return OrchestratorAll, nil
default:
return defaultOrchestrator, fmt.Errorf("specified orchestrator %q is invalid, please use either kubernetes, swarm or all", value)
}
}

// NormalizeOrchestrator parses an orchestrator value and checks if it is valid
func NormalizeOrchestrator(value string) (Orchestrator, error) {
return normalize(value)
}

// GetStackOrchestrator checks DOCKER_STACK_ORCHESTRATOR environment variable and configuration file
// orchestrator value and returns user defined Orchestrator.
func GetStackOrchestrator(flagValue, contextValue, globalDefault string, stderr io.Writer) (Orchestrator, error) {
// Check flag
if o, err := normalize(flagValue); o != orchestratorUnset {
return o, err
}
// Check environment variable
env := os.Getenv(envVarDockerStackOrchestrator)
if env == "" && os.Getenv(envVarDockerOrchestrator) != "" {
fmt.Fprintf(stderr, "WARNING: experimental environment variable %s is set. Please use %s instead\n", envVarDockerOrchestrator, envVarDockerStackOrchestrator)
}
if o, err := normalize(env); o != orchestratorUnset {
return o, err
}
if o, err := normalize(contextValue); o != orchestratorUnset {
return o, err
}
if o, err := normalize(globalDefault); o != orchestratorUnset {
return o, err
}
// Nothing set, use default orchestrator
return defaultOrchestrator, nil
}
package command

import (
"fmt"
"io"
"os"
)

// Orchestrator type acts as an enum describing supported orchestrators.
type Orchestrator string

const (
// OrchestratorKubernetes orchestrator
OrchestratorKubernetes = Orchestrator("kubernetes")
// OrchestratorSwarm orchestrator
OrchestratorSwarm = Orchestrator("swarm")
// OrchestratorAll orchestrator
OrchestratorAll = Orchestrator("all")
orchestratorUnset = Orchestrator("unset")

defaultOrchestrator = OrchestratorSwarm
envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR"
envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR"
)

// HasKubernetes returns true if defined orchestrator has Kubernetes capabilities.
func (o Orchestrator) HasKubernetes() bool {
return o == OrchestratorKubernetes || o == OrchestratorAll
}

// HasSwarm returns true if defined orchestrator has Swarm capabilities.
func (o Orchestrator) HasSwarm() bool {
return o == OrchestratorSwarm || o == OrchestratorAll
}

// HasAll returns true if defined orchestrator has both Swarm and Kubernetes capabilities.
func (o Orchestrator) HasAll() bool {
return o == OrchestratorAll
}

func normalize(value string) (Orchestrator, error) {
switch value {
case "kubernetes":
return OrchestratorKubernetes, nil
case "swarm":
return OrchestratorSwarm, nil
case "", "unset":
return orchestratorUnset, nil
case "all":
return OrchestratorAll, nil
default:
return defaultOrchestrator, fmt.Errorf("specified orchestrator %q is invalid, please use either kubernetes, swarm or all", value)
}
}

// NormalizeOrchestrator parses an orchestrator value and checks if it is valid
func NormalizeOrchestrator(value string) (Orchestrator, error) {
return normalize(value)
}

// GetStackOrchestrator checks DOCKER_STACK_ORCHESTRATOR environment variable and configuration file
// orchestrator value and returns user defined Orchestrator.
func GetStackOrchestrator(flagValue, contextValue, globalDefault string, stderr io.Writer) (Orchestrator, error) {
// Check flag
if o, err := normalize(flagValue); o != orchestratorUnset {
return o, err
}
// Check environment variable
env := os.Getenv(envVarDockerStackOrchestrator)
if env == "" && os.Getenv(envVarDockerOrchestrator) != "" {
fmt.Fprintf(stderr, "WARNING: experimental environment variable %s is set. Please use %s instead\n", envVarDockerOrchestrator, envVarDockerStackOrchestrator)
}
if o, err := normalize(env); o != orchestratorUnset {
return o, err
}
if o, err := normalize(contextValue); o != orchestratorUnset {
return o, err
}
if o, err := normalize(globalDefault); o != orchestratorUnset {
return o, err
}
// Nothing set, use default orchestrator
return defaultOrchestrator, nil
}
Loading

0 comments on commit bba053c

Please sign in to comment.