-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract app code to inject plugins at compile time
- Loading branch information
Showing
5 changed files
with
142 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package app | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/innogames/slack-bot/v2/bot" | ||
"github.com/innogames/slack-bot/v2/bot/config" | ||
"github.com/innogames/slack-bot/v2/bot/storage" | ||
"github.com/innogames/slack-bot/v2/bot/util" | ||
"github.com/innogames/slack-bot/v2/client" | ||
"github.com/innogames/slack-bot/v2/command" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Run main entry point for the bot application. Listens on incoming slack messages and handles them | ||
func Run() { | ||
var configFile string | ||
var verbose bool | ||
var showConfig bool | ||
flag.StringVar(&configFile, "config", "config.yaml", "Path to config.yaml. Can be a directory which will load all '*.yaml' inside") | ||
flag.BoolVar(&verbose, "verbose", false, "More verbose output") | ||
flag.BoolVar(&showConfig, "show-config", false, "Print the config as YAML and exit") | ||
flag.Parse() | ||
|
||
cfg, err := config.Load(configFile) | ||
checkError(err) | ||
|
||
if verbose { | ||
cfg.Slack.Debug = true | ||
cfg.Logger.Level = "debug" | ||
} | ||
|
||
if showConfig { | ||
fmt.Println(config.Dump(cfg)) | ||
os.Exit(0) | ||
} | ||
|
||
bot.InitLogger(cfg.Logger) | ||
log.Infof("Loaded config from %s", configFile) | ||
|
||
err = storage.InitStorage(cfg.StoragePath) | ||
checkError(err) | ||
|
||
slackClient, err := client.GetSlackClient(cfg.Slack) | ||
checkError(err) | ||
|
||
// get the list of all default commands | ||
commands := command.GetCommands(slackClient, cfg) | ||
|
||
b := bot.NewBot(cfg, slackClient, commands) | ||
err = b.Init() | ||
checkError(err) | ||
|
||
// start main loop! | ||
ctx := util.NewServerContext() | ||
b.Run(ctx) | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package bot | ||
|
||
import "github.com/innogames/slack-bot/v2/client" | ||
|
||
// Plugin is a extended command which can be registered to the bot at compile time | ||
type Plugin struct { | ||
Init func(bot *Bot, slackClient client.SlackClient) Commands | ||
} | ||
|
||
var pluginList []Plugin | ||
|
||
// RegisterPlugin registers a new plugin, also in init() time | ||
func RegisterPlugin(plugin Plugin) { | ||
pluginList = append(pluginList, plugin) | ||
} | ||
|
||
func loadPlugins(bot *Bot, slackClient client.SlackClient) Commands { | ||
commands := Commands{} | ||
|
||
for _, plugin := range pluginList { | ||
commands.Merge(plugin.Init(bot, slackClient)) | ||
} | ||
|
||
clear(pluginList) | ||
|
||
return commands | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/innogames/slack-bot/v2/bot" | ||
"github.com/innogames/slack-bot/v2/bot/config" | ||
"github.com/innogames/slack-bot/v2/bot/storage" | ||
"github.com/innogames/slack-bot/v2/bot/util" | ||
"github.com/innogames/slack-bot/v2/client" | ||
"github.com/innogames/slack-bot/v2/command" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
import "github.com/innogames/slack-bot/v2/bot/app" | ||
import _ "github.com/innogames/slack-bot/v2/plugins/test" | ||
|
||
// main entry point for the bot application. Listens on incoming slack messages and handles them | ||
func main() { | ||
var configFile string | ||
var verbose bool | ||
var showConfig bool | ||
flag.StringVar(&configFile, "config", "config.yaml", "Path to config.yaml. Can be a directory which will load all '*.yaml' inside") | ||
flag.BoolVar(&verbose, "verbose", false, "More verbose output") | ||
flag.BoolVar(&showConfig, "show-config", false, "Print the config as YAML and exit") | ||
flag.Parse() | ||
|
||
cfg, err := config.Load(configFile) | ||
checkError(err) | ||
|
||
if verbose { | ||
cfg.Slack.Debug = true | ||
cfg.Logger.Level = "debug" | ||
} | ||
|
||
if showConfig { | ||
fmt.Println(config.Dump(cfg)) | ||
os.Exit(0) | ||
} | ||
|
||
bot.InitLogger(cfg.Logger) | ||
log.Infof("Loaded config from %s", configFile) | ||
|
||
err = storage.InitStorage(cfg.StoragePath) | ||
checkError(err) | ||
|
||
slackClient, err := client.GetSlackClient(cfg.Slack) | ||
checkError(err) | ||
|
||
// get the list of all default commands | ||
commands := command.GetCommands(slackClient, cfg) | ||
|
||
b := bot.NewBot(cfg, slackClient, commands) | ||
err = b.Init() | ||
checkError(err) | ||
|
||
// start main loop! | ||
ctx := util.NewServerContext() | ||
b.Run(ctx) | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
app.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package test | ||
|
||
import ( | ||
"github.com/innogames/slack-bot/v2/bot" | ||
"github.com/innogames/slack-bot/v2/bot/matcher" | ||
"github.com/innogames/slack-bot/v2/bot/msg" | ||
"github.com/innogames/slack-bot/v2/bot/util" | ||
"github.com/innogames/slack-bot/v2/client" | ||
) | ||
|
||
type testCommand struct { | ||
bot.BaseCommand | ||
} | ||
|
||
func (c *testCommand) GetMatcher() matcher.Matcher { | ||
return matcher.NewGroupMatcher( | ||
matcher.NewPrefixMatcher("foo", c.reply), | ||
) | ||
} | ||
|
||
func (c *testCommand) reply(match matcher.Result, message msg.Message) { | ||
text := match.GetString(util.FullMatch) | ||
if text == "" { | ||
return | ||
} | ||
|
||
c.SendMessage(message, text) | ||
} | ||
|
||
func start(b *bot.Bot, slackClient client.SlackClient) bot.Commands { | ||
commands := bot.Commands{} | ||
|
||
commands.AddCommand(&testCommand{bot.BaseCommand{SlackClient: slackClient}}) | ||
|
||
return commands | ||
} | ||
|
||
func init() { | ||
bot.RegisterPlugin(bot.Plugin{ | ||
Init: start, | ||
}) | ||
} |