Skip to content

Commit

Permalink
main: Sort dhcp hosts file
Browse files Browse the repository at this point in the history
  • Loading branch information
the-maldridge committed Sep 27, 2023
1 parent 03a5de6 commit ec3d382
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"log"
"os"
"sort"
"strings"
"text/template"

Expand Down Expand Up @@ -136,14 +137,23 @@ 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[int64(i)].HWAddr[0] < tHosts[int64(j)].HWAddr[0]
})

f, err := os.Create(os.Getenv("DNSMASQ_HOSTSFILE"))
if err != nil {
log.Println("Error writing out hosts file", err)
os.Exit(1)
}
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)
}
Expand Down

0 comments on commit ec3d382

Please sign in to comment.