From 13ee8271d08c79dc5679be65e9db5bd3181f034f Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 24 Feb 2020 21:14:22 +0200 Subject: [PATCH] lib/envflag: substitute dots with underscores in env var names if -envflag.enable is set See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/311 --- lib/envflag/envflag.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/envflag/envflag.go b/lib/envflag/envflag.go index 80f5579170..4bf9d74473 100644 --- a/lib/envflag/envflag.go +++ b/lib/envflag/envflag.go @@ -4,6 +4,7 @@ import ( "flag" "log" "os" + "strings" ) var enable = flag.Bool("envflag.enable", false, "Whether to enable reading flags from environment variables additionally to command line. "+ @@ -34,11 +35,18 @@ func Parse() { return } // Get flag value from environment var. - if v, ok := os.LookupEnv(f.Name); ok { + fname := getEnvFlagName(f.Name) + if v, ok := os.LookupEnv(fname); ok { if err := f.Value.Set(v); err != nil { // Do not use lib/logger here, since it is uninitialized yet. - log.Fatalf("cannot set flag %s to %q, which is read from environment variable: %s", f.Name, v, err) + log.Fatalf("cannot set flag %s to %q, which is read from environment variable %q: %s", f.Name, v, fname, err) } } }) } + +func getEnvFlagName(s string) string { + // Substitute dots with underscores, since env var names cannot contain dots. + // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/311#issuecomment-586354129 for details. + return strings.ReplaceAll(s, ".", "_") +}