Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #100 from compose/88-env-vars
Browse files Browse the repository at this point in the history
replace environment variables
  • Loading branch information
nstott committed Jul 20, 2015
2 parents 5fb50d8 + f0caeee commit 2044639
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions cmd/transporter/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"bytes"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"regexp"
"time"

"gopkg.in/yaml.v2"
)

// A Config stores meta information about the transporter. This contains a
Expand Down Expand Up @@ -39,6 +40,9 @@ func LoadConfig(filename string) (config Config, err error) {
return
}

// configs can have environment variables, replace these before continuing
ba = setConfigEnvironment(ba)

err = yaml.Unmarshal(ba, &config)

for k, v := range config.Nodes {
Expand All @@ -56,3 +60,21 @@ func LoadConfig(filename string) (config Config, err error) {

return
}

// setConfigEnvironment replaces environment variables marked in the form ${FOO} with the
// value stored in the environment variable `FOO`
func setConfigEnvironment(ba []byte) []byte {
re := regexp.MustCompile(`\$\{([a-zA-Z0-9]+)\}`)

matches := re.FindAllSubmatch(ba, -1)
if matches == nil {
return ba
}

for _, m := range matches {
v := os.Getenv(string(m[1]))
ba = bytes.Replace(ba, m[0], []byte(v), -1)
}

return ba
}

0 comments on commit 2044639

Please sign in to comment.