From 16926e73d3b5b01f45e0bdc81790836083c497d0 Mon Sep 17 00:00:00 2001 From: Shahar Levy Date: Thu, 18 Jan 2024 18:52:27 +0200 Subject: [PATCH 1/4] Added more configuration capabilities --- internal/sync/config.go | 61 ++++++++++++++++++++++------------ internal/sync/config_test.go | 27 +++++++++++++++ internal/sync/env.go | 40 ++++++++++++++++++++++ internal/sync/schedule_sync.go | 45 ++++++++++++++----------- 4 files changed, 131 insertions(+), 42 deletions(-) create mode 100644 internal/sync/env.go diff --git a/internal/sync/config.go b/internal/sync/config.go index 0a7f18b..77f5d9d 100644 --- a/internal/sync/config.go +++ b/internal/sync/config.go @@ -3,18 +3,23 @@ package sync import ( "fmt" "os" - "strconv" "strings" "time" ) const ( - scheduleKeyPrefix = "SCHEDULE_" - pagerDutyTokenKey = "PAGERDUTY_TOKEN" - slackTokenKey = "SLACK_TOKEN" - runInterval = "RUN_INTERVAL_SECONDS" - pdScheduleLookaheadKey = "PAGERDUTY_SCHEDULE_LOOKAHEAD" - runIntervalDefault = 60 + scheduleKeyPrefix = "SCHEDULE_" + pagerDutyTokenKey = "PAGERDUTY_TOKEN" + slackTokenKey = "SLACK_TOKEN" + runInterval = "RUN_INTERVAL_SECONDS" + pdScheduleLookaheadKey = "PAGERDUTY_SCHEDULE_LOOKAHEAD" + currentOnCallFormat = "USER_GROUP_CURRENT_ON_CALL_FORMAT" + currentOnCallEnabled = "USER_GROUP_CURRENT_ON_CALL_ENABLED" + allOnCallFormat = "USER_GROUP_ALL_ON_CALL_FORMAT" + allOnCallEnabled = "USER_GROUP_ALL_ON_CALL_ENABLED" + currentOnCallFormatDefault = "current-oncall-%s" + allOnCallFormatDefault = "all-oncall-%ss" + runIntervalDefault = 60 ) // Config is used to configure application @@ -26,6 +31,10 @@ type Config struct { SlackToken string RunIntervalInSeconds int PagerdutyScheduleLookahead time.Duration + CurrentOnCallNameFormat string + AllOnCallNameFormat string + CurrentOnCallEnabled bool + AllOnCallEnabled bool } // Schedule models a PagerDuty schedule that will be synced with Slack @@ -45,15 +54,13 @@ type Schedule struct { // ScheduleID = "1234", AllOnCallGroupName = "all-oncall-platform-engineers", CurrentOnCallGroupName: "current-oncall-platform-engineer" func NewConfigFromEnv() (*Config, error) { config := &Config{ - PagerDutyToken: os.Getenv(pagerDutyTokenKey), - SlackToken: os.Getenv(slackTokenKey), - RunIntervalInSeconds: runIntervalDefault, - } - - runInterval := os.Getenv(runInterval) - v, err := strconv.Atoi(runInterval) - if err == nil { - config.RunIntervalInSeconds = v + PagerDutyToken: os.Getenv(pagerDutyTokenKey), + SlackToken: os.Getenv(slackTokenKey), + RunIntervalInSeconds: GetEnvInt(runInterval, runIntervalDefault), + CurrentOnCallNameFormat: GetEnvStr(currentOnCallFormat, currentOnCallFormatDefault), + CurrentOnCallEnabled: GetEnvBool(currentOnCallEnabled, true), + AllOnCallNameFormat: GetEnvStr(allOnCallFormat, allOnCallFormatDefault), + AllOnCallEnabled: GetEnvBool(allOnCallEnabled, true), } pagerdutyScheduleLookahead, err := getPagerdutyScheduleLookahead() @@ -70,7 +77,7 @@ func NewConfigFromEnv() (*Config, error) { return nil, fmt.Errorf("expecting schedule value to be a comma separated scheduleId,name but got %s", value) } - config.Schedules = appendSchedule(config.Schedules, scheduleValues[0], scheduleValues[1]) + config.Schedules = appendSchedule(config, scheduleValues[0], scheduleValues[1]) } } @@ -81,13 +88,23 @@ func NewConfigFromEnv() (*Config, error) { return config, nil } -func appendSchedule(schedules []Schedule, scheduleID, teamName string) []Schedule { - currentGroupName := fmt.Sprintf("current-oncall-%s", teamName) - allGroupName := fmt.Sprintf("all-oncall-%ss", teamName) - newScheduleList := make([]Schedule, len(schedules)) +func getEnvOrDefault(key, defaultValue string) string { + value := os.Getenv(key) + if len(value) == 0 { + return defaultValue + } + return value +} + +func appendSchedule(config *Config, scheduleID, teamName string) []Schedule { + + currentGroupName := fmt.Sprintf(config.CurrentOnCallNameFormat, teamName) + allGroupName := fmt.Sprintf(config.AllOnCallNameFormat, teamName) + + newScheduleList := make([]Schedule, len(config.Schedules)) updated := false - for i, s := range schedules { + for i, s := range config.Schedules { if s.CurrentOnCallGroupName != currentGroupName { newScheduleList[i] = s diff --git a/internal/sync/config_test.go b/internal/sync/config_test.go index 93e267d..c276e72 100644 --- a/internal/sync/config_test.go +++ b/internal/sync/config_test.go @@ -33,6 +33,33 @@ func Test_NewConfigFromEnv_SingleScheduleDefined(t *testing.T) { config.Schedules)) } +func Test_NewConfigFromEnv_FormatsChanged(t *testing.T) { + defer SetEnv("SCHEDULE_PLATFORM", "1234,platform-engineer")() + defer SetEnv("PAGERDUTY_TOKEN", "token1")() + defer SetEnv("SLACK_TOKEN", "secretToken1")() + defer SetEnv("RUN_INTERVAL_SECONDS", "10")() + defer SetEnv("USER_GROUP_CURRENT_ON_CALL_FORMAT", "pd-%s")() + defer SetEnv("USER_GROUP_ALL_ON_CALL_FORMAT", "all-%ss")() + + config, err := NewConfigFromEnv() + + assert.NoError(t, err) + assert.Equal(t, "token1", config.PagerDutyToken) + assert.Equal(t, "secretToken1", config.SlackToken) + assert.Equal(t, 10, config.RunIntervalInSeconds) + assert.Equal(t, time.Hour*24*100, config.PagerdutyScheduleLookahead) + assert.Equal(t, 1, len(config.Schedules)) + assert.Equal(t, "all-platform-engineers", config.Schedules[0].AllOnCallGroupName) + assert.Equal(t, "pd-platform-engineer", config.Schedules[0].CurrentOnCallGroupName) + + assert.True(t, assert.ObjectsAreEqualValues([]Schedule{{ + ScheduleIDs: []string{"1234"}, + AllOnCallGroupName: "all-platform-engineers", + CurrentOnCallGroupName: "pd-platform-engineer", + }}, + config.Schedules)) +} + func Test_NewConfigFromEnv_SingleScheduleDefinedWithDefaultRunInterval(t *testing.T) { defer SetEnv("SCHEDULE_PLATFORM", "1234,platform-engineer")() defer SetEnv("PAGERDUTY_TOKEN", "token1")() diff --git a/internal/sync/env.go b/internal/sync/env.go new file mode 100644 index 0000000..4234e70 --- /dev/null +++ b/internal/sync/env.go @@ -0,0 +1,40 @@ +package sync + +import ( + "os" + "strconv" +) + +func GetEnvStr(key string, d string) string { + v := os.Getenv(key) + if v == "" { + return d + } + return v +} + +func GetEnvInt(key string, d int) int { + s, ok := os.LookupEnv(key) + if !ok { + return d + } + + v, err := strconv.Atoi(s) + if err != nil { + return d + } + return v +} + +func GetEnvBool(key string, d bool) bool { + s, ok := os.LookupEnv(key) + if !ok { + return d + } + + v, err := strconv.ParseBool(s) + if err != nil { + return d + } + return v +} diff --git a/internal/sync/schedule_sync.go b/internal/sync/schedule_sync.go index 74f81c5..97d3456 100644 --- a/internal/sync/schedule_sync.go +++ b/internal/sync/schedule_sync.go @@ -58,32 +58,37 @@ func Schedules(config *Config) error { } for _, schedule := range config.Schedules { - logrus.Infof("checking slack group: %s", schedule.CurrentOnCallGroupName) - currentOncallEngineerEmails, err := getEmailsForSchedules(schedule.ScheduleIDs, time.Second) - if err != nil { - logrus.Errorf("failed to get emails for %s: %v", schedule.CurrentOnCallGroupName, err) - continue - } + if config.CurrentOnCallEnabled { + logrus.Infof("checking slack group: %s", schedule.CurrentOnCallGroupName) - err = updateSlackGroup(currentOncallEngineerEmails, schedule.CurrentOnCallGroupName) - if err != nil { - logrus.Errorf("failed to update slack group %s: %v", schedule.CurrentOnCallGroupName, err) - continue + currentOncallEngineerEmails, err := getEmailsForSchedules(schedule.ScheduleIDs, time.Second) + if err != nil { + logrus.Errorf("failed to get emails for %s: %v", schedule.CurrentOnCallGroupName, err) + continue + } + + err = updateSlackGroup(currentOncallEngineerEmails, schedule.CurrentOnCallGroupName) + if err != nil { + logrus.Errorf("failed to update slack group %s: %v", schedule.CurrentOnCallGroupName, err) + continue + } } - logrus.Infof("checking slack group: %s", schedule.AllOnCallGroupName) + if config.AllOnCallEnabled { + logrus.Infof("checking slack group: %s", schedule.AllOnCallGroupName) - allOncallEngineerEmails, err := getEmailsForSchedules(schedule.ScheduleIDs, config.PagerdutyScheduleLookahead) - if err != nil { - logrus.Errorf("failed to get emails for %s: %v", schedule.AllOnCallGroupName, err) - continue - } + allOncallEngineerEmails, err := getEmailsForSchedules(schedule.ScheduleIDs, config.PagerdutyScheduleLookahead) + if err != nil { + logrus.Errorf("failed to get emails for %s: %v", schedule.AllOnCallGroupName, err) + continue + } - err = updateSlackGroup(allOncallEngineerEmails, schedule.AllOnCallGroupName) - if err != nil { - logrus.Errorf("failed to update slack group %s: %v", schedule.AllOnCallGroupName, err) - continue + err = updateSlackGroup(allOncallEngineerEmails, schedule.AllOnCallGroupName) + if err != nil { + logrus.Errorf("failed to update slack group %s: %v", schedule.AllOnCallGroupName, err) + continue + } } } From 1ebce537c778b909cb99fbca8d0e431acac114a0 Mon Sep 17 00:00:00 2001 From: Shahar Levy Date: Sun, 21 Jan 2024 17:42:08 +0200 Subject: [PATCH 2/4] Added configuration from yaml --- internal/sync/config.go | 63 ++++++------ ...config_test.go => config_from_env_test.go} | 0 internal/sync/config_from_yaml_test.go | 40 ++++++++ internal/sync/config_yaml.go | 98 +++++++++++++++++++ internal/sync/{env.go => defaults.go} | 0 internal/sync/example.yaml | 18 ++++ 6 files changed, 184 insertions(+), 35 deletions(-) rename internal/sync/{config_test.go => config_from_env_test.go} (100%) create mode 100644 internal/sync/config_from_yaml_test.go create mode 100644 internal/sync/config_yaml.go rename internal/sync/{env.go => defaults.go} (100%) create mode 100644 internal/sync/example.yaml diff --git a/internal/sync/config.go b/internal/sync/config.go index 77f5d9d..20dfb2e 100644 --- a/internal/sync/config.go +++ b/internal/sync/config.go @@ -8,18 +8,20 @@ import ( ) const ( - scheduleKeyPrefix = "SCHEDULE_" - pagerDutyTokenKey = "PAGERDUTY_TOKEN" - slackTokenKey = "SLACK_TOKEN" - runInterval = "RUN_INTERVAL_SECONDS" - pdScheduleLookaheadKey = "PAGERDUTY_SCHEDULE_LOOKAHEAD" - currentOnCallFormat = "USER_GROUP_CURRENT_ON_CALL_FORMAT" - currentOnCallEnabled = "USER_GROUP_CURRENT_ON_CALL_ENABLED" - allOnCallFormat = "USER_GROUP_ALL_ON_CALL_FORMAT" - allOnCallEnabled = "USER_GROUP_ALL_ON_CALL_ENABLED" + scheduleKeyPrefix = "SCHEDULE_" + pagerDutyTokenKey = "PAGERDUTY_TOKEN" + slackTokenKey = "SLACK_TOKEN" + runInterval = "RUN_INTERVAL_SECONDS" + pdScheduleLookaheadKey = "PAGERDUTY_SCHEDULE_LOOKAHEAD" + currentOnCallFormat = "USER_GROUP_CURRENT_ON_CALL_FORMAT" + currentOnCallEnabled = "USER_GROUP_CURRENT_ON_CALL_ENABLED" + allOnCallFormat = "USER_GROUP_ALL_ON_CALL_FORMAT" + allOnCallEnabled = "USER_GROUP_ALL_ON_CALL_ENABLED" + + pagerDutyLookAheadDefault = time.Hour * 24 * 100 + runIntervalDefault = 60 currentOnCallFormatDefault = "current-oncall-%s" allOnCallFormatDefault = "all-oncall-%ss" - runIntervalDefault = 60 ) // Config is used to configure application @@ -31,8 +33,6 @@ type Config struct { SlackToken string RunIntervalInSeconds int PagerdutyScheduleLookahead time.Duration - CurrentOnCallNameFormat string - AllOnCallNameFormat string CurrentOnCallEnabled bool AllOnCallEnabled bool } @@ -54,21 +54,23 @@ type Schedule struct { // ScheduleID = "1234", AllOnCallGroupName = "all-oncall-platform-engineers", CurrentOnCallGroupName: "current-oncall-platform-engineer" func NewConfigFromEnv() (*Config, error) { config := &Config{ - PagerDutyToken: os.Getenv(pagerDutyTokenKey), - SlackToken: os.Getenv(slackTokenKey), - RunIntervalInSeconds: GetEnvInt(runInterval, runIntervalDefault), - CurrentOnCallNameFormat: GetEnvStr(currentOnCallFormat, currentOnCallFormatDefault), - CurrentOnCallEnabled: GetEnvBool(currentOnCallEnabled, true), - AllOnCallNameFormat: GetEnvStr(allOnCallFormat, allOnCallFormatDefault), - AllOnCallEnabled: GetEnvBool(allOnCallEnabled, true), + PagerDutyToken: os.Getenv(pagerDutyTokenKey), + SlackToken: os.Getenv(slackTokenKey), + RunIntervalInSeconds: GetEnvInt(runInterval, runIntervalDefault), + CurrentOnCallEnabled: GetEnvBool(currentOnCallEnabled, true), + AllOnCallEnabled: GetEnvBool(allOnCallEnabled, true), } - pagerdutyScheduleLookahead, err := getPagerdutyScheduleLookahead() + pagerdutyScheduleLookahead, err := GetPagerdutyScheduleLookahead() if err != nil { return nil, err } + config.PagerdutyScheduleLookahead = pagerdutyScheduleLookahead + currentOnCallNameFormat := GetEnvStr(currentOnCallFormat, currentOnCallFormatDefault) + allOnCallNameFormat := GetEnvStr(allOnCallFormat, allOnCallFormatDefault) + for _, key := range os.Environ() { if strings.HasPrefix(key, scheduleKeyPrefix) { value := strings.Split(key, "=")[1] @@ -77,7 +79,7 @@ func NewConfigFromEnv() (*Config, error) { return nil, fmt.Errorf("expecting schedule value to be a comma separated scheduleId,name but got %s", value) } - config.Schedules = appendSchedule(config, scheduleValues[0], scheduleValues[1]) + config.Schedules = appendSchedule(config, scheduleValues[0], scheduleValues[1], currentOnCallNameFormat, allOnCallNameFormat) } } @@ -88,18 +90,10 @@ func NewConfigFromEnv() (*Config, error) { return config, nil } -func getEnvOrDefault(key, defaultValue string) string { - value := os.Getenv(key) - if len(value) == 0 { - return defaultValue - } - return value -} - -func appendSchedule(config *Config, scheduleID, teamName string) []Schedule { +func appendSchedule(config *Config, scheduleID, teamName string, currentOnCallNameFormat string, allOnCallNameFormat string) []Schedule { - currentGroupName := fmt.Sprintf(config.CurrentOnCallNameFormat, teamName) - allGroupName := fmt.Sprintf(config.AllOnCallNameFormat, teamName) + currentGroupName := fmt.Sprintf(currentOnCallNameFormat, teamName) + allGroupName := fmt.Sprintf(allOnCallNameFormat, teamName) newScheduleList := make([]Schedule, len(config.Schedules)) updated := false @@ -131,12 +125,11 @@ func appendSchedule(config *Config, scheduleID, teamName string) []Schedule { return newScheduleList } -func getPagerdutyScheduleLookahead() (time.Duration, error) { - result := time.Hour * 24 * 100 +func GetPagerdutyScheduleLookahead() (time.Duration, error) { pdScheduleLookahead, ok := os.LookupEnv(pdScheduleLookaheadKey) if !ok { - return result, nil + return pagerDutyLookAheadDefault, nil } v, err := time.ParseDuration(pdScheduleLookahead) diff --git a/internal/sync/config_test.go b/internal/sync/config_from_env_test.go similarity index 100% rename from internal/sync/config_test.go rename to internal/sync/config_from_env_test.go diff --git a/internal/sync/config_from_yaml_test.go b/internal/sync/config_from_yaml_test.go new file mode 100644 index 0000000..75381cb --- /dev/null +++ b/internal/sync/config_from_yaml_test.go @@ -0,0 +1,40 @@ +package sync + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +const ( + filePath = "example.yaml" +) + +func Test_NewConfigFromYaml_SingleScheduleDefined(t *testing.T) { + config, err := NewConfigFromYaml(filePath) + + assert.NoError(t, err) + assert.Equal(t, "token1", config.PagerDutyToken) + assert.Equal(t, "secretToken1", config.SlackToken) + assert.Equal(t, 600, config.RunIntervalInSeconds) + assert.Equal(t, time.Hour*24, config.PagerdutyScheduleLookahead) + assert.Equal(t, 2, len(config.Schedules)) + assert.Equal(t, true, config.CurrentOnCallEnabled) + assert.Equal(t, false, config.AllOnCallEnabled) + + firstSchedule := config.Schedules[0] + assert.Equal(t, "all-oncall-platform-engineers", firstSchedule.AllOnCallGroupName) + assert.Equal(t, "current-oncall-platform-engineer", firstSchedule.CurrentOnCallGroupName) + + assert.True(t, assert.ObjectsAreEqualValues([]Schedule{ + { + ScheduleIDs: []string{"1234"}, + AllOnCallGroupName: "all-oncall-platform-engineers", + CurrentOnCallGroupName: "current-oncall-platform-engineer", + }, { + ScheduleIDs: []string{"123", "12345"}, + AllOnCallGroupName: "all-company-wide", + CurrentOnCallGroupName: "pd-company-wide", + }}, + config.Schedules)) +} diff --git a/internal/sync/config_yaml.go b/internal/sync/config_yaml.go new file mode 100644 index 0000000..4a31214 --- /dev/null +++ b/internal/sync/config_yaml.go @@ -0,0 +1,98 @@ +package sync + +import ( + "fmt" + "gopkg.in/yaml.v3" + "log" + "os" + "time" +) + +type ConfigYaml struct { + RunInterval int `yaml:"run-interval"` + PagerDutyLookAheadDuration time.Duration `yaml:"pager-duty-look-ahead-duration"` + PagerDutyToken string `yaml:"pager-duty-token"` + SlackToken string `yaml:"slack-token"` + CurrentOnCallEnabled bool `yaml:"current-on-call-enabled"` + CurrentOnCallNameFormat string `yaml:"current-on-call-name-format"` + AllOnCallEnabled bool `yaml:"all-on-call-enabled"` + AllOnCallNameFormat string `yaml:"all-on-call-name-format"` + UserGroups []UserGroup `yaml:"user-groups"` +} + +type UserGroup struct { + Name string `yaml:"name"` + ScheduleIDs []string `yaml:"schedules"` + CurrentOnCallEnabled bool `yaml:"current-on-call-enabled"` + CurrentOnCallNameFormat string `yaml:"current-on-call-name-format"` + AllOnCallEnabled bool `yaml:"all-on-call-enabled"` + AllOnCallNameFormat string `yaml:"all-on-call-name-format"` +} + +func NewConfigFromYaml(filePath string) (*Config, error) { + fmt.Printf("Reading YAML file %s\n", filePath) + data, err := os.ReadFile(filePath) + if err != nil { + log.Fatalf("Error reading YAML file: %v", err) + } + + var configYaml ConfigYaml + err = yaml.Unmarshal(data, &configYaml) + if err != nil { + log.Fatalf("Error unmarshaling YAML: %v", err) + } + + if len(configYaml.UserGroups) == 0 { + return nil, fmt.Errorf("expecting at least one user-groups defined in YAML file") + } + + defaultCurrentOnCallNameFormat := getOrDefault(configYaml.CurrentOnCallNameFormat, currentOnCallFormatDefault) + defaultAllOnCallNameFormat := getOrDefault(configYaml.AllOnCallNameFormat, allOnCallFormatDefault) + + scheduleList := make([]Schedule, len(configYaml.UserGroups)) + + fmt.Printf("Creating %d user-groups.\n", len(scheduleList)) + for i, s := range configYaml.UserGroups { + fmt.Printf("Registering user-group %s.\n", s.Name) + + currentGroupName := fmt.Sprintf(getOrDefault(s.CurrentOnCallNameFormat, defaultCurrentOnCallNameFormat), s.Name) + allGroupName := fmt.Sprintf(getOrDefault(s.AllOnCallNameFormat, defaultAllOnCallNameFormat), s.Name) + + fmt.Printf("Registering schedules(%q) for user-groups(%s, %s).\n", s.ScheduleIDs, currentGroupName, allGroupName) + scheduleList[i] = Schedule{ + ScheduleIDs: s.ScheduleIDs, + CurrentOnCallGroupName: currentGroupName, + AllOnCallGroupName: allGroupName, + } + + } + + runIntervalInSeconds := configYaml.RunInterval + if runIntervalInSeconds == 0 { + runIntervalInSeconds = runIntervalDefault + } + + pagerdutyScheduleLookahead := configYaml.PagerDutyLookAheadDuration + if pagerdutyScheduleLookahead == 0 { + pagerdutyScheduleLookahead = pagerDutyLookAheadDefault + } + + config := &Config{ + Schedules: scheduleList, + PagerDutyToken: configYaml.PagerDutyToken, + SlackToken: configYaml.SlackToken, + PagerdutyScheduleLookahead: pagerdutyScheduleLookahead, + RunIntervalInSeconds: runIntervalInSeconds, + CurrentOnCallEnabled: configYaml.CurrentOnCallEnabled, + AllOnCallEnabled: configYaml.AllOnCallEnabled, + } + + return config, nil +} + +func getOrDefault(format string, formatDefault string) string { + if format == "" { + return formatDefault + } + return format +} diff --git a/internal/sync/env.go b/internal/sync/defaults.go similarity index 100% rename from internal/sync/env.go rename to internal/sync/defaults.go diff --git a/internal/sync/example.yaml b/internal/sync/example.yaml new file mode 100644 index 0000000..54fa9b1 --- /dev/null +++ b/internal/sync/example.yaml @@ -0,0 +1,18 @@ +run-interval: 600 +pager-duty-look-ahead-duration: 24h +pager-duty-token: "token1" +slack-token: "secretToken1" +current-on-call-enabled: true +all-on-call-enabled: false +user-groups: + - user-group: + name: platform-engineer + schedules: + - 1234 + - user-group: + name: company-wide + current-on-call-name-format: "pd-%s" + all-on-call-name-format: "all-%s" + schedules: + - 123 + - 12345 \ No newline at end of file From c5ae1b7bac305bc0d5a868e96295210b8cbd6776 Mon Sep 17 00:00:00 2001 From: Shahar Levy Date: Sun, 21 Jan 2024 17:51:56 +0200 Subject: [PATCH 3/4] Added main option for using file --- cmd/pagerduty-slack-sync/main.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cmd/pagerduty-slack-sync/main.go b/cmd/pagerduty-slack-sync/main.go index 3b54fd8..6ed545f 100644 --- a/cmd/pagerduty-slack-sync/main.go +++ b/cmd/pagerduty-slack-sync/main.go @@ -16,7 +16,8 @@ func main() { stop := make(chan os.Signal, 1) // Buffer size of 1 signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) - config, err := sync.NewConfigFromEnv() + config, err := getConfig() + if err != nil { logrus.Errorf("could not parse config, error: %v", err) os.Exit(-1) @@ -43,3 +44,14 @@ func main() { } } } + +func getConfig() (*sync.Config, error) { + configYamlFilePath := os.Getenv("CONFIG_YAML_FILE_PATH") + if configYamlFilePath != "" { + logrus.Infof("Getting configuration from yaml config %s", configYamlFilePath) + return sync.NewConfigFromYaml(configYamlFilePath) + } else { + logrus.Infof("Getting configuration from environment variables") + return sync.NewConfigFromEnv() + } +} From 5b6b4afee012ac1c5c1e33159745718d15ee3a16 Mon Sep 17 00:00:00 2001 From: Shahar Levy Date: Sun, 21 Jan 2024 18:18:27 +0200 Subject: [PATCH 4/4] Better logging --- cmd/pagerduty-slack-sync/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/pagerduty-slack-sync/main.go b/cmd/pagerduty-slack-sync/main.go index 6ed545f..b79d11f 100644 --- a/cmd/pagerduty-slack-sync/main.go +++ b/cmd/pagerduty-slack-sync/main.go @@ -24,7 +24,7 @@ func main() { return } - logrus.Infof("starting, going to sync %d schedules", len(config.Schedules)) + logrus.Infof("starting, going to sync %d schedules every %d seconds.", len(config.Schedules), config.RunIntervalInSeconds) timer := time.NewTicker(time.Second * time.Duration(config.RunIntervalInSeconds))