Skip to content

Commit 085d5c2

Browse files
authored
Merge pull request #4343 from thaJeztah/cleanup_sprintf
replace some basic uses of fmt.Sprintf(), and minor refactor
2 parents c96484a + d68b361 commit 085d5c2

9 files changed

Lines changed: 12 additions & 18 deletions

File tree

cli/command/config/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (c *configContext) Labels() string {
103103
}
104104
var joinLabels []string
105105
for k, v := range mapLabels {
106-
joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
106+
joinLabels = append(joinLabels, k+"="+v)
107107
}
108108
return strings.Join(joinLabels, ",")
109109
}

cli/command/container/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, options *createOptio
9191
if v == nil {
9292
newEnv = append(newEnv, k)
9393
} else {
94-
newEnv = append(newEnv, fmt.Sprintf("%s=%s", k, *v))
94+
newEnv = append(newEnv, k+"="+*v)
9595
}
9696
}
9797
copts.env = *opts.NewListOptsRef(&newEnv, nil)

cli/command/container/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func runRun(dockerCli command.Cli, flags *pflag.FlagSet, ropts *runOptions, copt
101101
if v == nil {
102102
newEnv = append(newEnv, k)
103103
} else {
104-
newEnv = append(newEnv, fmt.Sprintf("%s=%s", k, *v))
104+
newEnv = append(newEnv, k+"="+*v)
105105
}
106106
}
107107
copts.env = *opts.NewListOptsRef(&newEnv, nil)

cli/command/formatter/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func (c *ContainerContext) Labels() string {
247247

248248
var joinLabels []string
249249
for k, v := range c.c.Labels {
250-
joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
250+
joinLabels = append(joinLabels, k+"="+v)
251251
}
252252
return strings.Join(joinLabels, ",")
253253
}

cli/command/network/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (c *networkContext) Labels() string {
103103

104104
var joinLabels []string
105105
for k, v := range c.n.Labels {
106-
joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
106+
joinLabels = append(joinLabels, k+"="+v)
107107
}
108108
return strings.Join(joinLabels, ",")
109109
}

cli/command/secret/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (c *secretContext) Labels() string {
110110
}
111111
var joinLabels []string
112112
for k, v := range mapLabels {
113-
joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
113+
joinLabels = append(joinLabels, k+"="+v)
114114
}
115115
return strings.Join(joinLabels, ",")
116116
}

cli/command/system/events.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func prettyPrintEvent(out io.Writer, event eventtypes.Message) error {
133133
sort.Strings(keys)
134134
for _, k := range keys {
135135
v := event.Actor.Attributes[k]
136-
attrs = append(attrs, fmt.Sprintf("%s=%s", k, v))
136+
attrs = append(attrs, k+"="+v)
137137
}
138138
fmt.Fprintf(out, " (%s)", strings.Join(attrs, ", "))
139139
}

cli/compose/convert/service.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package convert
22

33
import (
4-
"fmt"
54
"os"
65
"sort"
76
"strings"
@@ -134,7 +133,7 @@ func Service(
134133
Hosts: convertExtraHosts(service.ExtraHosts),
135134
DNSConfig: dnsConfig,
136135
Healthcheck: healthcheck,
137-
Env: sortStrings(convertEnvironment(service.Environment)),
136+
Env: convertEnvironment(service.Environment),
138137
Labels: AddStackLabel(namespace, service.Labels),
139138
Dir: service.WorkingDir,
140139
User: service.User,
@@ -200,11 +199,6 @@ func getPlacementPreference(preferences []composetypes.PlacementPreferences) []s
200199
return result
201200
}
202201

203-
func sortStrings(strs []string) []string {
204-
sort.Strings(strs)
205-
return strs
206-
}
207-
208202
func convertServiceNetworks(
209203
networks map[string]*composetypes.ServiceNetworkConfig,
210204
networkConfigs networkMap,
@@ -597,6 +591,8 @@ func convertEndpointSpec(endpointMode string, source []composetypes.ServicePortC
597591
}
598592
}
599593

594+
// convertEnvironment converts key/value mappings to a slice, and sorts
595+
// the results.
600596
func convertEnvironment(source map[string]*string) []string {
601597
var output []string
602598

@@ -605,10 +601,10 @@ func convertEnvironment(source map[string]*string) []string {
605601
case nil:
606602
output = append(output, name)
607603
default:
608-
output = append(output, fmt.Sprintf("%s=%s", name, *value))
604+
output = append(output, name+"="+*value)
609605
}
610606
}
611-
607+
sort.Strings(output)
612608
return output
613609
}
614610

cli/compose/convert/service_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package convert
33
import (
44
"context"
55
"os"
6-
"sort"
76
"strings"
87
"testing"
98
"time"
@@ -59,7 +58,6 @@ func TestConvertEnvironment(t *testing.T) {
5958
"key": strPtr("value"),
6059
}
6160
env := convertEnvironment(source)
62-
sort.Strings(env)
6361
assert.Check(t, is.DeepEqual([]string{"foo=bar", "key=value"}, env))
6462
}
6563

0 commit comments

Comments
 (0)