mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
main: retrieve config from env vars
This commit is contained in:
parent
f17391a72b
commit
40c19e525c
24
main.go
24
main.go
@ -46,6 +46,8 @@ func init() {
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
getFlagsFromEnv()
|
||||
|
||||
if *proxyMode {
|
||||
startProxy()
|
||||
} else {
|
||||
@ -179,3 +181,25 @@ func (as *Addrs) Set(s string) error {
|
||||
func (as *Addrs) String() string {
|
||||
return strings.Join(*as, ",")
|
||||
}
|
||||
|
||||
// getFlagsFromEnv parses all registered flags in the global flagset,
|
||||
// and if they are not already set it attempts to set their values from
|
||||
// environment variables. Environment variables take the name of the flag but
|
||||
// are UPPERCASE, have the prefix "ETCD_", and any dashes are replaced by
|
||||
// underscores - for example: some-flag => ETCD_SOME_FLAG
|
||||
func getFlagsFromEnv() {
|
||||
alreadySet := make(map[string]bool)
|
||||
flag.Visit(func(f *flag.Flag) {
|
||||
alreadySet[f.Name] = true
|
||||
})
|
||||
flag.VisitAll(func(f *flag.Flag) {
|
||||
if !alreadySet[f.Name] {
|
||||
key := "ETCD_" + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
|
||||
val := os.Getenv(key)
|
||||
if val != "" {
|
||||
flag.Set(f.Name, val)
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user