forked from marcetin/parallelcoin
-
Notifications
You must be signed in to change notification settings - Fork 5
/
pod.go
108 lines (98 loc) · 2.42 KB
/
pod.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
package pod
import (
"github.com/p9c/pod/pkg/logg"
"github.com/p9c/pod/pkg/podcfg"
"reflect"
"sort"
)
type Schema struct {
Groups Groups `json:"groups"`
}
type Groups []Group
type Group struct {
Legend string `json:"legend"`
Fields `json:"fields"`
}
type Fields []Field
func (f Fields) Len() int {
return len(f)
}
func (f Fields) Less(i, j int) bool {
return f[i].Label < f[j].Label
}
func (f Fields) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
type Field struct {
Datatype string `json:"datatype"`
Description string `json:"help"`
Featured string `json:"featured"`
Group string `json:"group"`
Hooks string `json:"hooks"`
Label string `json:"label"`
Model string `json:"model"`
Options []string `json:"options"`
Restart string `json:"restart"`
Slug string `json:"slug"`
Type string `json:"type"`
Widget string `json:"inputType"`
}
// GetConfigSchema returns a schema for a given config
func GetConfigSchema(cfg *podcfg.Config) Schema {
t := reflect.TypeOf(cfg)
t = t.Elem()
var levelOptions, network []string
for i := range logg.LevelSpecs {
levelOptions = append(levelOptions, logg.LevelSpecs[i].Name)
}
network = []string{"mainnet", "testnet", "regtestnet", "simnet"}
rawFields := make(map[string]Fields)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
var options []string
switch {
case field.Name == "LogLevel":
options = levelOptions
case field.Name == "Network":
options = network
}
f := Field{
Datatype: field.Type.String(),
Description: field.Tag.Get("description"),
Featured: field.Tag.Get("featured"),
Group: field.Tag.Get("group"),
Hooks: field.Tag.Get("hooks"),
Label: field.Tag.Get("label"),
Model: field.Tag.Get("json"),
Options: options,
Slug: field.Name,
Type: field.Tag.Get("type"),
Widget: field.Tag.Get("widget"),
}
if f.Group != "" {
rawFields[f.Group] = append(rawFields[f.Group], f)
}
}
for i := range rawFields {
sort.Sort(rawFields[i])
}
var outGroups Groups
var rf []string
for i := range rawFields {
rf = append(rf, i)
}
sort.Strings(rf)
for i := range rf {
rf[i], rf[len(rf)-1-i] = rf[len(rf)-1-i], rf[i]
}
for i := range rf {
group := Group{
Legend: rf[i],
Fields: rawFields[rf[i]],
}
outGroups = append(Groups{group}, outGroups...)
}
return Schema{
Groups: outGroups,
}
}