Skip to content

Commit a068e3b

Browse files
flushthemoneymedyagh
authored andcommitted
refactor: modernize slice operations using slices and maps packages
Replace traditional append-in-loop patterns with modern Go functions slices.Sorted(), slices.Collect(), and maps.Keys()/maps.Values() Changes made (5 files): - cmd/minikube/cmd/config/addons_list.go: Use slices.Sorted(maps.Keys()) - cmd/minikube/cmd/version.go: Use slices.Sorted(maps.Keys()) - hack/changelog/changelog.go: Use slices.Collect(maps.Keys()) - pkg/minikube/node/cache.go: Use slices.Collect(maps.Keys()) - pkg/minikube/registry/registry.go: Use slices.Collect(maps.Values()) Files skipped due to complexity: - pkg/drivers/kic/oci/oci.go - pkg/drivers/hyperkit/driver.go - pkg/drivers/kvm/gpu.go - pkg/drivers/kvm/numa.go [Unrelated slice optimization possible, can be addressed along with other similar code] - pkg/minikube/tunnel/kic/* - cmd/minikube/cmd/service.go - hack/legacy_fill_db/filldb.go These cases require more sophisticated transformation logic that might be better addressed in separate issue/PR
1 parent cf5ea72 commit a068e3b

File tree

5 files changed

+18
-32
lines changed

5 files changed

+18
-32
lines changed

cmd/minikube/cmd/config/addons_list.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ package config
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
"maps"
2223
"os"
23-
"sort"
24+
"slices"
2425
"strings"
2526

2627
"github.com/olekukonko/tablewriter"
@@ -90,12 +91,7 @@ var stringFromStatus = func(addonStatus bool) string {
9091
}
9192

9293
var printAddonsList = func(cc *config.ClusterConfig, printDocs bool) {
93-
addonNames := make([]string, 0, len(assets.Addons))
94-
for addonName := range assets.Addons {
95-
addonNames = append(addonNames, addonName)
96-
}
97-
sort.Strings(addonNames)
98-
94+
addonNames := slices.Sorted(maps.Keys(assets.Addons))
9995
table := tablewriter.NewWriter(os.Stdout)
10096

10197
table.Options(
@@ -154,12 +150,7 @@ var printAddonsList = func(cc *config.ClusterConfig, printDocs bool) {
154150
}
155151

156152
var printAddonsJSON = func(cc *config.ClusterConfig) {
157-
addonNames := make([]string, 0, len(assets.Addons))
158-
for addonName := range assets.Addons {
159-
addonNames = append(addonNames, addonName)
160-
}
161-
sort.Strings(addonNames)
162-
153+
addonNames := slices.Sorted(maps.Keys(assets.Addons))
163154
addonsMap := map[string]map[string]interface{}{}
164155

165156
for _, addonName := range addonNames {

cmd/minikube/cmd/version.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ package cmd
1818

1919
import (
2020
"encoding/json"
21+
"maps"
2122
"os/exec"
22-
"sort"
23+
"slices"
2324
"strings"
2425

2526
"github.com/spf13/cobra"
@@ -89,11 +90,7 @@ var versionCmd = &cobra.Command{
8990
if gitCommitID != "" {
9091
out.Ln("commit: %v", gitCommitID)
9192
}
92-
keys := make([]string, 0, len(data))
93-
for k := range data {
94-
keys = append(keys, k)
95-
}
96-
sort.Strings(keys)
93+
keys := slices.Sorted(maps.Keys(data))
9794
for _, k := range keys {
9895
v := data[k]
9996
// for backward compatibility we keep displaying the old way for these two

hack/changelog/changelog.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import (
2121
"flag"
2222
"fmt"
2323
"log"
24+
"maps"
2425
"net/http"
2526
"os"
27+
"slices"
2628
"sort"
2729
"strings"
2830

@@ -198,10 +200,7 @@ func classify(pr *github.PullRequest, cfg Config, allowed map[string]struct{}) (
198200
// Then look for substring matches. Iterate in a deterministic order so
199201
// overlapping keywords behave predictably. Longer substrings are checked
200202
// first to favor more specific matches.
201-
subs := make([]string, 0, len(cfg.ContainGroups))
202-
for sub := range cfg.ContainGroups {
203-
subs = append(subs, sub)
204-
}
203+
subs := slices.Collect(maps.Keys(cfg.ContainGroups))
205204
sort.Slice(subs, func(i, j int) bool {
206205
if len(subs[i]) == len(subs[j]) {
207206
return subs[i] < subs[j]

pkg/minikube/node/cache.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package node
1818

1919
import (
2020
"fmt"
21+
"maps"
2122
"os"
2223
"path"
2324
"runtime"
25+
"slices"
2426
"strings"
2527

2628
"k8s.io/minikube/pkg/minikube/detect"
@@ -272,10 +274,9 @@ func imagesInConfigFile() ([]string, error) {
272274
return nil, errors.Wrap(err, "read")
273275
}
274276
if values, ok := configFile[cacheImageConfigKey]; ok {
275-
var images []string
276-
for key := range values.(map[string]interface{}) {
277-
images = append(images, key)
278-
}
277+
// Type assertion needed because config values are stored as interface{}
278+
m := values.(map[string]interface{})
279+
images := slices.Collect(maps.Keys(m))
279280
return images, nil
280281
}
281282
return []string{}, nil

pkg/minikube/registry/registry.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package registry
1818

1919
import (
2020
"fmt"
21+
"maps"
22+
"slices"
2123
"sync"
2224

2325
"github.com/docker/machine/libmachine/drivers"
@@ -157,11 +159,7 @@ func (r *driverRegistry) List() []DriverDef {
157159
r.lock.RLock()
158160
defer r.lock.RUnlock()
159161

160-
result := make([]DriverDef, 0, len(r.drivers))
161-
162-
for _, def := range r.drivers {
163-
result = append(result, def)
164-
}
162+
result := slices.Collect(maps.Values(r.drivers))
165163

166164
return result
167165
}

0 commit comments

Comments
 (0)