diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2024-10-20 09:18:06 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2024-10-20 09:18:06 +0200 |
commit | 404578aae4b3440215843bd2d0c156df2450e534 (patch) | |
tree | 030a1d01a05a6503c5eb4c6a5e5218b3f70df352 /utils | |
parent | bdd0e280c3615907e494bc45543257d9a9576e5a (diff) | |
download | infra-404578aae4b3440215843bd2d0c156df2450e534.tar.gz infra-404578aae4b3440215843bd2d0c156df2450e534.tar.bz2 infra-404578aae4b3440215843bd2d0c156df2450e534.tar.xz infra-404578aae4b3440215843bd2d0c156df2450e534.zip |
utils/main.go: sorting
Diffstat (limited to 'utils')
-rw-r--r-- | utils/main.go | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/utils/main.go b/utils/main.go index db27e9c..f419292 100644 --- a/utils/main.go +++ b/utils/main.go @@ -1,10 +1,12 @@ package main import ( + "cmp" "fmt" "log" "net/netip" "os" + "slices" "utils/yaml_ipam" ) @@ -28,6 +30,9 @@ func work() error { return err } + networks := slices.SortedFunc(slices.Values(ipam.networks), CompareNetwork) + roots := slices.SortedFunc(slices.Values(ipam.FindRoots()), CompareNetwork) + fmt.Printf("digraph {\n") //fmt.Printf(" layout=dot\n") //fmt.Printf(" layout=twopi\n") @@ -39,19 +44,22 @@ func work() error { //fmt.Printf(" rankdir=TB\n") fmt.Printf("\n") fmt.Printf(" # Nodes\n") - for _, n := range ipam.networks { + for _, n := range networks { fmt.Printf(" %s [ label = \"%s\\n%s\"];\n", n.name, n.name, n.prefix) } - for _, n := range ipam.FindRoots() { + + fmt.Printf("\n") + fmt.Printf(" # Roots\n") + for _, n := range roots { fmt.Printf(" %s [ root = true ];\n", n.name) } fmt.Printf("\n") fmt.Printf(" # Relationships\n") - for _, n := range ipam.networks { + for _, n := range networks { for _, c := range ipam.networks { if c.parent == n { - fmt.Printf(" %s -> %s;\n", n.name, c.name) + fmt.Printf(" %s -> %s;\n", n.name, c.name) } } } @@ -124,6 +132,14 @@ type Network struct { hosts []networkHost } +func CompareNetwork(a, b *Network) int { + return cmp.Compare(a.name, b.name) +} + +func (n *Network) Compare(other *Network) int { + return cmp.Compare(n.name, other.name) +} + type networkHost struct { name string address netip.Addr @@ -133,10 +149,10 @@ func processNetworks(networks map[string]yaml_ipam.Network6Yaml) (*Ipam, error) var ns []*Network for name, net := range networks { - log.Printf("Processing net %v\net", name) + log.Printf("Processing net %v\n", name) prefix, err := netip.ParsePrefix(net.Range) if err != nil { - return nil, fmt.Errorf("error parsing net range: %v\net", err) + return nil, fmt.Errorf("error parsing net range: %v", err) } log.Printf("prefix: %s", prefix.String()) @@ -144,7 +160,7 @@ func processNetworks(networks map[string]yaml_ipam.Network6Yaml) (*Ipam, error) for hostname, a := range net.Hosts { addr, err := netip.ParseAddr(a) if err != nil { - return nil, fmt.Errorf("network: %s, unable to parse host address %s\net", n.name, a) + return nil, fmt.Errorf("network: %s, unable to parse host address %s", n.name, a) } n.hosts = append(n.hosts, networkHost{ |