Skip to content

Commit

Permalink
Merge pull request #78 from catatsuy/feature_add_notify_slack_interva…
Browse files Browse the repository at this point in the history
…l_via_env

make NOTIFY_SLACK_INTERVAL available
  • Loading branch information
catatsuy authored Feb 21, 2021
2 parents f449adf + 9f9f277 commit f03250a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
13 changes: 12 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,29 @@ 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)
reset3 := setTestEnv("NOTIFY_SLACK_CHANNEL", expectedChannel)
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)
Expand All @@ -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) {
Expand Down

0 comments on commit f03250a

Please sign in to comment.