public-ip-tracker/tracker/main.go

71 lines
1.2 KiB
Go
Raw Normal View History

package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"git.faercol.me/faercol/public-ip-tracker/tracker/bot"
"git.faercol.me/faercol/public-ip-tracker/tracker/config"
)
type cliArgs struct {
configPath string
}
func parseArgs() *cliArgs {
configPath := flag.String("config", "", "Path to the JSON configuration file")
flag.Parse()
return &cliArgs{
configPath: *configPath,
}
}
func main() {
fmt.Println("Parsing arguments")
args := parseArgs()
mainCtx, cancel := context.WithCancel(context.Background())
fmt.Println("Parsing config")
conf, err := config.New(args.configPath)
if err != nil {
panic(err)
}
fmt.Println("Initializing bot")
notifBot := bot.New(mainCtx, conf)
if err := notifBot.SendInitMessage(); err != nil {
panic(err)
}
fmt.Println("Starting monitoring")
go notifBot.Run()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
outerloop:
for {
select {
case <-c:
fmt.Println("received cancel")
cancel()
break outerloop
case err := <-notifBot.ErrChan():
fmt.Printf("Unexpected error %s", err.Error())
case <-notifBot.Exit():
fmt.Println("Unexpected exit")
}
}
<-notifBot.Exit()
fmt.Println("OK")
os.Exit(0)
}