From 0c1f0f4e5e81ef9a1387a74deb504e4c69761f59 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Mon, 6 May 2024 13:34:29 -0500 Subject: [PATCH] chore(config): Move `interval` to `advanced.fallback-interval` --- internal/config/config.go | 6 +++--- internal/config/default.go | 10 +++++----- internal/config/load.go | 15 +++++++++++++++ internal/ticker/fetch.go | 2 +- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 3318c9f..e76c821 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -14,7 +14,6 @@ type Config struct { URL string `toml:"url" comment:"Nightscout URL. (required)"` Token string `toml:"token" comment:"Nightscout token. Using an access token is recommended instead of the API secret."` Units string `toml:"units" comment:"Blood sugar unit. (one of: mg/dL, mmol/L)"` - Interval Duration `toml:"interval" comment:"Update interval."` Arrows Arrows `toml:"arrows" comment:"Customize the arrows."` LocalFile LocalFile `toml:"local-file" comment:"Enables writing the latest blood sugar to a local temporary file."` Advanced Advanced `toml:"advanced" comment:"Advanced settings."` @@ -38,8 +37,9 @@ type LocalFile struct { } type Advanced struct { - FetchDelay Duration `toml:"fetch-delay" comment:"Time to wait before the next reading should be ready.\nIn testing, this seems to be about 20s behind, so the default is 30s to be safe.\nYour results may vary."` - RoundAge bool `toml:"round-age" comment:"If enabled, the reading's age will be rounded up to the nearest minute.\nNightscout rounds the age, so enable this if you want the values to match."` + FetchDelay Duration `toml:"fetch-delay" comment:"Time to wait before the next reading should be ready.\nIn testing, this seems to be about 20s behind, so the default is 30s to be safe.\nYour results may vary."` + FallbackInterval Duration `toml:"fallback-interval" comment:"Normally, readings will be fetched when ready (after ~5m).\nThis interval will be used if the next reading time cannot be estimated due to sensor warm-up, missed readings, errors, etc."` + RoundAge bool `toml:"round-age" comment:"If enabled, the reading's age will be rounded up to the nearest minute.\nNightscout rounds the age, so enable this if you want the values to match."` } const configDir = "nightscout-menu-bar" diff --git a/internal/config/default.go b/internal/config/default.go index 621ef71..631f880 100644 --- a/internal/config/default.go +++ b/internal/config/default.go @@ -9,9 +9,8 @@ const LocalFileFormatCsv = "csv" func NewDefault() *Config { return &Config{ - Title: "Nightscout", - Units: UnitsMgdl, - Interval: Duration{30 * time.Second}, + Title: "Nightscout", + Units: UnitsMgdl, Arrows: Arrows{ DoubleUp: "⇈", SingleUp: "↑", @@ -27,8 +26,9 @@ func NewDefault() *Config { Path: filepath.Join("$TMPDIR", "nightscout.csv"), }, Advanced: Advanced{ - FetchDelay: Duration{30 * time.Second}, - RoundAge: true, + FetchDelay: Duration{30 * time.Second}, + FallbackInterval: Duration{30 * time.Second}, + RoundAge: true, }, } } diff --git a/internal/config/load.go b/internal/config/load.go index 58dfac0..039a1f2 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -58,6 +58,10 @@ func (conf *Config) Load() error { return err } + if err := migrateConfig(k); err != nil { + return err + } + if err := k.UnmarshalWithConf("", &conf, koanf.UnmarshalConf{Tag: "toml"}); err != nil { return err } @@ -155,3 +159,14 @@ func InitLog() { TimeFormat: time.DateTime, }) } + +func migrateConfig(k *koanf.Koanf) error { + if k.Exists("interval") { + log.Info().Msg("Migrating config: interval to advanced.fallback-interval") + if err := k.Set("advanced.fallback-interval", k.Get("interval")); err != nil { + return err + } + } + + return nil +} diff --git a/internal/ticker/fetch.go b/internal/ticker/fetch.go index a668bb4..7ffc4a5 100644 --- a/internal/ticker/fetch.go +++ b/internal/ticker/fetch.go @@ -52,5 +52,5 @@ func (t *Ticker) Fetch(render chan<- *nightscout.Properties) time.Duration { } } } - return t.config.Interval.Duration + return t.config.Advanced.FallbackInterval.Duration }