Skip to content

Commit 3d62a7c

Browse files
authored
Merge pull request #6791 from thaJeztah/modernize
modernize: various cleanups
2 parents 3e466c8 + fddfe63 commit 3d62a7c

File tree

34 files changed

+107
-119
lines changed

34 files changed

+107
-119
lines changed

cli-plugins/manager/candidate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func TestValidateCandidate(t *testing.T) {
151151
assert.ErrorContains(t, err, tc.err)
152152
case tc.invalid != "":
153153
assert.NilError(t, err)
154-
assert.Assert(t, is.ErrorType(p.Err, reflect.TypeOf(&pluginError{})))
154+
assert.Assert(t, is.ErrorType(p.Err, reflect.TypeFor[*pluginError]()))
155155
assert.ErrorContains(t, p.Err, tc.invalid)
156156
default:
157157
assert.NilError(t, err)

cli-plugins/manager/hooks.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2+
//go:build go1.24
3+
14
package manager
25

36
import (
@@ -141,8 +144,7 @@ func pluginMatch(pluginCfg map[string]string, subCmd string) (string, bool) {
141144
return "", false
142145
}
143146

144-
commands := strings.Split(configuredPluginHooks, ",")
145-
for _, hookCmd := range commands {
147+
for hookCmd := range strings.SplitSeq(configuredPluginHooks, ",") {
146148
if hookMatch(hookCmd, subCmd) {
147149
return hookCmd, true
148150
}

cli/command/container/opts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ func convertToStandardNotation(ports []string) ([]string, error) {
902902
for _, publish := range ports {
903903
if strings.Contains(publish, "=") {
904904
params := map[string]string{"protocol": "tcp"}
905-
for _, param := range strings.Split(publish, ",") {
905+
for param := range strings.SplitSeq(publish, ",") {
906906
k, v, ok := strings.Cut(param, "=")
907907
if !ok || k == "" {
908908
return optsList, fmt.Errorf("invalid publish opts format (should be name=value but got '%s')", param)

cli/command/container/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func TestRunPullTermination(t *testing.T) {
247247
go func() {
248248
id := test.RandomID()[:12] // short-ID
249249
progressOutput := streamformatter.NewJSONProgressOutput(server, true)
250-
for i := 0; i < 100; i++ {
250+
for i := range 100 {
251251
select {
252252
case <-ctx.Done():
253253
assert.NilError(t, server.Close(), "failed to close imageCreateFunc server")

cli/command/container/signals_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import (
1111
)
1212

1313
func TestForwardSignals(t *testing.T) {
14-
ctx, cancel := context.WithCancel(context.Background())
15-
defer cancel()
14+
ctx := t.Context()
1615

1716
called := make(chan struct{})
1817
apiClient := &fakeClient{containerKillFunc: func(ctx context.Context, container string, options client.ContainerKillOptions) (client.ContainerKillResult, error) {

cli/command/container/stats.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2+
//go:build go1.24
3+
14
package container
25

36
import (
@@ -337,7 +340,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
337340
return err
338341
}
339342

340-
for _, line := range strings.Split(statsTextBuffer.String(), "\n") {
343+
for line := range strings.SplitSeq(statsTextBuffer.String(), "\n") {
341344
// In case the new text is shorter than the one we are writing over,
342345
// we'll append the "erase line" escape sequence to clear the remaining text.
343346
_, _ = fmt.Fprintln(&statsTextBuffer, line, "\033[K")

cli/command/container/tty.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func initTtySize(ctx context.Context, cli command.Cli, id string, isExec bool, r
6060
if err := rTTYfunc(ctx, cli, id, isExec); err != nil {
6161
go func() {
6262
var err error
63-
for retry := 0; retry < 10; retry++ {
63+
for retry := range 10 {
6464
time.Sleep(time.Duration(retry+1) * 10 * time.Millisecond)
6565
if err = rTTYfunc(ctx, cli, id, isExec); err == nil {
6666
break

cli/command/context.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package command
66
import (
77
"encoding/json"
88
"errors"
9+
"maps"
910

1011
"github.com/docker/cli/cli/context/store"
1112
)
@@ -23,9 +24,7 @@ func (dc DockerContext) MarshalJSON() ([]byte, error) {
2324
s["Description"] = dc.Description
2425
}
2526
if dc.AdditionalFields != nil {
26-
for k, v := range dc.AdditionalFields {
27-
s[k] = v
28-
}
27+
maps.Copy(s, dc.AdditionalFields)
2928
}
3029
return json.Marshal(s)
3130
}

cli/command/formatter/tabwriter/tabwriter_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (b *buffer) Write(buf []byte) (written int, err error) {
2525
m := len(buf)
2626
if n+m <= cap(b.a) {
2727
b.a = b.a[0 : n+m]
28-
for i := 0; i < m; i++ {
28+
for i := range m {
2929
b.a[n+i] = buf[i]
3030
}
3131
} else {
@@ -669,7 +669,7 @@ func BenchmarkTable(b *testing.B) {
669669
for i := 0; i < b.N; i++ {
670670
w := NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings
671671
// Write the line h times.
672-
for j := 0; j < h; j++ {
672+
for range h {
673673
w.Write(line)
674674
}
675675
w.Flush()
@@ -681,7 +681,7 @@ func BenchmarkTable(b *testing.B) {
681681
w := NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings
682682
for i := 0; i < b.N; i++ {
683683
// Write the line h times.
684-
for j := 0; j < h; j++ {
684+
for range h {
685685
w.Write(line)
686686
}
687687
w.Flush()
@@ -701,7 +701,7 @@ func BenchmarkPyramid(b *testing.B) {
701701
for i := 0; i < b.N; i++ {
702702
w := NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings
703703
// Write increasing prefixes of that line.
704-
for j := 0; j < x; j++ {
704+
for j := range x {
705705
w.Write(line[:j*2])
706706
w.Write([]byte{'\n'})
707707
}
@@ -723,7 +723,7 @@ func BenchmarkRagged(b *testing.B) {
723723
for i := 0; i < b.N; i++ {
724724
w := NewWriter(io.Discard, 4, 4, 1, ' ', 0) // no particular reason for these settings
725725
// Write the lines in turn h times.
726-
for j := 0; j < h; j++ {
726+
for j := range h {
727727
w.Write(lines[j%len(lines)])
728728
w.Write([]byte{'\n'})
729729
}

cli/command/idresolver/idresolver_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestResolveWithCache(t *testing.T) {
6060
idResolver := New(apiClient, false)
6161

6262
ctx := context.Background()
63-
for i := 0; i < 2; i++ {
63+
for range 2 {
6464
id, err := idResolver.Resolve(ctx, swarm.Node{}, "nodeID")
6565
assert.NilError(t, err)
6666
assert.Check(t, is.Equal("node-foo", id))

0 commit comments

Comments
 (0)