-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.go
303 lines (258 loc) · 6.7 KB
/
loader.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package xplugeth
import (
"flag"
"path"
"path/filepath"
"strings"
"io/ioutil"
"reflect"
"github.com/go-yaml/yaml"
"github.com/ethereum/go-ethereum/log"
)
var configPath string
type pluginLoader struct {
initialized bool
modules []reflect.Type
hookInterfaces []reflect.Type
hooks map[reflect.Type][]any
moduleValues []reflect.Value
names map[string]reflect.Type
patchsets map[reflect.Type][]Patchset
singletons map[reflect.Type]any
subCommands map[string]func([]string)error
flags []flag.FlagSet
providedSubCommand string
providedSCArgs []string
}
func (pl *pluginLoader) registerHook(t reflect.Type, p ...Patchset) {
pl.hookInterfaces = append(pl.hookInterfaces, t)
if len(p) > 0 {
pl.patchsets[t] = p
}
}
func (pl *pluginLoader) registerModule(t reflect.Type, name string) {
pl.modules = append(pl.modules, t)
if pl.names == nil {
n := make(map[string]reflect.Type)
pl.names = n
}
pl.names[name] = t
}
func (pl *pluginLoader) registerSubCommands(provided map[string]func([]string)error) {
for name, f := range provided {
pl.subCommands[name] = f
}
}
func (pl *pluginLoader) registerFlags(provided flag.FlagSet) {
pl.flags = append(pl.flags, provided)
}
func (pl *pluginLoader) initialize(dirpath string) {
pl.initialized = true
pl.hooks = make(map[reflect.Type][]any)
for _, mt := range pl.modules {
mv := reflect.New(mt)
pl.moduleValues = append(pl.moduleValues, mv)
for _, ht := range pl.hookInterfaces {
if reflect.PointerTo(mt).Implements(ht) {
pl.hooks[ht] = append(pl.hooks[ht], mv.Interface())
}
}
}
configPath = dirpath
}
func (pl *pluginLoader) getModules(t reflect.Type) []any {
return pl.hooks[t]
}
func (pl *pluginLoader) getModulesByMethodName(name string) []any {
results := make([]any, len(pl.moduleValues))
for i, mv := range pl.moduleValues {
if v := mv.MethodByName(name); v.IsValid() && !v.IsZero() {
results[i] = mv.Interface()
}
}
return results
}
func (pl *pluginLoader) storeSingleton(t reflect.Type, v any) error {
if _, ok := pl.singletons[t]; ok {
return ErrSingletonAlreadySet
}
pl.singletons[t] = v
return nil
}
func (pl *pluginLoader) getSingleton(t reflect.Type) (any, bool) {
v, ok := pl.singletons[t]
return v, ok
}
func (pl *pluginLoader) hasModule(name string) bool {
_, ok := pl.names[name]
return ok
}
func (pl *pluginLoader) parseCommands(commands []string) (int,bool) {
var i int
var ok bool
if pl.initialized {
if i, ok = pl.hasSubcommand(commands); ok {
pl.hasFlag(commands)
return i, ok
}
if i, ok = pl.hasFlag(commands); ok {
return i, ok
}
}
return 0, false
}
func (pl *pluginLoader) hasSubcommand(commands []string) (int, bool) {
if commands == nil || len(commands) == 0 {
return 0, false
}
for i, name := range commands {
if _, ok := pl.subCommands[name]; ok {
pl.providedSubCommand = name
pl.providedSCArgs = commands[i:]
return i, true
}
}
return 0, false
}
func (pl *pluginLoader) hasFlag(args []string) (int, bool) {
if args == nil || len(args) == 0 {
return 0, false
}
masterFlagSet := *flag.NewFlagSet("master-plugin-flagset", flag.ContinueOnError)
for _, flagset := range pl.flags {
flagset.VisitAll(func(f *flag.Flag) {
masterFlagSet.Var(f.Value, f.Name, f.Usage)
})
}
flagArgs := make([]string, len(args))
prefix := "--"
for i, arg := range args {
if strings.HasPrefix(arg, prefix) {
flagArgs[i] = arg
}
}
var idx int
var present bool
for i, arg := range flagArgs {
argName := strings.TrimPrefix(arg, "--")
if eqIdx := strings.Index(argName, "="); eqIdx != -1 {
argName = argName[:eqIdx]
}
if p := masterFlagSet.Lookup(argName); p != nil {
idx = i
present = true
if err := masterFlagSet.Parse(args[i:]); err != nil {
log.Error("error parsing flags, xplugeth flags should be positioned after all geth flags", "err", err)
return 0, false
}
return idx, present
}
}
return idx, present
}
func (pl *pluginLoader) runSubcommand() (bool, error) {
if pl.providedSubCommand == "" {
return false, nil
} else {
return true, pl.subCommands[pl.providedSubCommand](pl.providedSCArgs)
}
}
var pl *pluginLoader
func init() {
pl = &pluginLoader{
modules: []reflect.Type{},
hookInterfaces: []reflect.Type{},
hooks: make(map[reflect.Type][]any),
singletons: make(map[reflect.Type]any),
patchsets: make(map[reflect.Type][]Patchset),
subCommands: make(map[string]func([]string)error),
flags: make([]flag.FlagSet, 0),
}
}
func RegisterModule[t any](name string) {
pl.registerModule(reflect.TypeFor[t](), name)
}
func RegisterSubCommands(funcs map[string]func([]string)error) {
pl.registerSubCommands(funcs)
}
func RegisterFlags(flags flag.FlagSet) {
pl.registerFlags(flags)
}
func RegisterHook[t any](p ...Patchset) {
pl.registerHook(reflect.TypeFor[t](), p...)
}
func Initialize(dirpath string) {
pl.initialize(dirpath)
}
func GetModules[t any]() []t {
mods := pl.getModules(reflect.TypeFor[t]())
res := make([]t, len(mods))
for i, m := range mods {
res[i] = m.(t)
}
return res
}
func GetModulesByMethodName(name string) []any {
return pl.getModulesByMethodName(name)
}
func StoreSingleton[t any](value t) error {
return pl.storeSingleton(reflect.TypeFor[t](), value)
}
func GetSingleton[t any]() (t, bool) {
v, ok := pl.getSingleton(reflect.TypeFor[t]())
if !ok {
var x t
return x, ok
}
return v.(t), ok
}
func HasModule(name string) bool {
return pl.hasModule(name)
}
func ParseCommands(commands []string) (int,bool) {
return pl.parseCommands(commands)
}
func RunSubcommand() (bool, error) {
return pl.runSubcommand()
}
func GetConfig[T any](name string) (*T, bool) {
files, err := ioutil.ReadDir(configPath)
if err != nil {
log.Warn("Could not load plugins config directory, config values set to default.", "path", configPath)
return nil, false
}
var fpath string
for _, file := range files {
ext := filepath.Ext(file.Name())
nameWithoutExt := strings.TrimSuffix(file.Name(), ext)
if nameWithoutExt == name {
if !strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".yml") {
log.Warn("plugin config file is not .yml or .yaml file. Skipping.", "file", file.Name())
continue
} else {
fpath = path.Join(configPath, file.Name())
}
} else {
log.Warn("plugin config file does not exist")
continue
}
}
c := new(T)
data, err := ioutil.ReadFile(fpath)
if err != nil {
log.Error("error reading plugin config", "err", err)
return nil, false
}
if err := yaml.Unmarshal(data, c); err != nil {
log.Error("error unmarshalling plugin config", "err", err)
return nil, false
}
return c, true
}
func GetPatchsets() [][]Patchset {
res := make([][]Patchset, 0, len(pl.patchsets))
for _, patchsets := range pl.patchsets {
res = append(res, patchsets)
}
return res
}