Add support for notification dry run feature
- Introduced NOTIFICATION_DRY_RUN configuration option in .env.example and k8s/secret.example.yaml. - Updated README.md to include usage instructions for the new dry run feature. - Implemented logic in app.go to preview notifications without sending them when the dry run option is enabled. - Enhanced config.go to load the new configuration option and validate notification types accordingly. - Added a new function in notify.go to generate manual-needed notification messages for preview.
This commit is contained in:
@@ -28,6 +28,7 @@ type Config struct {
|
||||
AlertAfter time.Duration
|
||||
HTTPTimeout time.Duration
|
||||
|
||||
NotificationDryRun bool
|
||||
NotificationType string
|
||||
NotificationNTFYURL string
|
||||
NotificationSMTPHost string
|
||||
@@ -56,6 +57,7 @@ func Load(args []string) (Config, error) {
|
||||
AlertAfter: envDuration("ALERT_AFTER", 48*time.Hour),
|
||||
HTTPTimeout: envDuration("HTTP_TIMEOUT", 30*time.Second),
|
||||
|
||||
NotificationDryRun: envBool("NOTIFICATION_DRY_RUN", false),
|
||||
NotificationType: env("NOTIFICATION_TYPE", ""),
|
||||
NotificationNTFYURL: env("NOTIFICATION_NTFY_URL", env("NOTIFY_URL", "")),
|
||||
NotificationSMTPHost: env("NOTIFICATION_SMTP_HOST", ""),
|
||||
@@ -72,6 +74,7 @@ func Load(args []string) (Config, error) {
|
||||
fs.BoolVar(&cfg.SkipQBit, "skip-qbit", cfg.SkipQBit, "skip qBittorrent matching/actions")
|
||||
fs.BoolVar(&cfg.JSONOutput, "json", cfg.JSONOutput, "print JSON summary")
|
||||
fs.DurationVar(&cfg.AlertAfter, "alert-after", cfg.AlertAfter, "mark active torrents manual-needed after this duration")
|
||||
fs.BoolVar(&cfg.NotificationDryRun, "notification-dry-run", cfg.NotificationDryRun, "print the manual-needed notification without sending it")
|
||||
fs.StringVar(&cfg.NotificationType, "notification-type", cfg.NotificationType, "notification sender: ntfy, smtp, or empty")
|
||||
fs.StringVar(&cfg.NotificationNTFYURL, "notification-ntfy-url", cfg.NotificationNTFYURL, "ntfy topic URL for manual-needed alerts")
|
||||
fs.StringVar(&cfg.NotificationNTFYURL, "notify-url", cfg.NotificationNTFYURL, "deprecated: use --notification-ntfy-url")
|
||||
@@ -99,7 +102,11 @@ func Load(args []string) (Config, error) {
|
||||
return cfg, fmt.Errorf("set QBITTORRENT_URL, QBITTORRENT_USERNAME and QBITTORRENT_PASSWORD, or use --dry-run/--skip-qbit")
|
||||
}
|
||||
}
|
||||
if err := validateNotification(cfg); err != nil {
|
||||
if !cfg.NotificationDryRun {
|
||||
if err := validateNotification(cfg); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
} else if err := validateNotificationType(cfg); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
@@ -179,6 +186,9 @@ func normalizeNotificationType(cfg Config) string {
|
||||
}
|
||||
|
||||
func validateNotification(cfg Config) error {
|
||||
if err := validateNotificationType(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
switch cfg.NotificationType {
|
||||
case "":
|
||||
return nil
|
||||
@@ -207,3 +217,12 @@ func validateNotification(cfg Config) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateNotificationType(cfg Config) error {
|
||||
switch cfg.NotificationType {
|
||||
case "", "ntfy", "smtp":
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("unsupported NOTIFICATION_TYPE %q", cfg.NotificationType)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user