2023-01-05 17:51:53 +00:00
|
|
|
package main
|
|
|
|
|
2023-01-25 19:42:18 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2023-01-28 13:29:39 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2023-01-25 19:42:18 +00:00
|
|
|
|
|
|
|
"git.faercol.me/faercol/public-ip-tracker/tracker/bot"
|
|
|
|
"git.faercol.me/faercol/public-ip-tracker/tracker/config"
|
|
|
|
)
|
2023-01-05 17:51:53 +00:00
|
|
|
|
2023-01-25 19:42:18 +00:00
|
|
|
type cliArgs struct {
|
|
|
|
configPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseArgs() *cliArgs {
|
|
|
|
configPath := flag.String("config", "", "Path to the JSON configuration file")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
return &cliArgs{
|
|
|
|
configPath: *configPath,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 17:51:53 +00:00
|
|
|
func main() {
|
2023-01-25 19:42:18 +00:00
|
|
|
fmt.Println("Parsing arguments")
|
|
|
|
args := parseArgs()
|
|
|
|
|
2023-01-28 13:29:39 +00:00
|
|
|
mainCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2023-01-25 19:42:18 +00:00
|
|
|
fmt.Println("Parsing config")
|
|
|
|
conf, err := config.New(args.configPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Initializing bot")
|
2023-01-28 13:29:39 +00:00
|
|
|
notifBot := bot.New(mainCtx, conf)
|
2023-01-25 19:42:18 +00:00
|
|
|
if err := notifBot.SendInitMessage(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-01-28 13:29:39 +00:00
|
|
|
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()
|
|
|
|
|
2023-01-25 19:42:18 +00:00
|
|
|
fmt.Println("OK")
|
2023-01-28 13:29:39 +00:00
|
|
|
os.Exit(0)
|
2023-01-05 17:51:53 +00:00
|
|
|
}
|