gcal-sync/main.go
2024-10-01 13:44:45 -04:00

94 lines
1.8 KiB
Go

package main
import (
"context"
"errors"
"flag"
"log"
"log/slog"
"time"
"git.cycore.io/scm/gcal-sync/config"
)
var configFile string
var debug bool
var interval time.Duration
func init() {
flag.StringVar(&configFile, "config", "/etc/gcal-sync/config.yaml", "name of the file from which to load the configuration")
flag.BoolVar(&debug, "debug", false, "enable debug logging")
flag.DurationVar(&interval, "interval", 5*time.Minute, "refresh frequency")
}
func main() {
ctx := context.Background()
flag.Parse()
if debug {
slog.SetLogLoggerLevel(slog.LevelDebug)
} else {
slog.SetLogLoggerLevel(slog.LevelInfo)
}
cfg, err := config.Load(configFile)
if err != nil {
log.Fatalf("failed to load configuration file %s: %s", configFile, err.Error())
}
var failures error
for _, item := range cfg.Calendars {
if err := item.Sync(ctx, cfg); err != nil {
slog.Error("failed to sync calendar",
slog.String("name", item.Name),
slog.String("error", err.Error()),
)
failures = errors.Join(failures, err)
continue
}
slog.Debug("posted updated calendar data",
slog.String("name", item.Name),
)
}
for {
if err := Sync(ctx, cfg); err != nil {
slog.Warn("failed to sync all calendars")
} else {
slog.Info("synchronized all calendar data",
slog.Int("count", len(cfg.Calendars)),
)
}
time.Sleep(interval)
}
}
func Sync(ctx context.Context, cfg *config.Config) error {
var failures error
for _, item := range cfg.Calendars {
if err := item.Sync(ctx, cfg); err != nil {
slog.Error("failed to sync calendar",
slog.String("name", item.Name),
slog.String("url", item.URL),
slog.String("error", err.Error()),
)
failures = errors.Join(failures, err)
continue
}
slog.Debug("posted updated calendar data",
slog.String("name", item.Name),
)
}
return failures
}