From 5570855a8a40609c58139f7294402c7ea62c503e Mon Sep 17 00:00:00 2001 From: Michael Aldridge Date: Wed, 27 Sep 2023 13:24:14 -0500 Subject: [PATCH] main: Sort dhcp hosts file --- main.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index a5438dd..e0b09d9 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "io" "log" "os" + "sort" "strings" "text/template" @@ -136,6 +137,15 @@ func main() { } if os.Getenv("DNSMASQ_HOSTSFILE") != "" { + tHosts := make([]*DHCPHost, len(hosts)) + for i, host := range hosts { + sort.Strings(host.HWAddr) + tHosts[i] = host + } + sort.Slice(tHosts, func(i, j int) bool { + return tHosts[i].HWAddr[0] < tHosts[j].HWAddr[0] + }) + f, err := os.Create(os.Getenv("DNSMASQ_HOSTSFILE")) if err != nil { log.Println("Error writing out hosts file", err) @@ -143,7 +153,7 @@ func main() { } defer f.Close() - for _, host := range hosts { + for _, host := range tHosts { if err := hostTmpl.Execute(f, host); err != nil { log.Println("Error executing template", err) }