Skip to content

Commit 9dc92a0

Browse files
committed
fix: create constants for remotes
1 parent 527afef commit 9dc92a0

File tree

12 files changed

+50
-42
lines changed

12 files changed

+50
-42
lines changed

core/configure.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func configureChatApplication(bot *models.Bot) {
4646

4747
switch strings.ToLower(bot.ChatApplication) {
4848
//nolint:goconst // refactor
49-
case "discord":
49+
case models.ChatAppDiscord:
5050
// Discord bot token
5151
token, err := utils.Substitute(bot.DiscordToken, emptyMap)
5252
if err != nil {
@@ -75,11 +75,11 @@ func configureChatApplication(bot *models.Bot) {
7575
}
7676

7777
//nolint:goconst // refactor
78-
case "slack":
78+
case models.ChatAppSlack:
7979
configureSlackBot(bot)
8080

8181
//nolint:goconst // refactor
82-
case "mattermost":
82+
case models.ChatAppMattermost:
8383
log.Info().Msgf("configuring remote '%#q'", bot.ChatApplication)
8484
token, err := utils.Substitute(bot.MatterMostToken, emptyMap)
8585

@@ -109,7 +109,7 @@ func configureChatApplication(bot *models.Bot) {
109109
bot.MatterMostInsecureProtocol = insc
110110

111111
//nolint:goconst // refactor
112-
case "telegram":
112+
case models.ChatAppTelegram:
113113
token, err := utils.Substitute(bot.TelegramToken, emptyMap)
114114
if err != nil {
115115
log.Error().Msgf("could not set 'telegram_token': %s", err.Error())
@@ -126,7 +126,7 @@ func configureChatApplication(bot *models.Bot) {
126126
bot.TelegramToken = token
127127

128128
//nolint:goconst // refactor
129-
case "google_chat":
129+
case models.ChatAppGoogleChat:
130130
gchat.Configure(bot)
131131

132132
default:

core/configure_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,31 @@ func Test_configureChatApplication(t *testing.T) {
2929

3030
testBotSlackNoToken := new(models.Bot)
3131
testBotSlackNoToken.CLI = true
32-
testBotSlackNoToken.ChatApplication = "slack"
32+
testBotSlackNoToken.ChatApplication = models.ChatAppSlack
3333
validateRemoteSetup(testBotSlackNoToken)
3434

3535
testBotBadName := new(models.Bot)
3636
testBotBadName.CLI = true
37-
testBotBadName.ChatApplication = "slack"
37+
testBotBadName.ChatApplication = models.ChatAppSlack
3838
testBotBadName.Name = "${BOT_NAME}"
3939
validateRemoteSetup(testBotBadName)
4040

4141
testBotSlackBadToken := new(models.Bot)
4242
testBotSlackBadToken.CLI = true
43-
testBotSlackBadToken.ChatApplication = "slack"
43+
testBotSlackBadToken.ChatApplication = models.ChatAppSlack
4444
testBotSlackBadToken.SlackToken = "${TOKEN}"
4545
validateRemoteSetup(testBotSlackBadToken)
4646

4747
testBotSlackBadSigningSecret := new(models.Bot)
4848
testBotSlackBadSigningSecret.CLI = true
49-
testBotSlackBadSigningSecret.ChatApplication = "slack"
49+
testBotSlackBadSigningSecret.ChatApplication = models.ChatAppSlack
5050
testBotSlackBadSigningSecret.SlackToken = "${TOKEN}"
5151
testBotSlackBadSigningSecret.SlackSigningSecret = "${TEST_BAD_SIGNING_SECRET}"
5252
validateRemoteSetup(testBotSlackBadSigningSecret)
5353

5454
testBotSlack := new(models.Bot)
5555
testBotSlack.CLI = true
56-
testBotSlack.ChatApplication = "slack"
56+
testBotSlack.ChatApplication = models.ChatAppSlack
5757
testBotSlack.SlackToken = "${TEST_SLACK_TOKEN}"
5858
testBotSlack.SlackAppToken = "${TEST_SLACK_APP_TOKEN}"
5959

@@ -64,18 +64,18 @@ func Test_configureChatApplication(t *testing.T) {
6464

6565
testBotDiscordNoToken := new(models.Bot)
6666
testBotDiscordNoToken.CLI = true
67-
testBotDiscordNoToken.ChatApplication = "discord"
67+
testBotDiscordNoToken.ChatApplication = models.ChatAppDiscord
6868
validateRemoteSetup(testBotDiscordNoToken)
6969

7070
testBotDiscordBadToken := new(models.Bot)
7171
testBotDiscordBadToken.CLI = true
72-
testBotDiscordBadToken.ChatApplication = "discord"
72+
testBotDiscordBadToken.ChatApplication = models.ChatAppDiscord
7373
testBotDiscordBadToken.DiscordToken = "${TOKEN}"
7474
validateRemoteSetup(testBotDiscordBadToken)
7575

7676
testBotDiscordServerID := new(models.Bot)
7777
testBotDiscordServerID.CLI = true
78-
testBotDiscordServerID.ChatApplication = "discord"
78+
testBotDiscordServerID.ChatApplication = models.ChatAppDiscord
7979
testBotDiscordServerID.DiscordToken = "${TEST_DISCORD_TOKEN}"
8080
testBotDiscordServerID.DiscordServerID = "${TEST_DISCORD_SERVER_ID}"
8181

@@ -86,15 +86,15 @@ func Test_configureChatApplication(t *testing.T) {
8686

8787
testBotDiscordBadServerID := new(models.Bot)
8888
testBotDiscordBadServerID.CLI = true
89-
testBotDiscordBadServerID.ChatApplication = "discord"
89+
testBotDiscordBadServerID.ChatApplication = models.ChatAppDiscord
9090
testBotDiscordBadServerID.DiscordToken = "${TEST_DISCORD_TOKEN}"
9191
testBotDiscordBadServerID.DiscordServerID = "${TOKEN}"
9292

9393
validateRemoteSetup(testBotDiscordServerID)
9494

9595
testBotTelegram := new(models.Bot)
9696
testBotTelegram.CLI = true
97-
testBotTelegram.ChatApplication = "telegram"
97+
testBotTelegram.ChatApplication = models.ChatAppTelegram
9898
testBotTelegram.TelegramToken = "${TEST_TELEGRAM_TOKEN}"
9999

100100
t.Setenv("TEST_TELEGRAM_TOKEN", "TESTTOKEN")
@@ -103,12 +103,12 @@ func Test_configureChatApplication(t *testing.T) {
103103

104104
testBotTelegramNoToken := new(models.Bot)
105105
testBotTelegramNoToken.CLI = true
106-
testBotTelegramNoToken.ChatApplication = "telegram"
106+
testBotTelegramNoToken.ChatApplication = models.ChatAppTelegram
107107
validateRemoteSetup(testBotTelegramNoToken)
108108

109109
testBotTelegramBadToken := new(models.Bot)
110110
testBotTelegramBadToken.CLI = true
111-
testBotTelegramBadToken.ChatApplication = "telegram"
111+
testBotTelegramBadToken.ChatApplication = models.ChatAppTelegram
112112
testBotTelegramBadToken.TelegramToken = "${TOKEN}"
113113
validateRemoteSetup(testBotTelegramBadToken)
114114

@@ -124,12 +124,12 @@ func Test_configureChatApplication(t *testing.T) {
124124
{"Slack - no token", args{bot: testBotSlackNoToken}, false},
125125
{"Slack - bad token", args{bot: testBotSlackBadToken}, false},
126126
{"Slack - bad signing secret", args{bot: testBotSlackBadSigningSecret}, false},
127-
{"Slack", args{bot: testBotSlack}, true},
127+
{models.ChatAppSlack, args{bot: testBotSlack}, true},
128128
{"Discord - no token", args{bot: testBotDiscordNoToken}, false},
129129
{"Discord - bad token", args{bot: testBotDiscordBadToken}, false},
130130
{"Discord w/ server id", args{bot: testBotDiscordServerID}, true},
131131
{"Discord w/ bad server id", args{bot: testBotDiscordBadServerID}, false},
132-
{"Telegram", args{bot: testBotTelegram}, true},
132+
{models.ChatAppTelegram, args{bot: testBotTelegram}, true},
133133
{"Telegram - no token", args{bot: testBotTelegramNoToken}, false},
134134
{"Telegram - bad token", args{bot: testBotTelegramBadToken}, false},
135135
}
@@ -151,7 +151,7 @@ func Test_setSlackListenerPort(t *testing.T) {
151151
baseBot := func() *models.Bot {
152152
bot := new(models.Bot)
153153
bot.CLI = true
154-
bot.ChatApplication = "slack"
154+
bot.ChatApplication = models.ChatAppSlack
155155
bot.SlackToken = "${TEST_SLACK_TOKEN}"
156156
bot.SlackInteractionsCallbackPath = "${TEST_SLACK_INTERACTIONS_CALLBACK_PATH}"
157157

@@ -215,15 +215,15 @@ func Test_validateRemoteSetup(t *testing.T) {
215215

216216
testBotCLIChat := new(models.Bot)
217217
testBotCLIChat.CLI = true
218-
testBotCLIChat.ChatApplication = "slack"
218+
testBotCLIChat.ChatApplication = models.ChatAppSlack
219219

220220
testBotCLIChatScheduler := new(models.Bot)
221221
testBotCLIChatScheduler.CLI = true
222-
testBotCLIChatScheduler.ChatApplication = "slack"
222+
testBotCLIChatScheduler.ChatApplication = models.ChatAppSlack
223223
testBotCLIChatScheduler.Scheduler = true
224224

225225
testBotChatScheduler := new(models.Bot)
226-
testBotChatScheduler.ChatApplication = "slack"
226+
testBotChatScheduler.ChatApplication = models.ChatAppSlack
227227
testBotChatScheduler.Scheduler = true
228228

229229
testBotCLIChatSchedulerFail := new(models.Bot)

core/outputs.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Outputs(outputMsgs <-chan models.Message, hitRule <-chan models.Rule, bot *
2929
chatApp := strings.ToLower(bot.ChatApplication)
3030

3131
switch chatApp {
32-
case "discord":
32+
case models.ChatAppDiscord:
3333
if service == models.MsgServiceScheduler {
3434
log.Warn().Msg("scheduler does not currently support discord")
3535
break
@@ -38,7 +38,7 @@ func Outputs(outputMsgs <-chan models.Message, hitRule <-chan models.Rule, bot *
3838
remoteDiscord := &discord.Client{Token: bot.DiscordToken}
3939
remoteDiscord.Reaction(message, rule, bot)
4040
remoteDiscord.Send(message, bot)
41-
case "mattermost":
41+
case models.ChatAppMattermost:
4242
remoteMM := &mattermost.Client{
4343
Server: bot.MatterMostServer,
4444
Token: bot.MatterMostToken,
@@ -48,7 +48,7 @@ func Outputs(outputMsgs <-chan models.Message, hitRule <-chan models.Rule, bot *
4848
}
4949

5050
remoteMM.Send(message, bot)
51-
case "slack":
51+
case models.ChatAppSlack:
5252
// Create Slack client
5353
remoteSlack := &slack.Client{
5454
ListenerPort: bot.SlackListenerPort,
@@ -62,12 +62,12 @@ func Outputs(outputMsgs <-chan models.Message, hitRule <-chan models.Rule, bot *
6262
}
6363

6464
remoteSlack.Send(message, bot)
65-
case "telegram":
65+
case models.ChatAppTelegram:
6666
remoteTelegram := &telegram.Client{
6767
Token: bot.TelegramToken,
6868
}
6969
remoteTelegram.Send(message, bot)
70-
case "google_chat":
70+
case models.ChatAppGoogleChat:
7171
gchat.HandleRemoteOutput(message, bot)
7272
default:
7373
log.Error().Msgf("chat application %#q is not supported", chatApp)

core/remotes.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ func Remotes(inputMsgs chan<- models.Message, rules map[string]models.Rule, bot
4646

4747
switch chatApp {
4848
// Setup remote to use the Discord client to read from Discord
49-
case "discord":
49+
case models.ChatAppDiscord:
5050
// Create Discord client
5151
remoteDiscord := &discord.Client{
5252
Token: bot.DiscordToken,
5353
}
5454
// Read messages from Discord
5555
go remoteDiscord.Read(inputMsgs, rules, bot)
5656
// Setup remote to use the Slack client to read from Slack
57-
case "slack":
57+
case models.ChatAppSlack:
5858
// Create Slack client
5959
remoteSlack := &slack.Client{
6060
Token: bot.SlackToken,
@@ -63,7 +63,7 @@ func Remotes(inputMsgs chan<- models.Message, rules map[string]models.Rule, bot
6363
}
6464
// Read messages from Slack
6565
go remoteSlack.Read(inputMsgs, rules, bot)
66-
case "mattermost":
66+
case models.ChatAppMattermost:
6767
remoteMattermost := &mattermost.Client{
6868
Token: bot.MatterMostToken,
6969
Server: bot.MatterMostServer,
@@ -80,13 +80,13 @@ func Remotes(inputMsgs chan<- models.Message, rules map[string]models.Rule, bot
8080

8181
go remoteMattermost.Read(inputMsgs, rules, bot)
8282
// Setup remote to use the Telegram client to read from Telegram
83-
case "telegram":
83+
case models.ChatAppTelegram:
8484
remoteTelegram := &telegram.Client{
8585
Token: bot.TelegramToken,
8686
}
8787
// Read messages from Telegram
8888
go remoteTelegram.Read(inputMsgs, rules, bot)
89-
case "google_chat":
89+
case models.ChatAppGoogleChat:
9090
gchat.HandleRemoteInput(inputMsgs, rules, bot)
9191
default:
9292
log.Error().Msgf("chat application %#q is not supported", chatApp)

models/remotes.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ package models
44

55
import "github.com/slack-go/slack"
66

7+
const (
8+
ChatAppSlack = "slack"
9+
ChatAppDiscord = "discord"
10+
ChatAppMattermost = "mattermost"
11+
ChatAppTelegram = "telegram"
12+
ChatAppGoogleChat = "google_chat"
13+
)
14+
715
// Remotes is a struct that holds data for various remotes.
816
type Remotes struct {
917
Slack SlackConfig `mapstructure:"slack" binding:"omitempty"`

remote/discord/remote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (c *Client) new() *discordgo.Session {
3939

4040
// Name returns the name of the remote.
4141
func (c *Client) Name() string {
42-
return "discord"
42+
return models.ChatAppDiscord
4343
}
4444

4545
// Reaction implementation to satisfy remote interface

remote/gchat/remote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (c *Client) new() *pubsub.Client {
4545

4646
// Name returns the name of the remote.
4747
func (c *Client) Name() string {
48-
return "google_chat"
48+
return models.ChatAppGoogleChat
4949
}
5050

5151
// Read messages from Google Chat.

remote/mattermost/remote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (c *Client) new() *model.Client4 {
4141
return api
4242
}
4343

44-
func (c *Client) Name() string { return "mattermost" }
44+
func (c *Client) Name() string { return models.ChatAppMattermost }
4545

4646
func (c *Client) Reaction(_ models.Message, rule models.Rule, _ *models.Bot) {
4747
if rule.RemoveReaction != "" {

remote/slack/remote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type slackLogger struct {
4242

4343
// Name returns the name of the remote.
4444
func (c *Client) Name() string {
45-
return "slack"
45+
return models.ChatAppSlack
4646
}
4747

4848
func (l *slackLogger) Output(_ int, s string) error {

remote/telegram/remote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (c *Client) new() *tgbotapi.BotAPI {
4040

4141
// Name returns the name of the remote.
4242
func (c *Client) Name() string {
43-
return "telegram"
43+
return models.ChatAppTelegram
4444
}
4545

4646
// Reaction implementation to satisfy remote interface.

0 commit comments

Comments
 (0)