forked from subscan-explorer/subscan-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
52 lines (42 loc) · 1.07 KB
/
registry.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
package plugins
import (
"github.com/itering/subscan-plugin"
"github.com/itering/subscan/plugins/balance"
"github.com/itering/subscan/plugins/system"
"reflect"
"strings"
)
type PluginFactory subscan_plugin.Plugin
var RegisteredPlugins = make(map[string]PluginFactory)
// register local plugin
func init() {
registerNative(balance.New())
registerNative(system.New())
}
func register(name string, f interface{}) {
name = strings.ToLower(name)
if f == nil {
return
}
if _, ok := RegisteredPlugins[name]; ok {
return
}
if _, ok := f.(PluginFactory); ok {
RegisteredPlugins[name] = f.(PluginFactory)
}
}
func registerNative(p interface{}) {
register(reflect.ValueOf(p).Type().Elem().Name(), p)
}
type PluginInfo struct {
Name string `json:"name"`
Version string `json:"version"`
Ui bool `json:"ui"`
}
func List() []PluginInfo {
plugins := make([]PluginInfo, 0, len(RegisteredPlugins))
for name, plugin := range RegisteredPlugins {
plugins = append(plugins, PluginInfo{Name: name, Version: plugin.Version(), Ui: plugin.UiConf() != nil})
}
return plugins
}