butterrobot/internal/plugin/plugin.go
Felipe M. 7c684af8c3
All checks were successful
ci/woodpecker/tag/release Pipeline was successful
refactor: python -> go
2025-04-20 14:13:44 +02:00

82 lines
1.7 KiB
Go

package plugin
import (
"sync"
"git.nakama.town/fmartingr/butterrobot/internal/model"
)
var (
// plugins holds all registered plugins
plugins = make(map[string]model.Plugin)
// pluginsMu protects the plugins map
pluginsMu sync.RWMutex
)
// Register registers a plugin with the given ID
func Register(plugin model.Plugin) {
pluginsMu.Lock()
defer pluginsMu.Unlock()
plugins[plugin.GetID()] = plugin
}
// Get returns a plugin by ID
func Get(id string) (model.Plugin, error) {
pluginsMu.RLock()
defer pluginsMu.RUnlock()
plugin, exists := plugins[id]
if !exists {
return nil, model.ErrPluginNotFound
}
return plugin, nil
}
// GetAvailablePlugins returns all registered plugins
func GetAvailablePlugins() map[string]model.Plugin {
pluginsMu.RLock()
defer pluginsMu.RUnlock()
// Create a copy to avoid race conditions
result := make(map[string]model.Plugin, len(plugins))
for id, plugin := range plugins {
result[id] = plugin
}
return result
}
// BasePlugin provides a common base for plugins
type BasePlugin struct {
ID string
Name string
Help string
ConfigRequired bool
}
// GetID returns the plugin ID
func (p *BasePlugin) GetID() string {
return p.ID
}
// GetName returns the plugin name
func (p *BasePlugin) GetName() string {
return p.Name
}
// GetHelp returns the plugin help text
func (p *BasePlugin) GetHelp() string {
return p.Help
}
// RequiresConfig indicates if the plugin requires configuration
func (p *BasePlugin) RequiresConfig() bool {
return p.ConfigRequired
}
// OnMessage is the default implementation that does nothing
func (p *BasePlugin) OnMessage(msg *model.Message, config map[string]interface{}) []*model.Message {
return nil
}