Browse Source

cluster: sort regions, for `jpictl dump`' sake

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/37/head
Alejandro Mery 1 year ago
parent
commit
884b11d1f9
  1. 2
      pkg/cluster/regions.go
  2. 34
      pkg/cluster/regions_utils.go

2
pkg/cluster/regions.go

@ -56,6 +56,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
// first regions defined by zones // first regions defined by zones
m.ForEachZone(func(z *Zone) bool { m.ForEachZone(func(z *Zone) bool {
SortRegions(z.Regions)
for _, region := range z.Regions { for _, region := range z.Regions {
regions[region] = append(regions[region], z) regions[region] = append(regions[region], z)
} }
@ -74,6 +75,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
m.finishRegion(r) m.finishRegion(r)
} }
m.sortRegions()
return nil return nil
} }

34
pkg/cluster/regions_utils.go

@ -8,22 +8,34 @@ import "sort"
func SortRegions(regions []string) []string { func SortRegions(regions []string) []string {
sort.Slice(regions, func(i, j int) bool { sort.Slice(regions, func(i, j int) bool {
r1, r2 := regions[i], regions[j] r1, r2 := regions[i], regions[j]
return regionLess(r1, r2)
})
return regions
}
func regionLess(r1, r2 string) bool {
switch {
case len(r1) < 4:
switch { switch {
case len(r1) < 4: case len(r1) < len(r2):
switch { return true
case len(r1) < len(r2): case len(r1) > len(r2):
return true
case len(r1) > len(r2):
return false
default:
return r1 < r2
}
case len(r2) < 4:
return false return false
default: default:
return r1 < r2 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
} }

Loading…
Cancel
Save