formatter: minor refactor to reduce needless conversions

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-08-28 23:09:27 +02:00
parent 8fc038afe7
commit b951b5b8c9
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 9 additions and 8 deletions

View File

@ -103,9 +103,9 @@ func (cs *Stats) GetStatistics() StatsEntry {
func NewStatsFormat(source, osType string) formatter.Format { func NewStatsFormat(source, osType string) formatter.Format {
if source == formatter.TableFormatKey { if source == formatter.TableFormatKey {
if osType == winOSType { if osType == winOSType {
return formatter.Format(winDefaultStatsTableFormat) return winDefaultStatsTableFormat
} }
return formatter.Format(defaultStatsTableFormat) return defaultStatsTableFormat
} }
return formatter.Format(source) return formatter.Format(source)
} }

View File

@ -8,9 +8,10 @@ import (
const ( const (
// KubernetesStackTableFormat is the default Kubernetes stack format // 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 is the default Swarm stack format
SwarmStackTableFormat = "table {{.Name}}\t{{.Services}}\t{{.Orchestrator}}" SwarmStackTableFormat formatter.Format = "table {{.Name}}\t{{.Services}}\t{{.Orchestrator}}"
stackServicesHeader = "SERVICES" stackServicesHeader = "SERVICES"
stackOrchestrastorHeader = "ORCHESTRATOR" stackOrchestrastorHeader = "ORCHESTRATOR"

View File

@ -27,7 +27,7 @@ func TestStackContextWrite(t *testing.T) {
}, },
// Table format // Table format
{ {
formatter.Context{Format: formatter.Format(SwarmStackTableFormat)}, formatter.Context{Format: SwarmStackTableFormat},
`NAME SERVICES ORCHESTRATOR `NAME SERVICES ORCHESTRATOR
baz 2 orchestrator1 baz 2 orchestrator1
bar 1 orchestrator2 bar 1 orchestrator2
@ -35,7 +35,7 @@ bar 1 orchestrator2
}, },
// Kubernetes table format adds Namespace column // Kubernetes table format adds Namespace column
{ {
formatter.Context{Format: formatter.Format(KubernetesStackTableFormat)}, formatter.Context{Format: KubernetesStackTableFormat},
`NAME SERVICES ORCHESTRATOR NAMESPACE `NAME SERVICES ORCHESTRATOR NAMESPACE
baz 2 orchestrator1 namespace1 baz 2 orchestrator1 namespace1
bar 1 orchestrator2 namespace2 bar 1 orchestrator2 namespace2

View File

@ -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 { 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 { if format == "" || format == formatter.TableFormatKey {
format = formatter.SwarmStackTableFormat format = formatter.SwarmStackTableFormat
if orchestrator.HasKubernetes() { if orchestrator.HasKubernetes() {
@ -69,7 +69,7 @@ func format(dockerCli command.Cli, opts options.List, orchestrator command.Orche
} }
stackCtx := formatter.Context{ stackCtx := formatter.Context{
Output: dockerCli.Out(), Output: dockerCli.Out(),
Format: formatter.Format(format), Format: format,
} }
sort.Slice(stacks, func(i, j int) bool { sort.Slice(stacks, func(i, j int) bool {
return sortorder.NaturalLess(stacks[i].Name, stacks[j].Name) || return sortorder.NaturalLess(stacks[i].Name, stacks[j].Name) ||