Skip to content

Commit

Permalink
Merge pull request #147 from kelseyhightower/get_all_envs
Browse files Browse the repository at this point in the history
backend: env uses pattern matching to mimic recursion
  • Loading branch information
kelseyhightower committed Oct 8, 2014
2 parents afbf1e4 + a9104d2 commit 9589292
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions backends/env/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ func NewEnvClient() (*Client, error) {

// GetValues queries the environment for keys
func (c *Client) GetValues(keys []string) (map[string]string, error) {
allEnvVars := os.Environ()
envMap := make(map[string]string)
for _, e := range allEnvVars {
index := strings.Index(e, "=")
envMap[e[:index]] = e[index+1:]
}
vars := make(map[string]string)
for _, key := range keys {
k := transform(key)
value := os.Getenv(k)
if value != "" {
vars[key] = value
for envKey, envValue := range envMap {
if strings.HasPrefix(envKey, k) {
vars[clean(envKey)] = envValue
}
}
}
return vars, nil
Expand All @@ -32,3 +39,10 @@ func transform(key string) string {
k := strings.TrimPrefix(key, "/")
return strings.ToUpper(replacer.Replace(k))
}

var cleanReplacer = strings.NewReplacer("_", "/")

func clean(key string) string {
newKey := "/" + key
return cleanReplacer.Replace(strings.ToLower(newKey))
}

0 comments on commit 9589292

Please sign in to comment.