mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-15 08:23:34 +01:00
1010a57882
Now flags can be set via environment vars with the same names as flags. Command-line flags override flags set via env vars. Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/311
33 lines
703 B
Go
33 lines
703 B
Go
package envflag
|
|
|
|
import (
|
|
"flag"
|
|
"os"
|
|
)
|
|
|
|
// 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()
|
|
|
|
// 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
|
|
}
|
|
if v, ok := os.LookupEnv(f.Name); ok {
|
|
f.Value.Set(v)
|
|
}
|
|
})
|
|
}
|