-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
55 lines (45 loc) · 1.14 KB
/
plugin.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
// Copyright (C) 2024 Matt Dunleavy. All rights reserved.
// Use of this source code is subject to the MIT license
// that can be found in the LICENSE file.
package pluginmanager
import (
"plugin"
"time"
)
type PluginMetadata struct {
Name string
Version string
Dependencies map[string]string
GoVersion string
Signature []byte
}
type Plugin interface {
Metadata() PluginMetadata
PreLoad() error
Init() error
PostLoad() error
Execute() error
PreUnload() error
Shutdown() error
}
type PluginStats struct {
ExecutionCount int64
LastExecutionTime time.Duration
TotalExecutionTime time.Duration
}
const PluginSymbol = "Plugin"
func LoadPlugin(path string) (Plugin, error) {
p, err := plugin.Open(path)
if err != nil {
return nil, &PluginError{Op: "open", Err: err}
}
symPlugin, err := p.Lookup(PluginSymbol)
if err != nil {
return nil, &PluginError{Op: "lookup", Err: err}
}
plugin, ok := symPlugin.(Plugin)
if !ok {
return nil, &PluginError{Op: "assert", Err: ErrInvalidPluginInterface}
}
return plugin, nil
}