diff --git a/README.md b/README.md index f0f98a9..c504f93 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,20 @@ $ go install github.com/google/alertmanager-irc-relay $ alertmanager-irc-relay --config /path/to/your/config/file ``` +The configuration file can reference environment variables. It is then possible +to specify certain parameters directly when running the bot: +``` +$ cat /path/to/your/config/file +... +http_port: $MY_SERVICE_PORT +... +irc_nickname_password: $NICKSERV_PASSWORD +... +$ export MY_SERVICE_PORT=8000 NICKSERV_PASSWORD=mynickserv_key +$ alertmanager-irc-relay --config /path/to/your/config/file +``` + + ### Prometheus configuration Prometheus can be configured following the official diff --git a/config.go b/config.go index 722c896..e554a82 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,7 @@ package main import ( "gopkg.in/yaml.v2" "io/ioutil" + "os" ) const ( @@ -68,6 +69,7 @@ func LoadConfig(configFile string) (*Config, error) { if err != nil { return nil, err } + data = []byte(os.ExpandEnv(string(data))) if err := yaml.Unmarshal(data, config); err != nil { return nil, err } diff --git a/config_test.go b/config_test.go index db98312..a029c37 100644 --- a/config_test.go +++ b/config_test.go @@ -80,6 +80,35 @@ func TestLoadGoodConfig(t *testing.T) { } } +func TestLoadWithEnvironmentVariables(t *testing.T) { + expectedNickPass := "mynickpass" + + os.Setenv("NICKSERV_PASSWORD", expectedNickPass) + defer os.Clearenv() + + tmpfile, err := ioutil.TempFile("", "airtestenvvarconfig") + if err != nil { + t.Errorf("Could not create tmpfile for testing: %s", err) + } + defer os.Remove(tmpfile.Name()) + + msgOnceConfigData := []byte("irc_nickname_password: $NICKSERV_PASSWORD") + if _, err := tmpfile.Write(msgOnceConfigData); err != nil { + t.Errorf("Could not write test data in tmpfile: %s", err) + } + tmpfile.Close() + + config, err := LoadConfig(tmpfile.Name()) + if config == nil { + t.Errorf("Expected a config, got: %s", err) + } + + if config.IRCNickPass != expectedNickPass { + t.Errorf("Loaded unexpected value: %s (expected: %s)", + config.IRCNickPass, expectedNickPass) + } +} + func TestLoadBadFile(t *testing.T) { tmpfile, err := ioutil.TempFile("", "airtestbadfile") if err != nil {