-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
67 lines (52 loc) · 1.36 KB
/
plugins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//go:build cgo && (linux || darwin || freebsd)
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"plugin"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
func loadPlugins(pluginDir string) error {
logger := log.WithField("plugin_dir", pluginDir)
d, err := os.Stat(pluginDir)
if err != nil {
if os.IsNotExist(err) {
logger.Debug("Plugin directory not found, skipping")
return nil
}
return errors.Wrap(err, "getting plugin-dir info")
}
if !d.IsDir() {
return errors.New("plugin-dir is not a directory")
}
args := getRegistrationArguments()
return errors.Wrap(filepath.Walk(pluginDir, func(currentPath string, _ os.FileInfo, err error) error {
if err != nil {
return err
}
if !strings.HasSuffix(currentPath, ".so") {
// Ignore that file, is not a plugin
return nil
}
logger := log.WithField("plugin", path.Base(currentPath))
p, err := plugin.Open(currentPath)
if err != nil {
logger.WithError(err).Error("Unable to open plugin")
return nil
}
f, err := p.Lookup("Register")
if err != nil {
logger.WithError(err).Error("Unable to find register function")
return nil
}
if err = f.(func(plugins.RegistrationArguments) error)(args); err != nil {
return fmt.Errorf("registering plugin: %w", err)
}
return nil
}), "loading plugins")
}