Skip to content

Commit

Permalink
Support environmental variables in config.
Browse files Browse the repository at this point in the history
  • Loading branch information
chancez committed Sep 2, 2014
1 parent 7636395 commit 6ed5cb4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Bug Handling
Features
--------

* Support environmental variables in config files (#1023)

0.7.1 (2014-MM-DD)
==================

Expand Down
12 changes: 10 additions & 2 deletions cmd/hekad/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,20 @@ func LoadHekadConfig(configPath string) (config *HekadConfig, err error) {
continue
}
fPath := filepath.Join(configPath, fName)
if _, err = toml.DecodeFile(fPath, &configFile); err != nil {
contents, err := pipeline.ReplaceEnvsFile(fPath)
if err != nil {
return nil, err
}
if _, err = toml.Decode(contents, &configFile); err != nil {
return nil, fmt.Errorf("Error decoding config file: %s", err)
}
}
} else {
if _, err = toml.DecodeFile(configPath, &configFile); err != nil {
contents, err := pipeline.ReplaceEnvsFile(configPath)
if err != nil {
return nil, err
}
if _, err = toml.Decode(contents, &configFile); err != nil {
return nil, fmt.Errorf("Error decoding config file: %s", err)
}
}
Expand Down
17 changes: 17 additions & 0 deletions docs/source/config/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ Example hekad.toml file
.. end-hekad-toml
Using Environment Variables
===========================

If you wish to use environmental variables in your config files as a way to
configure values, you can simply use ``$VARIABLE_NAME`` or ``${VARIABLE_NAME}``
and the text will be replaced with the value of the environmental variable.

Example:

.. code-block:: ini
[AMQPInput]
url = "amqp://$USER:$PASSWORD@rabbitmq/"
exchange = "testout"
exchangeType = "fanout"
.. start-restarting
.. _configuring_restarting:
Expand Down
16 changes: 15 additions & 1 deletion pipeline/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"code.google.com/p/go-uuid/uuid"
"fmt"
"github.com/bbangert/toml"
"io/ioutil"
"log"
"os"
"reflect"
Expand Down Expand Up @@ -814,7 +815,12 @@ const protobufEncoderToml = `
func (self *PipelineConfig) LoadFromConfigFile(filename string) (err error) {
var configFile ConfigFile

if _, err = toml.DecodeFile(filename, &configFile); err != nil {
contents, err := ReplaceEnvsFile(filename)
if err != nil {
return err
}

if _, err = toml.Decode(contents, &configFile); err != nil {
return fmt.Errorf("Error decoding config file: %s", err)
}

Expand Down Expand Up @@ -960,3 +966,11 @@ func subsFromSection(section toml.Primitive) []string {
}
return subs
}

func ReplaceEnvsFile(path string) (string, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return os.ExpandEnv(string(contents)), nil
}

0 comments on commit 6ed5cb4

Please sign in to comment.