Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix(network): sort subnet and gateway columns numerically (#4119)
Kyle Mendell324adf3585ec
diff --git a/backend/internal/network/handler.go b/backend/internal/network/handler.goindex 3bc0f6e0a..6a17bc7b0 100644--- a/backend/internal/network/handler.go+++ b/backend/internal/network/handler.go@@ -3,9 +3,7 @@ package networkimport ("github.com/getarcaneapp/arcane/backend/v2/internal/database"- "bytes""context"- "net""net/http""sort""strings"@@ -23,6 +21,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/pkg/utils""github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil""github.com/getarcaneapp/arcane/backend/v2/pkg/utils/mapper"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/netutils""github.com/getarcaneapp/arcane/types/v2/base"networktypes "github.com/getarcaneapp/arcane/types/v2/network"dockernetwork "github.com/moby/moby/api/types/network"@@ -326,22 +325,7 @@ func (h *NetworkHandler) GetNetwork(ctx context.Context, input *GetNetworkInput)valB = b.IPv6Address}- // Parse IPs for proper numeric comparison- ipA, _, _ := strings.Cut(valA, "/")- ipB, _, _ := strings.Cut(valB, "/")-- parsedA := net.ParseIP(ipA)- parsedB := net.ParseIP(ipB)-- if parsedA == nil || parsedB == nil {- // Fallback to string comparison if parsing fails- if input.Order == "desc" {- return valA > valB- }- return valA < valB- }-- cmp := bytes.Compare(parsedA, parsedB)+ cmp := netutils.CompareAddresses(valA, valB)if input.Order == "desc" {return cmp > 0}diff --git a/backend/internal/network/service.go b/backend/internal/network/service.goindex df02d71b2..21350f278 100644--- a/backend/internal/network/service.go+++ b/backend/internal/network/service.go@@ -20,6 +20,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/pkg/pagination""github.com/getarcaneapp/arcane/backend/v2/pkg/utils""github.com/getarcaneapp/arcane/backend/v2/pkg/utils/mapper"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/netutils"networktypes "github.com/getarcaneapp/arcane/types/v2/network""github.com/moby/moby/api/types/container""github.com/moby/moby/api/types/network"@@ -470,7 +471,7 @@ func (s *NetworkService) buildNetworkSortBindings() []pagination.SortBinding[netFn: func(a, b networktypes.Summary) int {subnetsA, _ := ipamValuesInternal(a)subnetsB, _ := ipamValuesInternal(b)- return slices.Compare(subnetsA, subnetsB)+ return compareIPAMListsInternal(subnetsA, subnetsB, netutils.CompareSubnets)},},{@@ -478,7 +479,7 @@ func (s *NetworkService) buildNetworkSortBindings() []pagination.SortBinding[netFn: func(a, b networktypes.Summary) int {_, gatewaysA := ipamValuesInternal(a)_, gatewaysB := ipamValuesInternal(b)- return slices.Compare(gatewaysA, gatewaysB)+ return compareIPAMListsInternal(gatewaysA, gatewaysB, netutils.CompareAddresses)},},}@@ -496,6 +497,14 @@ func ipamValuesInternal(n networktypes.Summary) (subnets, gateways []string) {return subnets, gateways}+// compareIPAMListsInternal sorts copies of both lists so IPAM entry order does+// not affect row order; the displayed entries are untouched.+func compareIPAMListsInternal(a, b []string, compare func(x, y string) int) int {+ sortedA := slices.SortedFunc(slices.Values(a), compare)+ sortedB := slices.SortedFunc(slices.Values(b), compare)+ return slices.CompareFunc(sortedA, sortedB, compare)+}+func (s *NetworkService) compareNetworkCreated(a, b networktypes.Summary) int {if a.Created.Before(b.Created) {return -1diff --git a/backend/pkg/utils/netutils/compare.go b/backend/pkg/utils/netutils/compare.gonew file mode 100644index 000000000..09a18fbce--- /dev/null+++ b/backend/pkg/utils/netutils/compare.go@@ -0,0 +1,57 @@+package netutils++import (+ "bytes"+ "cmp"+ "net"+ "strings"+)++// CompareAddresses orders IP address strings numerically, ignoring any CIDR suffix.+// Empty values sort first and malformed values last in string order.+func CompareAddresses(a, b string) int {+ if a == "" || b == "" {+ return strings.Compare(a, b)+ }++ hostA, _, _ := strings.Cut(a, "/")+ hostB, _, _ := strings.Cut(b, "/")+ ipA := net.ParseIP(hostA)+ ipB := net.ParseIP(hostB)++ switch {+ case ipA != nil && ipB != nil:+ return bytes.Compare(ipA.To16(), ipB.To16())+ case ipA != nil:+ return -1+ case ipB != nil:+ return 1+ }+ return strings.Compare(a, b)+}++// CompareSubnets orders CIDR strings by masked network address, then prefix length.+// Empty values sort first and malformed values last in string order.+func CompareSubnets(a, b string) int {+ if a == "" || b == "" {+ return strings.Compare(a, b)+ }++ _, netA, errA := net.ParseCIDR(a)+ _, netB, errB := net.ParseCIDR(b)++ switch {+ case errA == nil && errB == nil:+ if c := bytes.Compare(netA.IP.To16(), netB.IP.To16()); c != 0 {+ return c+ }+ onesA, bitsA := netA.Mask.Size()+ onesB, bitsB := netB.Mask.Size()+ return cmp.Compare(onesA+128-bitsA, onesB+128-bitsB)+ case errA == nil:+ return -1+ case errB == nil:+ return 1+ }+ return strings.Compare(a, b)+}