Skip to content

Commit f9828df

Browse files
thaJeztahndeloof
authored andcommitted
modernize some code
Results of running the modernize command, with some minor changes afterwards (removing the `contains` and `hasStatus` helper functions); go install golang.org/x/tools/go/analysis/passes/modernize/cmd/modernize@latest modernize -fix ./... Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent da691c7 commit f9828df

File tree

13 files changed

+28
-74
lines changed

13 files changed

+28
-74
lines changed

cmd/compatibility/convert.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package compatibility
1919
import (
2020
"fmt"
2121
"os"
22+
"slices"
2223
"strings"
2324

2425
"github.com/docker/compose/v5/cmd/compose"
@@ -59,7 +60,7 @@ func Convert(args []string) []string {
5960
ARGS:
6061
for i := 0; i < l; i++ {
6162
arg := args[i]
62-
if contains(getCompletionCommands(), arg) {
63+
if slices.Contains(getCompletionCommands(), arg) {
6364
command = append([]string{arg}, command...)
6465
continue
6566
}
@@ -79,7 +80,7 @@ ARGS:
7980
arg = "version"
8081
}
8182

82-
if contains(getBoolFlags(), arg) {
83+
if slices.Contains(getBoolFlags(), arg) {
8384
rootFlags = append(rootFlags, arg)
8485
continue
8586
}
@@ -105,12 +106,3 @@ ARGS:
105106
}
106107
return append(rootFlags, command...)
107108
}
108-
109-
func contains(array []string, needle string) bool {
110-
for _, val := range array {
111-
if val == needle {
112-
return true
113-
}
114-
}
115-
return false
116-
}

cmd/compose/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"io"
2525
"os"
26+
"slices"
2627
"sort"
2728
"strings"
2829

@@ -454,9 +455,7 @@ func runHash(ctx context.Context, dockerCli command.Cli, opts configOptions) err
454455
}
455456

456457
sorted := services
457-
sort.Slice(sorted, func(i, j int) bool {
458-
return sorted[i] < sorted[j]
459-
})
458+
slices.Sort(sorted)
460459

461460
for _, name := range sorted {
462461
s, err := project.GetService(name)

cmd/compose/ps.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,9 @@ func runPs(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOp
167167
func filterByStatus(containers []api.ContainerSummary, statuses []string) []api.ContainerSummary {
168168
var filtered []api.ContainerSummary
169169
for _, c := range containers {
170-
if hasStatus(c, statuses) {
170+
if slices.Contains(statuses, string(c.State)) {
171171
filtered = append(filtered, c)
172172
}
173173
}
174174
return filtered
175175
}
176-
177-
func hasStatus(c api.ContainerSummary, statuses []string) bool {
178-
for _, status := range statuses {
179-
if string(c.State) == status {
180-
return true
181-
}
182-
}
183-
return false
184-
}

cmd/display/tty.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,7 @@ func (w *ttyWriter) printWithDimensions(terminalWidth, terminalHeight int) {
317317
allTasks := slices.Collect(w.parentTasks())
318318

319319
// Available lines: terminal height - 2 (header line + potential "more" line)
320-
maxLines := terminalHeight - 2
321-
if maxLines < 1 {
322-
maxLines = 1
323-
}
320+
maxLines := max(terminalHeight-2, 1)
324321

325322
showMore := len(allTasks) > maxLines
326323
tasksToShow := allTasks
@@ -354,10 +351,7 @@ func (w *ttyWriter) printWithDimensions(terminalWidth, terminalHeight int) {
354351
if showMore {
355352
moreCount := len(allTasks) - len(tasksToShow)
356353
moreText := fmt.Sprintf(" ... %d more", moreCount)
357-
pad := terminalWidth - len(moreText)
358-
if pad < 0 {
359-
pad = 0
360-
}
354+
pad := max(terminalWidth-len(moreText), 0)
361355
_, _ = fmt.Fprintf(w.out, "%s%s\n", moreText, strings.Repeat(" ", pad))
362356
numLines++
363357
}
@@ -392,10 +386,7 @@ func (w *ttyWriter) applyPadding(lines []lineData, terminalWidth int, timerLen i
392386
if l.details != "" {
393387
lineLen += 1 + utf8.RuneCountInString(l.details)
394388
}
395-
l.timerPad = terminalWidth - lineLen - timerLen
396-
if l.timerPad < 1 {
397-
l.timerPad = 1
398-
}
389+
l.timerPad = max(terminalWidth-lineLen-timerLen, 1)
399390
lines[i] = l
400391

401392
}
@@ -472,10 +463,7 @@ func truncateDetails(lines []lineData, overflow int) bool {
472463
for i := range lines {
473464
l := &lines[i]
474465
if len(l.details) > 3 {
475-
reduction := overflow
476-
if reduction > len(l.details)-3 {
477-
reduction = len(l.details) - 3
478-
}
466+
reduction := min(overflow, len(l.details)-3)
479467
l.details = l.details[:len(l.details)-reduction-3] + "..."
480468
return true
481469
} else if l.details != "" {
@@ -504,10 +492,7 @@ func truncateLongestTaskID(lines []lineData, overflow, minIDLen int) bool {
504492

505493
l := &lines[longestIdx]
506494
reduction := overflow + 3 // account for "..."
507-
newLen := len(l.taskID) - reduction
508-
if newLen < minIDLen-3 {
509-
newLen = minIDLen - 3
510-
}
495+
newLen := max(len(l.taskID)-reduction, minIDLen-3)
511496
if newLen > 0 {
512497
l.taskID = l.taskID[:newLen] + "..."
513498
}
@@ -546,10 +531,7 @@ func (w *ttyWriter) prepareLineData(t *task) lineData {
546531
total += child.total
547532
current += child.current
548533
r := len(percentChars) - 1
549-
p := child.percent
550-
if p > 100 {
551-
p = 100
552-
}
534+
p := min(child.percent, 100)
553535
completion = append(completion, percentChars[r*p/100])
554536
}
555537
}

cmd/display/tty_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func TestPrintWithDimensions_TaskWithProgress(t *testing.T) {
186186
w.ids = append(w.ids, "Image nginx")
187187

188188
// Create child tasks to trigger progress display
189-
for i := 0; i < 3; i++ {
189+
for i := range 3 {
190190
child := &task{
191191
ID: "layer" + string(rune('a'+i)),
192192
parents: map[string]struct{}{"Image nginx": {}},

cmd/formatter/shortcut.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (lk *LogKeyboard) clearNavigationMenu() {
189189
saveCursor()
190190

191191
// clearLine()
192-
for i := 0; i < height; i++ {
192+
for range height {
193193
moveCursorDown(1)
194194
clearLine()
195195
}
@@ -341,7 +341,7 @@ func (lk *LogKeyboard) EnableDetach(detach func()) {
341341
}
342342

343343
func allocateSpace(lines int) {
344-
for i := 0; i < lines; i++ {
344+
for range lines {
345345
clearLine()
346346
newLine()
347347
carriageReturn()

internal/tracing/mux.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ func (m MuxExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlyS
3636
)
3737

3838
for _, exporter := range m.exporters {
39-
wg.Add(1)
40-
go func() {
41-
defer wg.Done()
39+
wg.Go(func() {
4240
if err := exporter.ExportSpans(ctx, spans); err != nil {
4341
errMu.Lock()
4442
errs = append(errs, err)
4543
errMu.Unlock()
4644
}
47-
}()
45+
})
4846
}
4947
wg.Wait()
5048
return errors.Join(errs...)
@@ -58,15 +56,13 @@ func (m MuxExporter) Shutdown(ctx context.Context) error {
5856
)
5957

6058
for _, exporter := range m.exporters {
61-
wg.Add(1)
62-
go func() {
63-
defer wg.Done()
59+
wg.Go(func() {
6460
if err := exporter.Shutdown(ctx); err != nil {
6561
errMu.Lock()
6662
errs = append(errs, err)
6763
errMu.Unlock()
6864
}
69-
}()
65+
})
7066
}
7167
wg.Wait()
7268
return errors.Join(errs...)

pkg/compose/pull.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,7 @@ func toPullProgressEvent(parent string, jm jsonstream.Message, events api.EventP
430430
current = jm.Progress.Current
431431
total = jm.Progress.Total
432432
if jm.Progress.Total > 0 {
433-
percent = int(jm.Progress.Current * 100 / jm.Progress.Total)
434-
if percent > 100 {
435-
percent = 100
436-
}
433+
percent = min(int(jm.Progress.Current*100/jm.Progress.Total), 100)
437434
}
438435
}
439436
case DownloadCompletePhase, AlreadyExistsPhase, PullCompletePhase:

pkg/compose/push.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@ func toPushProgressEvent(prefix string, jm jsonstream.Message, events api.EventP
155155
current = jm.Progress.Current
156156
total = jm.Progress.Total
157157
if jm.Progress.Total > 0 {
158-
percent = int(jm.Progress.Current * 100 / jm.Progress.Total)
159-
if percent > 100 {
160-
percent = 100
161-
}
158+
percent = min(int(jm.Progress.Current*100/jm.Progress.Total), 100)
162159
}
163160
}
164161
}

pkg/e2e/logs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestLocalComposeLargeLogs(t *testing.T) {
109109

110110
f, err := os.Create(file)
111111
assert.NilError(t, err)
112-
for i := 0; i < 300_000; i++ {
112+
for i := range 300_000 {
113113
_, err := io.WriteString(f, fmt.Sprintf("This is line %d in a laaaarge text file\n", i))
114114
assert.NilError(t, err)
115115
}

0 commit comments

Comments
 (0)