From 909c90a558d3ecae701e104be8f58157be1faaff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Wed, 6 Jan 2021 17:25:00 +0100 Subject: [PATCH] fix environment variable names --- internal/flags/flags.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/internal/flags/flags.go b/internal/flags/flags.go index e3da941..f2decd0 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -330,10 +330,29 @@ Should only be used for testing.`) } -// SetDefaults provides default values for environment variables -func SetDefaults() { - day := (time.Hour * 24).Seconds() +func mustBindEnv(flag string, env string) { + if err := viper.BindEnv(flag, env); err != nil { + log.Fatalf("failed to bind env %q to flag %q: %v", env, flag, err) + } +} + +// SetEnvBindings binds environment variables to their corresponding config keys +func SetEnvBindings() { + + // Using WATCHTOWER as a prefix... + viper.SetEnvPrefix("WATCHTOWER") + // ...and replacing dashes with undescores + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) + // ...map all environment variables to corresponding flags in upper case viper.AutomaticEnv() + + // Aliases for non-matching ENV keys (backwards compability) + mustBindEnv("interval", "WATCHTOWER_POLL_INTERVAL") + + // Aliases for DOCKER_-prefixed env variables (matching those used for docker cli) + mustBindEnv("host", "DOCKER_HOST") + mustBindEnv("tlsverify", "DOCKER_TLS_VERIFY") + mustBindEnv("api-version", "DOCKER_API_VERSION") } // BindViperFlags binds the cmd PFlags to the viper configuration