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>
This commit is contained in:
Sebastiaan van Stijn 2026-02-11 23:10:42 +01:00 committed by Nicolas De loof
parent da691c7cc1
commit f9828dfab9
13 changed files with 28 additions and 74 deletions

View file

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

View file

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

View file

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

View file

@ -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])
}
}

View file

@ -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": {}},

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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