This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from jehiah/env_parsing_37
Better environment variable parsing
- Loading branch information
Showing
7 changed files
with
56 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
func LoadOptionsFromEnv(options interface{}, cfg map[string]interface{}) { | ||
val := reflect.ValueOf(options).Elem() | ||
typ := val.Type() | ||
for i := 0; i < typ.NumField(); i++ { | ||
// pull out the struct tags: | ||
// flag - the name of the command line flag | ||
// deprecated - (optional) the name of the deprecated command line flag | ||
// cfg - (optional, defaults to underscored flag) the name of the config file option | ||
field := typ.Field(i) | ||
flagName := field.Tag.Get("flag") | ||
envName := field.Tag.Get("env") | ||
cfgName := field.Tag.Get("cfg") | ||
if cfgName == "" && flagName != "" { | ||
cfgName = strings.Replace(flagName, "-", "_", -1) | ||
} | ||
if envName == "" || cfgName == "" { | ||
// resolvable fields must have the `env` and `cfg` struct tag | ||
continue | ||
} | ||
v := os.Getenv(envName) | ||
if v != "" { | ||
cfg[cfgName] = v | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters