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