-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginloader.go
More file actions
46 lines (43 loc) · 1.17 KB
/
Copy pathpluginloader.go
File metadata and controls
46 lines (43 loc) · 1.17 KB
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
package redeye
import (
"fmt"
"log/slog"
"os"
"path/filepath"
"plugin"
)
// LoadPlugin opens a compiled .so plugin file. The plugin's init() is expected
// to call redeye.Filters.Add to register any filters it provides.
// Requires a CGO-enabled, dynamically linked host binary (not compatible with
// -buildmode=static or the cross-compiled RPi static build).
func LoadPlugin(path string) error {
if _, err := plugin.Open(path); err != nil {
return fmt.Errorf("open plugin %s: %w", path, err)
}
slog.Info("plugin loaded", "path", path)
return nil
}
// LoadPlugins walks dir and opens every .so file it finds.
// A missing directory is silently ignored (returns 0, nil).
// Returns the count of successfully loaded plugins.
func LoadPlugins(dir string) (int, error) {
entries, err := os.ReadDir(dir)
if err != nil {
if os.IsNotExist(err) {
return 0, nil
}
return 0, fmt.Errorf("read plugin dir %s: %w", dir, err)
}
var n int
for _, e := range entries {
if e.IsDir() || filepath.Ext(e.Name()) != ".so" {
continue
}
if err := LoadPlugin(filepath.Join(dir, e.Name())); err != nil {
slog.Warn("plugin skipped", "err", err)
continue
}
n++
}
return n, nil
}