diff --git a/cmd/compatibility/convert.go b/cmd/compatibility/convert.go index 78d9b8303c..39d63114c8 100644 --- a/cmd/compatibility/convert.go +++ b/cmd/compatibility/convert.go @@ -19,6 +19,7 @@ package compatibility import ( "fmt" "os" + "slices" "strings" "github.com/docker/compose/v5/cmd/compose" @@ -59,7 +60,7 @@ func Convert(args []string) []string { ARGS: for i := 0; i < l; i++ { arg := args[i] - if contains(getCompletionCommands(), arg) { + if slices.Contains(getCompletionCommands(), arg) { command = append([]string{arg}, command...) continue } @@ -79,7 +80,7 @@ ARGS: arg = "version" } - if contains(getBoolFlags(), arg) { + if slices.Contains(getBoolFlags(), arg) { rootFlags = append(rootFlags, arg) continue } @@ -105,12 +106,3 @@ ARGS: } return append(rootFlags, command...) } - -func contains(array []string, needle string) bool { - for _, val := range array { - if val == needle { - return true - } - } - return false -} diff --git a/cmd/compose/config.go b/cmd/compose/config.go index 4b5f189273..646ecd8180 100644 --- a/cmd/compose/config.go +++ b/cmd/compose/config.go @@ -23,6 +23,7 @@ import ( "fmt" "io" "os" + "slices" "sort" "strings" @@ -454,9 +455,7 @@ func runHash(ctx context.Context, dockerCli command.Cli, opts configOptions) err } sorted := services - sort.Slice(sorted, func(i, j int) bool { - return sorted[i] < sorted[j] - }) + slices.Sort(sorted) for _, name := range sorted { s, err := project.GetService(name) diff --git a/cmd/compose/ps.go b/cmd/compose/ps.go index 500a294274..2528fccacf 100644 --- a/cmd/compose/ps.go +++ b/cmd/compose/ps.go @@ -167,18 +167,9 @@ func runPs(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOp func filterByStatus(containers []api.ContainerSummary, statuses []string) []api.ContainerSummary { var filtered []api.ContainerSummary for _, c := range containers { - if hasStatus(c, statuses) { + if slices.Contains(statuses, string(c.State)) { filtered = append(filtered, c) } } return filtered } - -func hasStatus(c api.ContainerSummary, statuses []string) bool { - for _, status := range statuses { - if string(c.State) == status { - return true - } - } - return false -} diff --git a/cmd/display/tty.go b/cmd/display/tty.go index 3a69ebaadc..7b624a5cdd 100644 --- a/cmd/display/tty.go +++ b/cmd/display/tty.go @@ -317,10 +317,7 @@ func (w *ttyWriter) printWithDimensions(terminalWidth, terminalHeight int) { allTasks := slices.Collect(w.parentTasks()) // Available lines: terminal height - 2 (header line + potential "more" line) - maxLines := terminalHeight - 2 - if maxLines < 1 { - maxLines = 1 - } + maxLines := max(terminalHeight-2, 1) showMore := len(allTasks) > maxLines tasksToShow := allTasks @@ -354,10 +351,7 @@ func (w *ttyWriter) printWithDimensions(terminalWidth, terminalHeight int) { if showMore { moreCount := len(allTasks) - len(tasksToShow) moreText := fmt.Sprintf(" ... %d more", moreCount) - pad := terminalWidth - len(moreText) - if pad < 0 { - pad = 0 - } + pad := max(terminalWidth-len(moreText), 0) _, _ = fmt.Fprintf(w.out, "%s%s\n", moreText, strings.Repeat(" ", pad)) numLines++ } @@ -392,10 +386,7 @@ func (w *ttyWriter) applyPadding(lines []lineData, terminalWidth int, timerLen i if l.details != "" { lineLen += 1 + utf8.RuneCountInString(l.details) } - l.timerPad = terminalWidth - lineLen - timerLen - if l.timerPad < 1 { - l.timerPad = 1 - } + l.timerPad = max(terminalWidth-lineLen-timerLen, 1) lines[i] = l } @@ -472,10 +463,7 @@ func truncateDetails(lines []lineData, overflow int) bool { for i := range lines { l := &lines[i] if len(l.details) > 3 { - reduction := overflow - if reduction > len(l.details)-3 { - reduction = len(l.details) - 3 - } + reduction := min(overflow, len(l.details)-3) l.details = l.details[:len(l.details)-reduction-3] + "..." return true } else if l.details != "" { @@ -504,10 +492,7 @@ func truncateLongestTaskID(lines []lineData, overflow, minIDLen int) bool { l := &lines[longestIdx] reduction := overflow + 3 // account for "..." - newLen := len(l.taskID) - reduction - if newLen < minIDLen-3 { - newLen = minIDLen - 3 - } + newLen := max(len(l.taskID)-reduction, minIDLen-3) if newLen > 0 { l.taskID = l.taskID[:newLen] + "..." } @@ -546,10 +531,7 @@ func (w *ttyWriter) prepareLineData(t *task) lineData { total += child.total current += child.current r := len(percentChars) - 1 - p := child.percent - if p > 100 { - p = 100 - } + p := min(child.percent, 100) completion = append(completion, percentChars[r*p/100]) } } diff --git a/cmd/display/tty_test.go b/cmd/display/tty_test.go index 875f5f029f..0bbd35f2a2 100644 --- a/cmd/display/tty_test.go +++ b/cmd/display/tty_test.go @@ -186,7 +186,7 @@ func TestPrintWithDimensions_TaskWithProgress(t *testing.T) { w.ids = append(w.ids, "Image nginx") // Create child tasks to trigger progress display - for i := 0; i < 3; i++ { + for i := range 3 { child := &task{ ID: "layer" + string(rune('a'+i)), parents: map[string]struct{}{"Image nginx": {}}, diff --git a/cmd/formatter/shortcut.go b/cmd/formatter/shortcut.go index 16e8fc2e7a..163182baff 100644 --- a/cmd/formatter/shortcut.go +++ b/cmd/formatter/shortcut.go @@ -189,7 +189,7 @@ func (lk *LogKeyboard) clearNavigationMenu() { saveCursor() // clearLine() - for i := 0; i < height; i++ { + for range height { moveCursorDown(1) clearLine() } @@ -341,7 +341,7 @@ func (lk *LogKeyboard) EnableDetach(detach func()) { } func allocateSpace(lines int) { - for i := 0; i < lines; i++ { + for range lines { clearLine() newLine() carriageReturn() diff --git a/internal/tracing/mux.go b/internal/tracing/mux.go index 1a09ef1d96..d27e262d2c 100644 --- a/internal/tracing/mux.go +++ b/internal/tracing/mux.go @@ -36,15 +36,13 @@ func (m MuxExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlyS ) for _, exporter := range m.exporters { - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { if err := exporter.ExportSpans(ctx, spans); err != nil { errMu.Lock() errs = append(errs, err) errMu.Unlock() } - }() + }) } wg.Wait() return errors.Join(errs...) @@ -58,15 +56,13 @@ func (m MuxExporter) Shutdown(ctx context.Context) error { ) for _, exporter := range m.exporters { - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { if err := exporter.Shutdown(ctx); err != nil { errMu.Lock() errs = append(errs, err) errMu.Unlock() } - }() + }) } wg.Wait() return errors.Join(errs...) diff --git a/pkg/compose/pull.go b/pkg/compose/pull.go index ea2b3783a0..8a02dc719b 100644 --- a/pkg/compose/pull.go +++ b/pkg/compose/pull.go @@ -430,10 +430,7 @@ func toPullProgressEvent(parent string, jm jsonstream.Message, events api.EventP current = jm.Progress.Current total = jm.Progress.Total if jm.Progress.Total > 0 { - percent = int(jm.Progress.Current * 100 / jm.Progress.Total) - if percent > 100 { - percent = 100 - } + percent = min(int(jm.Progress.Current*100/jm.Progress.Total), 100) } } case DownloadCompletePhase, AlreadyExistsPhase, PullCompletePhase: diff --git a/pkg/compose/push.go b/pkg/compose/push.go index 58762dd515..b981baf028 100644 --- a/pkg/compose/push.go +++ b/pkg/compose/push.go @@ -155,10 +155,7 @@ func toPushProgressEvent(prefix string, jm jsonstream.Message, events api.EventP current = jm.Progress.Current total = jm.Progress.Total if jm.Progress.Total > 0 { - percent = int(jm.Progress.Current * 100 / jm.Progress.Total) - if percent > 100 { - percent = 100 - } + percent = min(int(jm.Progress.Current*100/jm.Progress.Total), 100) } } } diff --git a/pkg/e2e/logs_test.go b/pkg/e2e/logs_test.go index 6250bee8c0..3706f13642 100644 --- a/pkg/e2e/logs_test.go +++ b/pkg/e2e/logs_test.go @@ -109,7 +109,7 @@ func TestLocalComposeLargeLogs(t *testing.T) { f, err := os.Create(file) assert.NilError(t, err) - for i := 0; i < 300_000; i++ { + for i := range 300_000 { _, err := io.WriteString(f, fmt.Sprintf("This is line %d in a laaaarge text file\n", i)) assert.NilError(t, err) } diff --git a/pkg/watch/debounce_test.go b/pkg/watch/debounce_test.go index 39029f845c..160309c91d 100644 --- a/pkg/watch/debounce_test.go +++ b/pkg/watch/debounce_test.go @@ -31,7 +31,7 @@ func Test_BatchDebounceEvents(t *testing.T) { t.Cleanup(stop) eventBatchCh := BatchDebounceEvents(ctx, clock, ch) - for i := 0; i < 100; i++ { + for i := range 100 { path := "/a" if i%2 == 0 { path = "/b" diff --git a/pkg/watch/notify_test.go b/pkg/watch/notify_test.go index be52ced3a3..fbade58aea 100644 --- a/pkg/watch/notify_test.go +++ b/pkg/watch/notify_test.go @@ -118,7 +118,7 @@ func TestGitBranchSwitch(t *testing.T) { done := f.consumeEventsInBackground(ctx) for i, dir := range dirs { - for j := 0; j < count; j++ { + for j := range count { base := fmt.Sprintf("x/y/dir-%d/x.txt", j) p := filepath.Join(dir, base) f.WriteFile(p, "contents") diff --git a/pkg/watch/watcher_naive_test.go b/pkg/watch/watcher_naive_test.go index 78acfdc5fe..50271df272 100644 --- a/pkg/watch/watcher_naive_test.go +++ b/pkg/watch/watcher_naive_test.go @@ -56,7 +56,7 @@ func TestDontWatchEachFile(t *testing.T) { t.Fatal(err) } - for i := 0; i < 100; i++ { + for i := range 100 { f.WriteFile(f.JoinPath(initialDir, fmt.Sprintf("%d", i)), "initial data") } @@ -79,7 +79,7 @@ func TestDontWatchEachFile(t *testing.T) { t.Fatal(err) } - for i := 0; i < 100; i++ { + for i := range 100 { f.WriteFile(f.JoinPath(inplaceDir, fmt.Sprintf("%d", i)), "inplace data") } @@ -98,7 +98,7 @@ func TestDontWatchEachFile(t *testing.T) { t.Fatal(err) } - for i := 0; i < 100; i++ { + for i := range 100 { f.WriteFile(f.JoinPath(stagedDir, fmt.Sprintf("%d", i)), "staged data") } @@ -146,7 +146,7 @@ func TestDontRecurseWhenWatchingParentsOfNonExistentFiles(t *testing.T) { f.watch(filepath.Join(watched, ".tiltignore")) excludedDir := f.JoinPath(watched, "excluded") - for i := 0; i < 10; i++ { + for i := range 10 { f.WriteFile(f.JoinPath(excludedDir, fmt.Sprintf("%d", i), "data.txt"), "initial data") } f.fsync()