From a06aa338ed1b5ef18d54b6d384be1d183085433e Mon Sep 17 00:00:00 2001 From: ryok-0319 Date: Tue, 24 Sep 2019 14:31:14 +0900 Subject: [PATCH] Enable slack env vars for channel id and bot name --- notifier/slack/client.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/notifier/slack/client.go b/notifier/slack/client.go index 26c95f1..5b8104a 100644 --- a/notifier/slack/client.go +++ b/notifier/slack/client.go @@ -12,6 +12,12 @@ import ( // EnvToken is Slack API Token const EnvToken = "SLACK_TOKEN" +// EnvChannelID is Slack channel ID +const EnvChannelID = "SLACK_CHANNEL_ID" + +// EnvBotName is Slack bot name +const EnvBotName = "SLACK_BOT_NAME" + // Client is a API client for Slack type Client struct { *slack.Client @@ -51,6 +57,19 @@ func NewClient(cfg Config) (*Client, error) { if token == "" { return &Client{}, errors.New("slack token is missing") } + + channel := cfg.Channel + channel = strings.TrimPrefix(channel, "$") + if channel == EnvChannelID { + channel = os.Getenv(EnvChannelID) + } + + botname := cfg.Botname + botname = strings.TrimPrefix(botname, "$") + if botname == EnvBotName { + botname = os.Getenv(EnvBotName) + } + client := slack.New(token) c := &Client{ Config: cfg, @@ -60,8 +79,8 @@ func NewClient(cfg Config) (*Client, error) { c.Notify = (*NotifyService)(&c.common) c.API = &Slack{ Client: client, - Channel: cfg.Channel, - Botname: cfg.Botname, + Channel: channel, + Botname: botname, } return c, nil }