Melora Hugues
3a1bb20a1f
All checks were successful
continuous-integration/drone/push Build is passing
Ref #4 This commit adds an active monitoring of the current public IP. If a change is detected, then a message is sent to the given Telegram channel. Add a few tests for the main monitoring logic.
70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
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)
|
|
}
|