From 5bbe15ef247e7ee3c0a2fb835782c667fc9e9052 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 29 Oct 2023 19:08:10 +0000 Subject: [PATCH 1/2] cluster: move SortRegions() from dns to cluster Signed-off-by: Alejandro Mery --- pkg/cluster/regions_utils.go | 29 +++++++++++++++++++++++++++++ pkg/dns/record.go | 30 +++--------------------------- 2 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 pkg/cluster/regions_utils.go diff --git a/pkg/cluster/regions_utils.go b/pkg/cluster/regions_utils.go new file mode 100644 index 0000000..7677df0 --- /dev/null +++ b/pkg/cluster/regions_utils.go @@ -0,0 +1,29 @@ +package cluster + +import "sort" + +// SortRegions sorts regions. first by length those 3-character +// or shorter, and then by length. It's mostly aimed at +// supporting ISO-3166 order +func SortRegions(regions []string) []string { + sort.Slice(regions, func(i, j int) bool { + r1, r2 := regions[i], regions[j] + + switch { + case len(r1) < 4: + switch { + case len(r1) < len(r2): + return true + case len(r1) > len(r2): + return false + default: + return r1 < r2 + } + case len(r2) < 4: + return false + default: + return r1 < r2 + } + }) + return regions +} diff --git a/pkg/dns/record.go b/pkg/dns/record.go index 1b9fc51..3a00d25 100644 --- a/pkg/dns/record.go +++ b/pkg/dns/record.go @@ -11,6 +11,8 @@ import ( "darvaza.org/core" "github.com/libdns/libdns" + + "git.jpi.io/amery/jpictl/pkg/cluster" ) func (mgr *Manager) fqdn(name string) string { @@ -86,32 +88,6 @@ func lessRecord(a, b libdns.Record) bool { } } -// SortRegions sorts regions. first by length those 3-character -// or shorter, and then by length. It's mostly aimed at -// supporting ISO-3166 order -func SortRegions(regions []string) []string { - sort.Slice(regions, func(i, j int) bool { - r1, r2 := regions[i], regions[j] - - switch { - case len(r1) < 4: - switch { - case len(r1) < len(r2): - return true - case len(r1) > len(r2): - return false - default: - return r1 < r2 - } - case len(r2) < 4: - return false - default: - return r1 < r2 - } - }) - return regions -} - // AddrRecord represents an A or AAAA record type AddrRecord struct { Name string @@ -172,7 +148,7 @@ func (mgr *Manager) genRegionsSorted() []string { regions = append(regions, name) } - return SortRegions(regions) + return cluster.SortRegions(regions) } func (mgr *Manager) genAllAddrRecords() []AddrRecord { -- 2.17.1 From 884b11d1f96f8a27619d22105d2280a1c761c5d3 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 29 Oct 2023 19:19:40 +0000 Subject: [PATCH 2/2] cluster: sort regions, for `jpictl dump`' sake Signed-off-by: Alejandro Mery --- pkg/cluster/regions.go | 2 ++ pkg/cluster/regions_utils.go | 34 +++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/pkg/cluster/regions.go b/pkg/cluster/regions.go index c3b716d..3384ca5 100644 --- a/pkg/cluster/regions.go +++ b/pkg/cluster/regions.go @@ -56,6 +56,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error { // first regions defined by zones m.ForEachZone(func(z *Zone) bool { + SortRegions(z.Regions) for _, region := range z.Regions { regions[region] = append(regions[region], z) } @@ -74,6 +75,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error { m.finishRegion(r) } + m.sortRegions() return nil } diff --git a/pkg/cluster/regions_utils.go b/pkg/cluster/regions_utils.go index 7677df0..a44a4ea 100644 --- a/pkg/cluster/regions_utils.go +++ b/pkg/cluster/regions_utils.go @@ -8,22 +8,34 @@ import "sort" func SortRegions(regions []string) []string { sort.Slice(regions, func(i, j int) bool { r1, r2 := regions[i], regions[j] + return regionLess(r1, r2) + }) + + return regions +} +func regionLess(r1, r2 string) bool { + switch { + case len(r1) < 4: switch { - case len(r1) < 4: - switch { - case len(r1) < len(r2): - return true - case len(r1) > len(r2): - return false - default: - return r1 < r2 - } - case len(r2) < 4: + case len(r1) < len(r2): + return true + case len(r1) > len(r2): return false default: return r1 < r2 } + case len(r2) < 4: + return false + default: + return r1 < r2 + } +} + +func (m *Cluster) sortRegions() { + sort.Slice(m.Regions, func(i, j int) bool { + r1 := m.Regions[i].Name + r2 := m.Regions[j].Name + return regionLess(r1, r2) }) - return regions } -- 2.17.1