Skip to content

Commit

Permalink
Feat: Add -reload [s] flag to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
betamos committed Aug 25, 2023
1 parent 782f54a commit 3f0c686
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
network = flag.String("net", "udp", "Change the network to use ipv4 or ipv6 only.")
maxAge = flag.Int("max-age", 60, "Set the max age in seconds.")
text = flag.String("text", "", "Text values for the instance (comma-separated).")
reload = flag.Int("reload", 0, "Reload every n seconds. 0 means never.")

verbose = flag.Bool("v", false, "Verbose mode, with debug output.")
)
Expand All @@ -52,8 +53,8 @@ func main() {
Hostname: *hostname,
}
for _, addr := range split(*addrs) {
instance.Addrs = append(instance.Addrs, netip.MustParseAddr(addr))
}
instance.Addrs = append(instance.Addrs, netip.MustParseAddr(addr))
}
service := zeroconf.ParseService(*serviceStr)

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
Expand Down Expand Up @@ -84,10 +85,24 @@ func main() {
if err != nil {
log.Fatalln("failed creating client:", err)
}
<-ctx.Done()
client.Close()

if err != nil {
// Reload periodically. The "empty ticker" blocks forever
ticker := new(time.Ticker)
if *reload > 0 {
ticker = time.NewTicker(time.Duration(*reload) * time.Second)
}
loop:
for {
select {
case <-ctx.Done():
break loop
case <-ticker.C:
client.Reload()
}
}
ticker.Stop()

if err := client.Close(); err != nil {
log.Fatalln("failed closing client:", err)
}
}
Expand Down

0 comments on commit 3f0c686

Please sign in to comment.