2020-02-10 11:26:18 +00:00
|
|
|
package envflag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2020-02-10 14:08:04 +00:00
|
|
|
"log"
|
2020-02-10 11:26:18 +00:00
|
|
|
"os"
|
2020-02-24 19:14:22 +00:00
|
|
|
"strings"
|
2022-10-25 22:52:05 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envtemplate"
|
2020-02-10 11:26:18 +00:00
|
|
|
)
|
|
|
|
|
2020-03-30 12:51:19 +00:00
|
|
|
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. "+
|
2021-08-11 07:29:33 +00:00
|
|
|
"Flags are read only from command line if this flag isn't set. See https://docs.victoriametrics.com/#environment-variables for more details")
|
2020-03-30 12:51:19 +00:00
|
|
|
prefix = flag.String("envflag.prefix", "", "Prefix for environment variables if -envflag.enable is set")
|
|
|
|
)
|
2020-02-10 13:58:30 +00:00
|
|
|
|
2020-02-10 11:26:18 +00:00
|
|
|
// 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() {
|
2022-10-28 23:28:58 +00:00
|
|
|
ParseFlagSet(flag.CommandLine, os.Args[1:])
|
|
|
|
}
|
2022-10-25 22:52:05 +00:00
|
|
|
|
2022-10-28 23:28:58 +00:00
|
|
|
// ParseFlagSet parses the given args into the given fs.
|
|
|
|
func ParseFlagSet(fs *flag.FlagSet, args []string) {
|
|
|
|
args = expandArgs(args)
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
// Do not use lib/logger here, since it is uninitialized yet.
|
|
|
|
log.Fatalf("cannot parse flags %q: %s", args, err)
|
|
|
|
}
|
2020-02-10 13:58:30 +00:00
|
|
|
if !*enable {
|
|
|
|
return
|
|
|
|
}
|
2020-02-10 11:26:18 +00:00
|
|
|
// Remember explicitly set command-line flags.
|
|
|
|
flagsSet := make(map[string]bool)
|
2022-10-28 23:28:58 +00:00
|
|
|
fs.Visit(func(f *flag.Flag) {
|
2020-02-10 11:26:18 +00:00
|
|
|
flagsSet[f.Name] = true
|
|
|
|
})
|
|
|
|
|
|
|
|
// Obtain the remaining flag values from environment vars.
|
2022-10-28 23:28:58 +00:00
|
|
|
fs.VisitAll(func(f *flag.Flag) {
|
2020-02-10 11:26:18 +00:00
|
|
|
if flagsSet[f.Name] {
|
|
|
|
// The flag is explicitly set via command-line.
|
|
|
|
return
|
|
|
|
}
|
2020-02-10 13:58:30 +00:00
|
|
|
// Get flag value from environment var.
|
2020-02-24 19:14:22 +00:00
|
|
|
fname := getEnvFlagName(f.Name)
|
2022-10-26 11:49:20 +00:00
|
|
|
if v, ok := envtemplate.LookupEnv(fname); ok {
|
2022-10-28 23:28:58 +00:00
|
|
|
if err := fs.Set(f.Name, v); err != nil {
|
2020-02-10 14:08:04 +00:00
|
|
|
// Do not use lib/logger here, since it is uninitialized yet.
|
2022-10-28 23:28:58 +00:00
|
|
|
log.Fatalf("cannot set flag %s to %q, which is read from env var %q: %s", f.Name, v, fname, err)
|
2020-02-10 14:08:04 +00:00
|
|
|
}
|
2020-02-10 11:26:18 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-02-24 19:14:22 +00:00
|
|
|
|
2022-10-28 23:28:58 +00:00
|
|
|
// expandArgs substitutes %{ENV_VAR} placeholders inside args
|
|
|
|
// with the corresponding environment variable values.
|
|
|
|
func expandArgs(args []string) []string {
|
|
|
|
dstArgs := make([]string, 0, len(args))
|
|
|
|
for _, arg := range args {
|
|
|
|
s, err := envtemplate.ReplaceString(arg)
|
|
|
|
if err != nil {
|
|
|
|
// Do not use lib/logger here, since it is uninitialized yet.
|
|
|
|
log.Fatalf("cannot process arg %q: %s", arg, err)
|
|
|
|
}
|
|
|
|
if len(s) > 0 {
|
|
|
|
dstArgs = append(dstArgs, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dstArgs
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:22 +00:00
|
|
|
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.
|
2020-03-30 12:51:19 +00:00
|
|
|
s = strings.ReplaceAll(s, ".", "_")
|
|
|
|
return *prefix + s
|
2020-02-24 19:14:22 +00:00
|
|
|
}
|