diff --git a/config.go b/config.go index 64cf410..8c2ec4e 100644 --- a/config.go +++ b/config.go @@ -13,11 +13,11 @@ var errInvalidConfig = errors.New("Invalid configuration") // Config type is contains all provided user configuration type Config struct { - CheckInterval SecondsOrDuration `yaml:"check_interval"` - DefaultAlertAfter int16 `yaml:"default_alert_after"` - DefaultAlertEvery *int16 `yaml:"default_alert_every"` - DefaultAlertDown []string `yaml:"default_alert_down"` - DefaultAlertUp []string `yaml:"default_alert_up"` + CheckInterval time.Duration `yaml:"check_interval"` + DefaultAlertAfter int16 `yaml:"default_alert_after"` + DefaultAlertEvery *int16 `yaml:"default_alert_every"` + DefaultAlertDown []string `yaml:"default_alert_down"` + DefaultAlertUp []string `yaml:"default_alert_up"` Monitors []*Monitor Alerts map[string]*Alert } @@ -56,34 +56,6 @@ func (cos *CommandOrShell) UnmarshalYAML(unmarshal func(interface{}) error) erro return nil } -// SecondsOrDuration wraps a duration value for parsing a duration or seconds from YAML -// NOTE: This should be removed in favor of only parsing durations once compatibility is broken -type SecondsOrDuration struct { - value time.Duration -} - -// Value returns a duration value -func (sod SecondsOrDuration) Value() time.Duration { - return sod.value -} - -// UnmarshalYAML allows unmarshalling a duration value or seconds if an int was provided -func (sod *SecondsOrDuration) UnmarshalYAML(unmarshal func(interface{}) error) error { - var seconds int64 - err := unmarshal(&seconds) - - if err == nil { - sod.value = time.Second * time.Duration(seconds) - - return nil - } - - // Error indicates that we don't have an int - err = unmarshal(&sod.value) - - return err -} - // IsValid checks config validity and returns true if valid func (config Config) IsValid() (isValid bool) { isValid = true diff --git a/config_test.go b/config_test.go index 2a20bad..099038a 100644 --- a/config_test.go +++ b/config_test.go @@ -47,15 +47,15 @@ func TestIntervalParsing(t *testing.T) { oneMinute := time.Minute // validate top level interval seconds represented as an int - if config.CheckInterval.Value() != oneSecond { + if config.CheckInterval != oneSecond { t.Errorf("Incorrectly parsed int seconds. expected=%v actual=%v", oneSecond, config.CheckInterval) } - if config.Monitors[0].CheckInterval.Value() != tenSeconds { + if config.Monitors[0].CheckInterval != tenSeconds { t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval) } - if config.Monitors[1].CheckInterval.Value() != oneMinute { + if config.Monitors[1].CheckInterval != oneMinute { t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval) } diff --git a/main.go b/main.go index 39f8e14..05920f6 100644 --- a/main.go +++ b/main.go @@ -120,6 +120,6 @@ func main() { err = checkMonitors(&config) slog.OnErrPanicf(err, "Error checking monitors") - time.Sleep(config.CheckInterval.Value()) + time.Sleep(config.CheckInterval) } } diff --git a/monitor.go b/monitor.go index d907c66..b05937d 100644 --- a/monitor.go +++ b/monitor.go @@ -11,9 +11,9 @@ import ( // Monitor represents a particular periodic check of a command type Monitor struct { //nolint:maligned // Config values - AlertAfter int16 `yaml:"alert_after"` - AlertEvery *int16 `yaml:"alert_every"` - CheckInterval SecondsOrDuration `yaml:"check_interval"` + AlertAfter int16 `yaml:"alert_after"` + AlertEvery *int16 `yaml:"alert_every"` + CheckInterval time.Duration `yaml:"check_interval"` Name string AlertDown []string `yaml:"alert_down"` AlertUp []string `yaml:"alert_up"` @@ -45,7 +45,7 @@ func (monitor Monitor) ShouldCheck() bool { sinceLastCheck := time.Since(monitor.lastCheck) - return sinceLastCheck >= monitor.CheckInterval.Value() + return sinceLastCheck >= monitor.CheckInterval } // Check will run the command configured by the Monitor and return a status diff --git a/monitor_test.go b/monitor_test.go index 9648d6b..14c4079 100644 --- a/monitor_test.go +++ b/monitor_test.go @@ -45,9 +45,9 @@ func TestMonitorShouldCheck(t *testing.T) { name string }{ {Monitor{}, true, "Empty"}, - {Monitor{lastCheck: timeNow, CheckInterval: SecondsOrDuration{time.Second * 15}}, false, "Just checked"}, - {Monitor{lastCheck: timeTenSecAgo, CheckInterval: SecondsOrDuration{time.Second * 15}}, false, "-10s"}, - {Monitor{lastCheck: timeTwentySecAgo, CheckInterval: SecondsOrDuration{time.Second * 15}}, true, "-20s"}, + {Monitor{lastCheck: timeNow, CheckInterval: time.Second * 15}, false, "Just checked"}, + {Monitor{lastCheck: timeTenSecAgo, CheckInterval: time.Second * 15}, false, "-10s"}, + {Monitor{lastCheck: timeTwentySecAgo, CheckInterval: time.Second * 15}, true, "-20s"}, } for _, c := range cases {