opts: modernize with slices and maps packages

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2026-09-02 02:50:27 +02:00
parent 6ea81b3e2e
commit b1780e06a0
2 changed files with 16 additions and 13 deletions
+6 -3
View File
@@ -1,7 +1,10 @@
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.26
package opts
import (
"sort"
"slices"
"strings"
)
@@ -82,8 +85,8 @@ func EffectiveCapAddCapDrop(add, drop []string) (capAdd, capDrop []string) {
}
}
sort.Strings(capAdd)
sort.Strings(capDrop)
slices.Sort(capAdd)
slices.Sort(capDrop)
return capAdd, capDrop
}
+10 -10
View File
@@ -1,8 +1,13 @@
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.26
package opts
import (
"fmt"
"sort"
"maps"
"slices"
"strings"
"github.com/docker/go-units"
"github.com/moby/moby/api/types/container"
@@ -41,20 +46,15 @@ func (o *UlimitOpt) String() string {
for _, v := range *o.values {
out = append(out, v.String())
}
sort.Strings(out)
return fmt.Sprintf("%v", out)
slices.Sort(out)
return fmt.Sprint(out)
}
// GetList returns a slice of pointers to Ulimits. Values are sorted by name.
func (o *UlimitOpt) GetList() []*container.Ulimit {
ulimits := make([]*container.Ulimit, 0, len(*o.values))
for _, v := range *o.values {
ulimits = append(ulimits, v)
}
sort.SliceStable(ulimits, func(i, j int) bool {
return ulimits[i].Name < ulimits[j].Name
return slices.SortedFunc(maps.Values(*o.values), func(a, b *container.Ulimit) int {
return strings.Compare(a.Name, b.Name)
})
return ulimits
}
// Type returns the option type