Merge pull request #2700 from thaJeztah/unconvert

formatter: minor refactor to reduce needless conversions
This commit is contained in:
Silvin Lubecki 2020-08-31 09:46:11 +02:00 committed by GitHub
commit f784262d07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 {
if source == formatter.TableFormatKey {
if osType == winOSType {
return formatter.Format(winDefaultStatsTableFormat)
return winDefaultStatsTableFormat
}
return formatter.Format(defaultStatsTableFormat)
return defaultStatsTableFormat
}
return formatter.Format(source)
}

View File

@ -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"

View File

@ -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

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 {
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) ||