diff --git a/README.md b/README.md index fadad68..c7bec26 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,7 @@ NOTIFY_SLACK_CHANNEL NOTIFY_SLACK_SNIPPET_CHANNEL NOTIFY_SLACK_USERNAME NOTIFY_SLACK_ICON_EMOJI +NOTIFY_SLACK_INTERVAL ``` It will be useful if you want to use it on a container. If you use it, you don't need a configuration file anymore. diff --git a/config/config.go b/config/config.go index b7178eb..3fc137f 100644 --- a/config/config.go +++ b/config/config.go @@ -54,6 +54,15 @@ func (c *Config) LoadEnv() error { c.IconEmoji = os.Getenv("NOTIFY_SLACK_ICON_EMOJI") } + durationStr := os.Getenv("NOTIFY_SLACK_INTERVAL") + if durationStr != "" { + duration, err := time.ParseDuration(durationStr) + if err != nil { + return fmt.Errorf("incorrect value to inteval option from NOTIFY_SLACK_INTERVAL: %s: %w", durationStr, err) + } + c.Duration = duration + } + return nil } diff --git a/config/config_test.go b/config/config_test.go index 3de823b..1f4a697 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -60,6 +60,8 @@ func TestLoadEnv(t *testing.T) { expectedSnippetChannel := "#general" expectedUsername := "deploy!" expectedIconEmoji := ":rocket:" + expectedIntervalStr := "2s" + expectedInterval := time.Duration(2 * time.Second) reset1 := setTestEnv("NOTIFY_SLACK_WEBHOOK_URL", expectedSlackURL) reset2 := setTestEnv("NOTIFY_SLACK_TOKEN", expectedToken) @@ -67,15 +69,20 @@ func TestLoadEnv(t *testing.T) { reset4 := setTestEnv("NOTIFY_SLACK_SNIPPET_CHANNEL", expectedSnippetChannel) reset5 := setTestEnv("NOTIFY_SLACK_USERNAME", expectedUsername) reset6 := setTestEnv("NOTIFY_SLACK_ICON_EMOJI", expectedIconEmoji) + reset7 := setTestEnv("NOTIFY_SLACK_INTERVAL", expectedIntervalStr) defer reset1() defer reset2() defer reset3() defer reset4() defer reset5() defer reset6() + defer reset7() c := NewConfig() - c.LoadEnv() + err := c.LoadEnv() + if err != nil { + t.Fatal(err) + } if c.SlackURL != expectedSlackURL { t.Errorf("got %s, want %s", c.SlackURL, expectedSlackURL) @@ -100,6 +107,10 @@ func TestLoadEnv(t *testing.T) { if c.IconEmoji != expectedIconEmoji { t.Errorf("got %s, want %s", c.IconEmoji, expectedIconEmoji) } + + if c.Duration != expectedInterval { + t.Errorf("got %+v, want %+v", c.Duration, expectedInterval) + } } func TestLoadTOMLFilename(t *testing.T) {