Skip to content

Commit

Permalink
extract app code to inject plugins at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
brainexe committed Jun 11, 2024
1 parent 3481901 commit 284b823
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 60 deletions.
66 changes: 66 additions & 0 deletions bot/app/start.go
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)
}
}
4 changes: 4 additions & 0 deletions bot/bot_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func (b *Bot) Init() (err error) {
log.Infof("Loaded %d allowed users and %d channels", len(b.allowedUsers), len(client.AllChannels))
log.Infof("Bot user: @%s with ID %s on workspace %s", b.auth.User, b.auth.UserID, b.auth.URL)

pluginCommands := loadPlugins(b, b.slackClient)
log.Infof("Loaded %d plugin commands", len(pluginCommands.commands))
b.commands.Merge(pluginCommands)

commands := b.commands.GetCommandNames()
log.Infof("Initialized %d commands:", len(commands))
log.Info(strings.Join(commands, ", "))
Expand Down
27 changes: 27 additions & 0 deletions bot/plugin.go
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
}
63 changes: 3 additions & 60 deletions cmd/bot/main.go
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()
}
42 changes: 42 additions & 0 deletions plugins/test/main.go
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,
})
}

0 comments on commit 284b823

Please sign in to comment.