Skip to content

Commit

Permalink
Support extra variables (#104)
Browse files Browse the repository at this point in the history
* substitute env variables in the bot name and output rooms
* added logging
  • Loading branch information
sarge authored and wass3r committed Jun 12, 2019
1 parent 976b414 commit 6221e72
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ build:
go build -v -a -o flottbot cmd/flottbot/main.go

run: build
./flottbot
./flottbot
8 changes: 8 additions & 0 deletions core/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func initLogger(b *models.Bot) {
// configureChatApplication configures a user's specified chat application
// TODO: Refactor to keep remote specifics in remote/
func configureChatApplication(bot *models.Bot) {

// update the bot name
token, err := utils.Substitute(bot.Name, map[string]string{})
if err != nil {
bot.Log.Warnf("Could not configure bot Name: %s", err.Error())
}
bot.Name = token

if bot.ChatApplication != "" {
switch strings.ToLower(bot.ChatApplication) {
case "discord":
Expand Down
16 changes: 13 additions & 3 deletions core/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,31 @@ func Test_validateRemoteSetup(t *testing.T) {
}

func TestConfigure(t *testing.T) {

testBot := new(models.Bot)
testBot.Name = "mybot(${FB_ENV})"
testBot.CLI = true

os.Setenv("FB_ENV", "dev")
defer os.Unsetenv("FB_ENV")

type args struct {
bot *models.Bot
}
tests := []struct {
name string
args args
name string
args args
expect args
}{
{"Basic", args{bot: testBot}},
{"Basic", args{bot: testBot}, args{bot: &models.Bot{Name: "mybot(dev)"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Configure(tt.args.bot)

if tt.args.bot.Name != tt.expect.bot.Name {
t.Errorf("configure() wanted bot.Name set to %v, but got %v", tt.args.bot.Name, tt.expect.bot.Name)
}
})
}
}
19 changes: 19 additions & 0 deletions core/rules.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"fmt"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -59,8 +60,26 @@ func Rules(rules *map[string]models.Rule, bot *models.Bot) {
if err != nil {
log.Fatalf(err.Error())
}
err = validateRule(bot, &rule)
if err != nil {
log.Fatalf(err.Error())
}
(*rules)[ruleFile] = rule
}

bot.Log.Infof("Configured '%s' rules!", bot.Name)
}

// Validate applies any environmental changes
func validateRule(bot *models.Bot, r *models.Rule) error {

for i := range r.OutputToRooms {
token, err := utils.Substitute(r.OutputToRooms[i], map[string]string{})
if err != nil {
return fmt.Errorf("Could not configure output room: %s", err.Error())
}

r.OutputToRooms[i] = token
}
return nil
}

0 comments on commit 6221e72

Please sign in to comment.