neko/internal/plugins/manager.go

131 lines
2.9 KiB
Go
Raw Normal View History

2022-04-16 07:28:00 +12:00
package plugins
import (
"fmt"
"os"
"path/filepath"
"plugin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/demodesk/neko/internal/config"
"github.com/demodesk/neko/pkg/types"
2022-04-16 07:28:00 +12:00
)
2022-04-19 22:14:59 +12:00
type ManagerCtx struct {
2022-04-16 07:28:00 +12:00
logger zerolog.Logger
2022-05-03 23:17:04 +12:00
plugins dependiencies
2022-04-16 07:28:00 +12:00
}
2022-04-19 22:14:59 +12:00
func New(config *config.Plugins) *ManagerCtx {
manager := &ManagerCtx{
2022-05-03 23:17:04 +12:00
logger: log.With().Str("module", "plugins").Logger(),
plugins: dependiencies{
deps: make(map[string]*dependency),
},
2022-04-16 07:28:00 +12:00
}
2022-05-03 23:17:04 +12:00
manager.plugins.logger = manager.logger
2022-04-16 07:28:00 +12:00
if config.Enabled {
err := manager.loadDir(config.Dir)
2022-05-03 23:17:04 +12:00
manager.logger.Err(err).Msgf("loading finished, total %d plugins", manager.plugins.len())
2022-04-16 07:28:00 +12:00
}
return manager
}
2022-04-19 22:14:59 +12:00
func (manager *ManagerCtx) loadDir(dir string) error {
2022-04-16 07:28:00 +12:00
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
err = manager.load(path)
manager.logger.Err(err).Str("plugin", path).Msg("loading a plugin")
return nil
})
}
2022-04-19 22:14:59 +12:00
func (manager *ManagerCtx) load(path string) error {
2022-04-16 07:28:00 +12:00
pl, err := plugin.Open(path)
if err != nil {
return err
}
sym, err := pl.Lookup("Plugin")
if err != nil {
return err
}
p, ok := sym.(types.Plugin)
if !ok {
return fmt.Errorf("not a valid plugin")
}
2022-05-03 23:17:04 +12:00
if err = manager.plugins.addPlugin(p); err != nil {
return fmt.Errorf("failed to add plugin '%s': %w", p.Name(), err)
2022-04-19 22:14:59 +12:00
}
2022-05-03 23:17:04 +12:00
manager.logger.Info().Msgf("loaded plugin '%s', total %d plugins", p.Name(), manager.plugins.len())
2022-04-16 07:28:00 +12:00
return nil
}
2022-04-19 22:14:59 +12:00
func (manager *ManagerCtx) InitConfigs(cmd *cobra.Command) {
2022-05-03 23:17:04 +12:00
_ = manager.plugins.forEach(func(plug *dependency) error {
if err := plug.plugin.Config().Init(cmd); err != nil {
log.Err(err).Str("plugin", plug.plugin.Name()).Msg("unable to initialize configuration")
2022-04-16 07:28:00 +12:00
}
2022-05-03 23:17:04 +12:00
return nil
})
2022-04-16 07:28:00 +12:00
}
2022-04-19 22:14:59 +12:00
func (manager *ManagerCtx) SetConfigs() {
2022-05-03 23:17:04 +12:00
_ = manager.plugins.forEach(func(plug *dependency) error {
plug.plugin.Config().Set()
return nil
})
2022-04-16 07:28:00 +12:00
}
2022-04-19 22:14:59 +12:00
func (manager *ManagerCtx) Start(
2022-04-16 07:28:00 +12:00
sessionManager types.SessionManager,
webSocketManager types.WebSocketManager,
apiManager types.ApiManager,
) {
2022-05-03 23:17:04 +12:00
_ = manager.plugins.start(types.PluginManagers{
SessionManager: sessionManager,
WebSocketManager: webSocketManager,
ApiManager: apiManager,
LoadServiceFromPlugin: manager.LookupService,
})
2022-04-16 07:28:00 +12:00
}
2022-04-19 22:14:59 +12:00
func (manager *ManagerCtx) Shutdown() error {
2022-05-03 23:17:04 +12:00
_ = manager.plugins.forEach(func(plug *dependency) error {
err := plug.plugin.Shutdown()
manager.logger.Err(err).Str("plugin", plug.plugin.Name()).Msg("plugin shutdown")
return nil
})
2022-04-16 07:28:00 +12:00
return nil
}
2022-04-19 22:14:59 +12:00
func (manager *ManagerCtx) LookupService(pluginName string) (any, error) {
2022-05-03 23:17:04 +12:00
plug, ok := manager.plugins.findPlugin(pluginName)
2022-04-19 22:14:59 +12:00
if !ok {
return nil, fmt.Errorf("plugin '%s' not found", pluginName)
}
2022-05-03 23:17:04 +12:00
expPlug, ok := plug.plugin.(types.ExposablePlugin)
2022-04-19 22:14:59 +12:00
if !ok {
return nil, fmt.Errorf("plugin '%s' is not exposable", pluginName)
}
return expPlug.ExposeService(), nil
}