From 0b744b8df23e9a5fda29b0368ca3bc6aceea5a45 Mon Sep 17 00:00:00 2001 From: mr-karan Date: Fri, 11 Jan 2019 15:15:04 +0530 Subject: [PATCH] feat: Add support for custom config file path --- README.md | 2 ++ main.go | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bcb458c..e1fbc01 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ You are now ready to send alerts to Google Chat! - Alertmanager has the ability of group similar alerts together and fire only one event, clubbing all the alerts data into one event. `calert` leverages this and sends all alerts in one message by looping over the alerts and passing data in the template. You can configure the rules for grouping the alerts in `alertmanager.yml` config. You can read more about it [here](https://github.com/prometheus/docs/blob/master/content/docs/alerting/alertmanager.md#grouping) +- You can use `--config.file` parameter to supply a custom config file path while running `calert`. + ## API - POST `/create?room_name=<>` (Used to receive new alerts and push to Google Chat) diff --git a/main.go b/main.go index ebb3a97..9c7257b 100644 --- a/main.go +++ b/main.go @@ -60,7 +60,9 @@ func initConfig() { os.Exit(0) } - viper.SetConfigName("config") + // Config Path flag. + flagSet.String("config.file", "", "Path to config file") + viper.SetDefault("server.address", ":5000") viper.SetDefault("server.socket", "/tmp/calert.sock") viper.SetDefault("server.name", "calert") @@ -68,13 +70,20 @@ func initConfig() { viper.SetDefault("server.write_timeout", 5000) viper.SetDefault("server.keepalive_timeout", 30000) viper.SetDefault("server.max_body_size", 5000) - // Process flags. flagSet.Parse(os.Args[1:]) viper.BindPFlags(flagSet) // Config file. - viper.AddConfigPath(".") + // check if config.file flag is passed and read from the file if it is set + configPath := viper.GetString("config.file") + if configPath != "" { + viper.SetConfigFile(configPath) + } else { + // fallback to default config. + viper.SetConfigName("config") + viper.AddConfigPath(".") + } err := viper.ReadInConfig() if err != nil { errLog.Fatalf("Error reading config: %s", err)