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

replace environment variables #100

Merged
merged 1 commit into from
Jul 20, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}