Browse Source

Merge pull request #57 from eyakubovich/master

Add support for configuration via env variables
Eugene Yakubovich 10 years ago
parent
commit
9de36f704f
1 changed files with 24 additions and 0 deletions
  1. 24 0
      main.go

+ 24 - 0
main.go

@@ -45,6 +45,28 @@ func init() {
 	flag.BoolVar(&opts.version, "version", false, "print version and exit")
 }
 
+// TODO: This is yet another copy (others found in etcd, fleet) -- Pull it out!
+// flagsFromEnv parses all registered flags in the given 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 given prefix, and any dashes are replaced by
+// underscores - for example: some-flag => PREFIX_SOME_FLAG
+func flagsFromEnv(prefix string, fs *flag.FlagSet) {
+	alreadySet := make(map[string]bool)
+	fs.Visit(func(f *flag.Flag) {
+		alreadySet[f.Name] = true
+	})
+	fs.VisitAll(func(f *flag.Flag) {
+		if !alreadySet[f.Name] {
+			key := strings.ToUpper(prefix + "_" + strings.Replace(f.Name, "-", "_", -1))
+			val := os.Getenv(key)
+			if val != "" {
+				fs.Set(f.Name, val)
+			}
+		}
+	})
+}
+
 func writeSubnetFile(sn *backend.SubnetDef) error {
 	// Write out the first usable IP by incrementing
 	// sn.IP by one
@@ -195,6 +217,8 @@ func main() {
 		os.Exit(0)
 	}
 
+	flagsFromEnv("FLANNELD", flag.CommandLine)
+
 	be, err := newBackend()
 	if err != nil {
 		log.Info(err)