mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-02-09 15:27:11 +00:00
lib/envflag: add -envflag.enable
command-line flag for enabling reading flags from environment vars
By default flags are read only from command line. They can be read from environment vars if `-envflag.enable` is set. Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/311
This commit is contained in:
parent
8466ab0034
commit
5d207b2025
1 changed files with 8 additions and 0 deletions
|
@ -5,6 +5,10 @@ import (
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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 envoronment vars. "+
|
||||||
|
"Flags are read only from command line if this flag isn't set")
|
||||||
|
|
||||||
// Parse parses environment vars and command-line flags.
|
// Parse parses environment vars and command-line flags.
|
||||||
//
|
//
|
||||||
// Flags set via command-line override flags set via environment vars.
|
// Flags set via command-line override flags set via environment vars.
|
||||||
|
@ -12,6 +16,9 @@ import (
|
||||||
// This function must be called instead of flag.Parse() before using any flags in the program.
|
// This function must be called instead of flag.Parse() before using any flags in the program.
|
||||||
func Parse() {
|
func Parse() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
if !*enable {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Remember explicitly set command-line flags.
|
// Remember explicitly set command-line flags.
|
||||||
flagsSet := make(map[string]bool)
|
flagsSet := make(map[string]bool)
|
||||||
|
@ -25,6 +32,7 @@ func Parse() {
|
||||||
// The flag is explicitly set via command-line.
|
// The flag is explicitly set via command-line.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// Get flag value from environment var.
|
||||||
if v, ok := os.LookupEnv(f.Name); ok {
|
if v, ok := os.LookupEnv(f.Name); ok {
|
||||||
f.Value.Set(v)
|
f.Value.Set(v)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue