Skip to content

Commit

Permalink
Inject a noop slack client in case no slack token is set (#13)
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
julienduchesne authored Feb 11, 2022
1 parent 02d875a commit fdd7998
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/handlers/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ type launchHandler struct {

// NewLaunchHandler returns an handler that launches a k6 load test.
func NewLaunchHandler(client k6.Client, kubeClient kubernetes.Interface, slackClient slack.Client) (http.Handler, error) {
if slackClient == nil {
return nil, errors.New("unexpected state. Slack client is nil")
}

return &launchHandler{
client: client,
kubeClient: kubeClient,
Expand Down
3 changes: 2 additions & 1 deletion pkg/slack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ type slackClientWrapper struct {

func NewClient(token string) Client {
if token == "" {
return nil
return &noopClient{}
}

return &slackClientWrapper{
client: slack.New(token),
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/slack/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package slack

import (
log "github.com/sirupsen/logrus"
)

type noopClient struct{}

func (c *noopClient) SendMessages(channels []string, text, context string) (map[string]string, error) {
if len(channels) > 0 {
log.Debugf("Slack disabled. Would've sent the following message: %s", text)
}
return nil, nil
}

func (c *noopClient) UpdateMessages(slackMessages map[string]string, text, context string) error {
if len(slackMessages) > 0 {
log.Debugf("Slack disabled. Would've updated messages to: %s", text)
}
return nil
}

func (c *noopClient) AddFileToThreads(slackMessages map[string]string, fileName, content string) error {
if len(slackMessages) > 0 {
log.Debugf("Slack disabled. Would've uploaded file named: %s", fileName)
}
return nil
}

0 comments on commit fdd7998

Please sign in to comment.