From 01cf08c22bcdd9ef52eb7afefbc3c081536fdf77 Mon Sep 17 00:00:00 2001 From: Julian Sommer <115516918+juliansommer@users.noreply.github.com> Date: Tue, 1 Sep 2026 12:21:22 +1000 Subject: [PATCH] refactor: replace hand-rolled loops with stdlib slices and maps helpers --- spinifex/ebsprovider/telemetry.go | 8 ++-- spinifex/gateway/hostmatch.go | 21 +++-------- spinifex/gateway/operations.go | 37 +++++-------------- spinifex/handlers/dns/writer.go | 19 ++++------ .../handlers/sts/web_identity_trust_policy.go | 10 ++--- spinifex/utils/nbd.go | 9 ++--- spinifex/vm/eni_reconcile.go | 8 ++-- 7 files changed, 37 insertions(+), 75 deletions(-) diff --git a/spinifex/ebsprovider/telemetry.go b/spinifex/ebsprovider/telemetry.go index 25cc744f6..22e9beb0d 100644 --- a/spinifex/ebsprovider/telemetry.go +++ b/spinifex/ebsprovider/telemetry.go @@ -3,6 +3,8 @@ package ebsprovider import ( "context" "encoding/json" + "maps" + "slices" "strings" "github.com/nats-io/nats.go" @@ -138,11 +140,7 @@ type headerCarrier nats.Header func (c headerCarrier) Get(key string) string { return nats.Header(c).Get(key) } func (c headerCarrier) Set(key, value string) { nats.Header(c).Set(key, value) } func (c headerCarrier) Keys() []string { - keys := make([]string, 0, len(c)) - for key := range c { - keys = append(keys, key) - } - return keys + return slices.Collect(maps.Keys(c)) } // startClientSpan opens a span for an outbound request and injects the trace diff --git a/spinifex/gateway/hostmatch.go b/spinifex/gateway/hostmatch.go index 1f10ff86c..eed73ecd6 100644 --- a/spinifex/gateway/hostmatch.go +++ b/spinifex/gateway/hostmatch.go @@ -2,6 +2,7 @@ package gateway import ( "context" + "net" "net/http" "strings" ) @@ -66,22 +67,12 @@ func parseRegistryHost(host, suffix string) (accountID, region string, ok bool) // stripPort removes a trailing :port from a host, leaving bare IPv6 intact. func stripPort(host string) string { - if host == "" { - return "" + if h, _, err := net.SplitHostPort(host); err == nil { + return h } - // Bracketed IPv6 with optional port: [::1]:5000 or [::1]. - if strings.HasPrefix(host, "[") { - if end := strings.IndexByte(host, ']'); end >= 0 { - return host[1:end] - } - return host - } - // Single colon means host:port; multiple colons mean bare IPv6, never a - // registry host, so leave it untouched. - if strings.Count(host, ":") == 1 { - if h, _, found := strings.Cut(host, ":"); found { - return h - } + // No port: unwrap a bracketed IPv6 literal, leave anything else as-is. + if h, ok := strings.CutPrefix(host, "["); ok { + return strings.TrimSuffix(h, "]") } return host } diff --git a/spinifex/gateway/operations.go b/spinifex/gateway/operations.go index ccdcfbc32..8e1f211f5 100644 --- a/spinifex/gateway/operations.go +++ b/spinifex/gateway/operations.go @@ -1,7 +1,8 @@ package gateway import ( - "sort" + "maps" + "slices" gateway_ecrapi "github.com/mulgadc/spinifex/spinifex/gateway/ecrapi" gateway_ecs "github.com/mulgadc/spinifex/spinifex/gateway/ecs" @@ -56,36 +57,18 @@ func AWSOperationInventory() map[string]ServiceOperationInventory { } } -func mapKeys[K ~string, V any](values map[K]V) []string { - keys := make([]string, 0, len(values)) - for key := range values { - keys = append(keys, string(key)) - } - sort.Strings(keys) - return keys +func mapKeys[V any](values map[string]V) []string { + return slices.Sorted(maps.Keys(values)) } func without(values, excluded []string) []string { - exclude := make(map[string]bool, len(excluded)) - for _, value := range excluded { - exclude[value] = true - } - result := make([]string, 0, len(values)) - for _, value := range values { - if !exclude[value] { - result = append(result, value) - } - } - return result + return slices.DeleteFunc(slices.Clone(values), func(value string) bool { + return slices.Contains(excluded, value) + }) } func union(left, right []string) []string { - set := make(map[string]bool, len(left)+len(right)) - for _, value := range left { - set[value] = true - } - for _, value := range right { - set[value] = true - } - return mapKeys(set) + merged := slices.Concat(left, right) + slices.Sort(merged) + return slices.Compact(merged) } diff --git a/spinifex/handlers/dns/writer.go b/spinifex/handlers/dns/writer.go index f73cc97c2..ba84524a6 100644 --- a/spinifex/handlers/dns/writer.go +++ b/spinifex/handlers/dns/writer.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "log/slog" + "slices" "strings" "time" @@ -249,21 +250,15 @@ func recordType(t string) (uint16, error) { // recordSetExists reports whether the zone already holds a record set for // (label, rtype); an upsert to it replaces in place and never grows the count. func recordSetExists(cfg nsconfig.ConfigArr, label string, rtype uint16) bool { - for _, r := range cfg.Records { - if strings.EqualFold(r.Domain, label) && r.Type == rtype { - return true - } - } - return false + return slices.ContainsFunc(cfg.Records, func(r nsconfig.Records) bool { + return strings.EqualFold(r.Domain, label) && r.Type == rtype + }) } func hasUpsert(changes []Change) bool { - for _, c := range changes { - if c.Action == ActionUpsert { - return true - } - } - return false + return slices.ContainsFunc(changes, func(c Change) bool { + return c.Action == ActionUpsert + }) } // northstarZoneConfig bundles the parsed service config with the writer's diff --git a/spinifex/handlers/sts/web_identity_trust_policy.go b/spinifex/handlers/sts/web_identity_trust_policy.go index a161b0749..1f956e241 100644 --- a/spinifex/handlers/sts/web_identity_trust_policy.go +++ b/spinifex/handlers/sts/web_identity_trust_policy.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "slices" "strings" "github.com/mulgadc/spinifex/spinifex/awserrors" @@ -90,12 +91,9 @@ func matchWebIdentityStatement(actions []string, principalRaw, conditionRaw json } func matchWebIdentityAction(actions []string) bool { - for _, a := range actions { - if a == stsActionAssumeRoleWithWebIdentity || a == stsActionWildcard || a == globalWildcard { - return true - } - } - return false + return slices.ContainsFunc(actions, func(a string) bool { + return a == stsActionAssumeRoleWithWebIdentity || a == stsActionWildcard || a == globalWildcard + }) } // matchFederatedPrincipal accepts Principal.Federated as a string or array. diff --git a/spinifex/utils/nbd.go b/spinifex/utils/nbd.go index 6d26250e9..c8aea0b59 100644 --- a/spinifex/utils/nbd.go +++ b/spinifex/utils/nbd.go @@ -131,13 +131,12 @@ func ParseNBDURI(nbdURI string) (serverType, path, host string, port int, err er } if after, ok := strings.CutPrefix(nbdURI, "nbd://"); ok { - hostPort := after - colonIdx := strings.LastIndex(hostPort, ":") - if colonIdx < 0 { + var portStr string + host, portStr, err = net.SplitHostPort(after) + if err != nil { return "", "", "", 0, fmt.Errorf("missing port in NBD TCP URI: %s", nbdURI) } - host = hostPort[:colonIdx] - port, err = strconv.Atoi(hostPort[colonIdx+1:]) + port, err = strconv.Atoi(portStr) if err != nil { return "", "", "", 0, fmt.Errorf("invalid port in NBD URI: %s", nbdURI) } diff --git a/spinifex/vm/eni_reconcile.go b/spinifex/vm/eni_reconcile.go index 5a2d2dc5e..59bd65850 100644 --- a/spinifex/vm/eni_reconcile.go +++ b/spinifex/vm/eni_reconcile.go @@ -3,6 +3,8 @@ package vm import ( "fmt" "log/slog" + "maps" + "slices" "strconv" "strings" ) @@ -139,11 +141,7 @@ func (m *Manager) ENISlotMapKeys(instance *VM) []string { } instance.ENIRequests.Mu.Lock() defer instance.ENIRequests.Mu.Unlock() - out := make([]string, 0, len(instance.ENIRequests.AttachedByENIID)) - for eniID := range instance.ENIRequests.AttachedByENIID { - out = append(out, eniID) - } - return out + return slices.Collect(maps.Keys(instance.ENIRequests.AttachedByENIID)) } // CleanupENITap removes the tap + OVS port for an ENI. Exported wrapper over