Support environment variables in the config file

Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
Luca Bigliardi 2020-11-05 16:44:56 +01:00
parent 826f088241
commit aa63009ab4
3 changed files with 45 additions and 0 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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 {