From e1723fa3d43f3facc9b834d646b3a055b8eb7373 Mon Sep 17 00:00:00 2001 From: Karolis Rusenas Date: Tue, 28 Apr 2020 11:35:18 +0100 Subject: [PATCH] handle empty value set as require_zfs.sh sets it anyway --- cmd/dotmesh-server/main.go | 2 +- pkg/config/config.go | 25 +++++++++++++++++++++++-- pkg/config/load_test.go | 13 +++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/cmd/dotmesh-server/main.go b/cmd/dotmesh-server/main.go index d41de959..a05095a6 100644 --- a/cmd/dotmesh-server/main.go +++ b/cmd/dotmesh-server/main.go @@ -184,7 +184,7 @@ func main() { // Set the URL to an empty string (or leave it unset) to disable checkpoints if serverConfig.Upgrades.URL != "" { - checkInterval := serverConfig.Upgrades.IntervalSeconds + checkInterval := serverConfig.Upgrades.IntervalSeconds.Value() // This is the name that the checkpoint library looks for os.Setenv("CHECKPOINT_URL", serverConfig.Upgrades.URL) diff --git a/pkg/config/config.go b/pkg/config/config.go index e5009773..a6136206 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -21,8 +21,8 @@ type ( } Upgrades struct { - URL string `envconfig:"DOTMESH_UPGRADES_URL"` - IntervalSeconds int `default:"300" envconfig:"DOTMESH_UPGRADES_INTERVAL_SECONDS"` + URL string `envconfig:"DOTMESH_UPGRADES_URL"` + IntervalSeconds DefaultInt `default:"300" envconfig:"DOTMESH_UPGRADES_INTERVAL_SECONDS"` } } ) @@ -61,3 +61,24 @@ func (b *DefaultDuration) Decode(value string) error { func (b *DefaultDuration) Duration() time.Duration { return time.Duration(*b) } + +type DefaultInt int + +func (b *DefaultInt) Value() int { + return int(*b) +} + +func (b *DefaultInt) Decode(value string) error { + if value == "" { + *b = DefaultInt(0) + return nil + } + + i, err := strconv.Atoi(value) + if err != nil { + return err + } + + *b = DefaultInt(i) + return nil +} diff --git a/pkg/config/load_test.go b/pkg/config/load_test.go index 41dd12ea..6c3f6862 100644 --- a/pkg/config/load_test.go +++ b/pkg/config/load_test.go @@ -18,3 +18,16 @@ func TestLoadTime(t *testing.T) { t.Errorf("expected 1s, got: %d", cfg.PollDirty.SuccessTimeout) } } + +func TestLoadInterval(t *testing.T) { + os.Setenv("DOTMESH_UPGRADES_INTERVAL_SECONDS", "") + + cfg, err := Load() + if err != nil { + t.Errorf("failed to load: %s", err) + } + + if int(cfg.Upgrades.IntervalSeconds) != 0 { + t.Errorf("expected 0, got: %d", int(cfg.Upgrades.IntervalSeconds)) + } +}