mirror of
https://github.com/google/alertmanager-irc-relay.git
synced 2025-01-07 10:42:48 +01:00
Support environment variables in the config file
Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
parent
826f088241
commit
aa63009ab4
14
README.md
14
README.md
@ -71,6 +71,20 @@ $ go install github.com/google/alertmanager-irc-relay
|
|||||||
$ alertmanager-irc-relay --config /path/to/your/config/file
|
$ 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 configuration
|
||||||
|
|
||||||
Prometheus can be configured following the official
|
Prometheus can be configured following the official
|
||||||
|
@ -17,6 +17,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -68,6 +69,7 @@ func LoadConfig(configFile string) (*Config, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
data = []byte(os.ExpandEnv(string(data)))
|
||||||
if err := yaml.Unmarshal(data, config); err != nil {
|
if err := yaml.Unmarshal(data, config); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
func TestLoadBadFile(t *testing.T) {
|
||||||
tmpfile, err := ioutil.TempFile("", "airtestbadfile")
|
tmpfile, err := ioutil.TempFile("", "airtestbadfile")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user