This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
/
main.go
144 lines (111 loc) · 2.86 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"plugin"
"reflect"
"github.com/KablamoOSS/kombustion/pkg/plugins/api/types"
log "github.com/sirupsen/logrus"
)
type TypeDefinition struct {
Name string
Description string
Fields []FieldDefinition
}
type FieldDefinition struct {
Name string
Description string
ExampleValue string
Children []FieldDefinition
}
func getPluginDir() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
plugindir := fmt.Sprintf("%s/.kombustion/plugins", usr.HomeDir)
os.MkdirAll(plugindir, 0744)
return plugindir
}
func loadPlugins() []TypeDefinition {
pluginBaseDir := ""
processedTypes := []TypeDefinition{}
if len(os.Getenv("PLUGINS")) > 0 {
pluginBaseDir = os.Getenv("PLUGINS")
} else {
pluginBaseDir = getPluginDir()
}
pluginDirectories, err := ioutil.ReadDir(pluginBaseDir)
if err != nil {
// log.Println("WARNING: ", err)
return processedTypes
}
for _, d := range pluginDirectories {
if !d.IsDir() {
// load all plugins in the base dir
processedTypes = append(processedTypes, loadPlugin(d.Name(), pluginBaseDir)...)
continue
}
pluginDir := filepath.Join(pluginBaseDir, d.Name())
plugins, err := ioutil.ReadDir(pluginDir)
if err != nil {
continue
}
// load all plugins in each sub dir
for _, f := range plugins {
processedTypes = append(processedTypes, loadPlugin(f.Name(), pluginDir)...)
}
}
return processedTypes
}
func loadPlugin(filename, pluginDir string) (ret []TypeDefinition) {
if filepath.Ext(filename) != ".so" && filepath.Ext(filename) != ".dll" && filepath.Ext(filename) != ".dylib" {
return
}
pluginPath := filepath.Join(pluginDir, filename)
p, err := plugin.Open(pluginPath)
if err != nil {
log.WithFields(log.Fields{
"filename": filename,
"err": err,
}).Warn("error reading plugin file")
return
}
help, _ := p.Lookup("Help")
if help != nil {
for _, typeMapping := range help.(*types.Help).Types {
var typeDef TypeDefinition
typeDef.Name = typeMapping.Name
typeDef.Description = typeMapping.Description
props := reflect.ValueOf(typeMapping.Config).FieldByName("Properties").Interface()
for i := 0; i < reflect.TypeOf(props).NumField(); i++ {
var fieldDef FieldDefinition
field := reflect.TypeOf(props).Field(i)
fieldDef.Name = field.Name
exampleVal, hasExampleSet := field.Tag.Lookup("example")
if !hasExampleSet {
exampleVal = ""
}
fieldDef.ExampleValue = exampleVal
typeDef.Fields = append(typeDef.Fields, fieldDef)
}
ret = append(ret, typeDef)
}
}
return
}
func loadPrimaryResources() {
/*for _, v := range parsers.GetParsers_resources() {
log.Warn(reflect.TypeOf(v))
}*/
}
func main() {
res := loadPlugins()
loadPrimaryResources()
bytes, _ := json.Marshal(res)
fmt.Println(string(bytes))
}