mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-15 16:30:55 +01:00
9c5db9400c
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
41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package envflag
|
|
|
|
import (
|
|
"flag"
|
|
"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.
|
|
//
|
|
// Flags set via command-line override flags set via environment vars.
|
|
//
|
|
// This function must be called instead of flag.Parse() before using any flags in the program.
|
|
func Parse() {
|
|
flag.Parse()
|
|
if !*enable {
|
|
return
|
|
}
|
|
|
|
// Remember explicitly set command-line flags.
|
|
flagsSet := make(map[string]bool)
|
|
flag.Visit(func(f *flag.Flag) {
|
|
flagsSet[f.Name] = true
|
|
})
|
|
|
|
// Obtain the remaining flag values from environment vars.
|
|
flag.VisitAll(func(f *flag.Flag) {
|
|
if flagsSet[f.Name] {
|
|
// The flag is explicitly set via command-line.
|
|
return
|
|
}
|
|
// Get flag value from environment var.
|
|
if v, ok := os.LookupEnv(f.Name); ok {
|
|
f.Value.Set(v)
|
|
}
|
|
})
|
|
}
|