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"
|
|
|
|
|
|
|
|
"gitlab.com/demodesk/neko/server/internal/config"
|
|
|
|
"gitlab.com/demodesk/neko/server/pkg/types"
|
|
|
|
)
|
|
|
|
|
2022-04-19 22:14:59 +12:00
|
|
|
type ManagerCtx struct {
|
2022-04-16 07:28:00 +12:00
|
|
|
logger zerolog.Logger
|
|
|
|
plugins map[string]types.Plugin
|
|
|
|
}
|
|
|
|
|
2022-04-19 22:14:59 +12:00
|
|
|
func New(config *config.Plugins) *ManagerCtx {
|
|
|
|
manager := &ManagerCtx{
|
2022-04-16 07:28:00 +12:00
|
|
|
logger: log.With().Str("module", "plugins").Logger(),
|
|
|
|
plugins: map[string]types.Plugin{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Enabled {
|
|
|
|
err := manager.loadDir(config.Dir)
|
|
|
|
manager.logger.Err(err).Msgf("loading finished, total %d plugins", len(manager.plugins))
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-19 22:14:59 +12:00
|
|
|
_, ok = manager.plugins[p.Name()]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("plugin '%s' already exists", p.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.plugins[p.Name()] = p
|
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-04-16 07:28:00 +12:00
|
|
|
for path, plug := range manager.plugins {
|
|
|
|
if err := plug.Config().Init(cmd); err != nil {
|
|
|
|
log.Err(err).Str("plugin", path).Msg("unable to initialize configuration")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 22:14:59 +12:00
|
|
|
func (manager *ManagerCtx) SetConfigs() {
|
2022-04-16 07:28:00 +12:00
|
|
|
for _, plug := range manager.plugins {
|
|
|
|
plug.Config().Set()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-20 21:31:37 +12:00
|
|
|
for path, plug := range manager.plugins {
|
|
|
|
err := plug.Start(types.PluginManagers{
|
2022-04-19 22:14:59 +12:00
|
|
|
SessionManager: sessionManager,
|
|
|
|
WebSocketManager: webSocketManager,
|
|
|
|
ApiManager: apiManager,
|
|
|
|
LoadServiceFromPlugin: manager.LookupService,
|
2022-04-16 07:28:00 +12:00
|
|
|
})
|
2022-04-20 21:31:37 +12:00
|
|
|
|
|
|
|
manager.logger.Err(err).Str("plugin", path).Msg("plugin start")
|
2022-04-16 07:28:00 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 22:14:59 +12:00
|
|
|
func (manager *ManagerCtx) Shutdown() error {
|
2022-04-16 07:28:00 +12:00
|
|
|
for path, plug := range manager.plugins {
|
|
|
|
err := plug.Shutdown()
|
|
|
|
manager.logger.Err(err).Str("plugin", path).Msg("plugin shutdown")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-19 22:14:59 +12:00
|
|
|
|
|
|
|
func (manager *ManagerCtx) LookupService(pluginName string) (any, error) {
|
|
|
|
plug, ok := manager.plugins[pluginName]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("plugin '%s' not found", pluginName)
|
|
|
|
}
|
|
|
|
|
|
|
|
expPlug, ok := plug.(types.ExposablePlugin)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("plugin '%s' is not exposable", pluginName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return expPlug.ExposeService(), nil
|
|
|
|
}
|