From b951b5b8c9c0c8b46ad82c1f10e181d546d97983 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 28 Aug 2020 23:09:27 +0200 Subject: [PATCH] formatter: minor refactor to reduce needless conversions Signed-off-by: Sebastiaan van Stijn --- cli/command/container/formatter_stats.go | 4 ++-- cli/command/stack/formatter/formatter.go | 5 +++-- cli/command/stack/formatter/formatter_test.go | 4 ++-- cli/command/stack/list.go | 4 ++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/cli/command/container/formatter_stats.go b/cli/command/container/formatter_stats.go index 565de4470b..80fdd586d6 100644 --- a/cli/command/container/formatter_stats.go +++ b/cli/command/container/formatter_stats.go @@ -103,9 +103,9 @@ func (cs *Stats) GetStatistics() StatsEntry { func NewStatsFormat(source, osType string) formatter.Format { if source == formatter.TableFormatKey { if osType == winOSType { - return formatter.Format(winDefaultStatsTableFormat) + return winDefaultStatsTableFormat } - return formatter.Format(defaultStatsTableFormat) + return defaultStatsTableFormat } return formatter.Format(source) } diff --git a/cli/command/stack/formatter/formatter.go b/cli/command/stack/formatter/formatter.go index 7f1cb82d54..c52d9309c8 100644 --- a/cli/command/stack/formatter/formatter.go +++ b/cli/command/stack/formatter/formatter.go @@ -8,9 +8,10 @@ import ( const ( // KubernetesStackTableFormat is the default Kubernetes stack format - KubernetesStackTableFormat = "table {{.Name}}\t{{.Services}}\t{{.Orchestrator}}\t{{.Namespace}}" + KubernetesStackTableFormat formatter.Format = "table {{.Name}}\t{{.Services}}\t{{.Orchestrator}}\t{{.Namespace}}" + // SwarmStackTableFormat is the default Swarm stack format - SwarmStackTableFormat = "table {{.Name}}\t{{.Services}}\t{{.Orchestrator}}" + SwarmStackTableFormat formatter.Format = "table {{.Name}}\t{{.Services}}\t{{.Orchestrator}}" stackServicesHeader = "SERVICES" stackOrchestrastorHeader = "ORCHESTRATOR" diff --git a/cli/command/stack/formatter/formatter_test.go b/cli/command/stack/formatter/formatter_test.go index ed878a67a2..519cd287b9 100644 --- a/cli/command/stack/formatter/formatter_test.go +++ b/cli/command/stack/formatter/formatter_test.go @@ -27,7 +27,7 @@ func TestStackContextWrite(t *testing.T) { }, // Table format { - formatter.Context{Format: formatter.Format(SwarmStackTableFormat)}, + formatter.Context{Format: SwarmStackTableFormat}, `NAME SERVICES ORCHESTRATOR baz 2 orchestrator1 bar 1 orchestrator2 @@ -35,7 +35,7 @@ bar 1 orchestrator2 }, // Kubernetes table format adds Namespace column { - formatter.Context{Format: formatter.Format(KubernetesStackTableFormat)}, + formatter.Context{Format: KubernetesStackTableFormat}, `NAME SERVICES ORCHESTRATOR NAMESPACE baz 2 orchestrator1 namespace1 bar 1 orchestrator2 namespace2 diff --git a/cli/command/stack/list.go b/cli/command/stack/list.go index 5793bfdca6..5175ea371c 100644 --- a/cli/command/stack/list.go +++ b/cli/command/stack/list.go @@ -60,7 +60,7 @@ func RunList(cmd *cobra.Command, dockerCli command.Cli, opts options.List, orche } func format(dockerCli command.Cli, opts options.List, orchestrator command.Orchestrator, stacks []*formatter.Stack) error { - format := opts.Format + format := formatter.Format(opts.Format) if format == "" || format == formatter.TableFormatKey { format = formatter.SwarmStackTableFormat if orchestrator.HasKubernetes() { @@ -69,7 +69,7 @@ func format(dockerCli command.Cli, opts options.List, orchestrator command.Orche } stackCtx := formatter.Context{ Output: dockerCli.Out(), - Format: formatter.Format(format), + Format: format, } sort.Slice(stacks, func(i, j int) bool { return sortorder.NaturalLess(stacks[i].Name, stacks[j].Name) ||