-
Notifications
You must be signed in to change notification settings - Fork 1
/
executor.go
42 lines (34 loc) · 1.03 KB
/
executor.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
package plugin
import (
"context"
"fmt"
"sync"
"github.com/rnetx/cdns/adapter"
"github.com/rnetx/cdns/log"
)
type PluginExecutorOptions struct {
Tag string `yaml:"tag"`
Type string `yaml:"type"`
Args any `yaml:"args,omitempty"`
}
var executorMap sync.Map
type PluginExecutorFactory func(ctx context.Context, core adapter.Core, logger log.Logger, tag string, args any) (adapter.PluginExecutor, error)
func RegisterPluginExecutor(_type string, factory PluginExecutorFactory) {
executorMap.Store(_type, factory)
}
func NewPluginExecutor(ctx context.Context, core adapter.Core, logger log.Logger, tag string, _type string, args any) (adapter.PluginExecutor, error) {
v, ok := executorMap.Load(_type)
if !ok {
return nil, fmt.Errorf("unknown plugin executor type: %s", _type)
}
f := v.(PluginExecutorFactory)
return f(ctx, core, logger, tag, args)
}
func PluginExecutorTypes() []string {
var types []string
executorMap.Range(func(key any, value any) bool {
types = append(types, key.(string))
return true
})
return types
}