Skip to content
Open
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
24 changes: 22 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package config

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"time"

log "github.com/golang/glog"
Expand All @@ -16,20 +19,37 @@ import (
// Load attempts to parse the given config file and return a Config object.
func Load(configFile string) (*Config, error) {
log.Infof("Loading configuration from %s", configFile)
buf, err := ioutil.ReadFile(configFile)
tmpl, err := template.ParseFiles(configFile)
if err != nil {
return nil, err
}

env := environmentAsMap()
var buf bytes.Buffer
log.Infof("Executing template from %s", configFile)
err = tmpl.Execute(&buf, env)
if err != nil {
return nil, err
}

c := Config{configFile: configFile}
err = yaml.Unmarshal(buf, &c)
err = yaml.Unmarshal(buf.Bytes(), &c)
if err != nil {
return nil, err
}

return &c, nil
}

func environmentAsMap() (env map[string]string) {
env = make(map[string]string)
for _, setting := range os.Environ() {
pair := strings.SplitN(setting, "=", 2)
env[pair[0]] = pair[1]
}
return
}

//
// Top-level config
//
Expand Down