Files

90 lines
2.2 KiB
Go

package main
import (
"fmt"
"log/slog"
"os"
"time"
"github.com/spf13/pflag"
)
type CLIArgs struct {
Verbose bool
AlwaysNotify bool
ConfigPath string
}
func parseCliArgs() *CLIArgs {
var cfg CLIArgs
// Define flags bound directly to the struct fields
pflag.BoolVarP(&cfg.Verbose, "verbose", "v", false, "Enable verbose (debug) logging")
pflag.BoolVarP(&cfg.AlwaysNotify, "always-notify", "a", false, "Always send a ntfy notification, even if no availability is found")
pflag.StringVarP(&cfg.ConfigPath, "config", "c", defaultConfigFile(), "Path to the JSON/YAML config file")
pflag.Parse()
return &cfg
}
func configureLogger(verbose bool) {
logLevel := slog.LevelInfo
if verbose {
logLevel = slog.LevelDebug
}
logHandler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
})
slog.SetDefault(slog.New(logHandler))
}
func main() {
cliArgs := parseCliArgs()
configureLogger(cliArgs.Verbose)
// Parse config file
config, err := loadConfig(cliArgs.ConfigPath)
if err != nil {
slog.Error("Failed to load config", "config file", cliArgs.ConfigPath, "err", err)
os.Exit(1)
}
slog.Debug("Loaded config", "Ntfy Channel", config.NtfyChannel)
// Check for availabilities
notifier := NewNotifier(config.NtfyChannel)
doctoClient := NewDoctoClient()
startDate := time.Now().Format(time.RFC3339)
for _, doctor := range config.Doctors {
hasAvailability, err := doctoClient.check(doctor, startDate)
if err != nil {
slog.Error("Failed to check Doctolib", "doctor", doctor.Name, "err", err)
continue
}
if hasAvailability {
slog.Info("DoctoCheck FOUND", "doctor", doctor.Name)
msg := fmt.Sprintf("Availability found for %s, check Doctolib!", doctor.Name)
err = notifier.Send(msg)
if err != nil {
slog.Error("Failed to send notification",
"doctor", doctor.Name,
"channel", config.NtfyChannel,
"err", err)
continue
}
}
if cliArgs.AlwaysNotify && !hasAvailability {
msg := fmt.Sprintf("[DEBUG] No availability found for %s", doctor.Name)
err = notifier.Send(msg)
if err != nil {
slog.Error("Failed to send notification",
"doctor", doctor.Name,
"channel", config.NtfyChannel,
"err", err)
continue
}
}
}
}