Browse Source

cluster: move SortRegions() from dns to cluster

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/37/head
Alejandro Mery 1 year ago
parent
commit
5bbe15ef24
  1. 29
      pkg/cluster/regions_utils.go
  2. 30
      pkg/dns/record.go

29
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
}

30
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 {

Loading…
Cancel
Save