From b47444e69d04f2937382c2f7f73e6897fd63a953 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 30 Mar 2020 15:51:19 +0300 Subject: [PATCH] lib/envflag: add `-envflag.prefix` for setting optional prefix for environment vars --- lib/envflag/envflag.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/envflag/envflag.go b/lib/envflag/envflag.go index 436704ba1..5ce002a11 100644 --- a/lib/envflag/envflag.go +++ b/lib/envflag/envflag.go @@ -7,9 +7,12 @@ import ( "strings" ) -var enable = flag.Bool("envflag.enable", false, "Whether to enable reading flags from environment variables additionally to command line. "+ - "Command line flag values have priority over values from environment vars. "+ - "Flags are read only from command line if this flag isn't set") +var ( + enable = flag.Bool("envflag.enable", false, "Whether to enable reading flags from environment variables additionally to command line. "+ + "Command line flag values have priority over values from environment vars. "+ + "Flags are read only from command line if this flag isn't set") + prefix = flag.String("envflag.prefix", "", "Prefix for environment variables if -envflag.enable is set") +) // Parse parses environment vars and command-line flags. // @@ -48,5 +51,6 @@ func Parse() { 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, ".", "_") + s = strings.ReplaceAll(s, ".", "_") + return *prefix + s }