-
Notifications
You must be signed in to change notification settings - Fork 211
/
base.go
160 lines (134 loc) · 4.44 KB
/
base.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
// Package cmd is the base package for various sets of builds and executables created from go-spacemesh
package cmd
import (
"math/big"
"reflect"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/config"
"github.com/spacemeshos/go-spacemesh/node/flags"
)
var (
// Version is the app's semantic version. Designed to be overwritten by make.
Version string
// Branch is the git branch used to build the App. Designed to be overwritten by make.
Branch string
// Commit is the git commit used to build the app. Designed to be overwritten by make.
Commit string
)
// EnsureCLIFlags checks flag types and converts them.
func EnsureCLIFlags(cmd *cobra.Command, appCFG *config.Config) error {
assignFields := func(p reflect.Type, elem reflect.Value, name string) {
for i := 0; i < p.NumField(); i++ {
if p.Field(i).Tag.Get("mapstructure") == name {
var val any
switch p.Field(i).Type.String() {
case "bool":
val = viper.GetBool(name)
case "string":
val = viper.GetString(name)
case "int":
val = viper.GetInt(name)
case "int8":
val = int8(viper.GetInt(name))
case "int16":
val = int16(viper.GetInt(name))
case "int32":
val = viper.GetInt32(name)
case "int64":
val = viper.GetInt64(name)
case "uint":
val = viper.GetUint(name)
case "uint8":
val = uint8(viper.GetUint(name))
case "uint16":
val = uint16(viper.GetUint(name))
case "uint32":
val = viper.GetUint32(name)
case "uint64":
val = viper.GetUint64(name)
case "float64":
val = viper.GetFloat64(name)
case "[]string":
val = viper.GetStringSlice(name)
case "time.Duration":
val = viper.GetDuration(name)
case "map[string]uint64":
val = flags.CastStringToMapStringUint64(viper.GetString(name))
case "*big.Rat":
v, ok := new(big.Rat).SetString(viper.GetString(name))
if !ok {
panic("bad string for *big.Rat provided")
}
val = v
case "types.RoundID":
val = types.RoundID(viper.GetUint64(name))
default:
val = viper.Get(name)
}
elem.Field(i).Set(reflect.ValueOf(val))
return
}
}
}
// this is ugly but we have to do this because viper can't handle nested structs when deserialize
cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
if f.Changed {
name := f.Name
ff := reflect.TypeOf(appCFG.BaseConfig)
elem := reflect.ValueOf(&appCFG.BaseConfig).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(*appCFG)
elem = reflect.ValueOf(&appCFG).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(*appCFG.Genesis)
elem = reflect.ValueOf(appCFG.Genesis).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.API)
elem = reflect.ValueOf(&appCFG.API).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.P2P)
elem = reflect.ValueOf(&appCFG.P2P).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.TIME)
elem = reflect.ValueOf(&appCFG.TIME).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.HARE)
elem = reflect.ValueOf(&appCFG.HARE).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.HareEligibility)
elem = reflect.ValueOf(&appCFG.HareEligibility).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.Beacon)
elem = reflect.ValueOf(&appCFG.Beacon).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.POET)
elem = reflect.ValueOf(&appCFG.POET).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.POST)
elem = reflect.ValueOf(&appCFG.POST).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.SMESHING)
elem = reflect.ValueOf(&appCFG.SMESHING).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.SMESHING.Opts)
elem = reflect.ValueOf(&appCFG.SMESHING.Opts).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.LOGGING)
elem = reflect.ValueOf(&appCFG.LOGGING).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.Tortoise)
elem = reflect.ValueOf(&appCFG.Tortoise).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.Bootstrap)
elem = reflect.ValueOf(&appCFG.Bootstrap).Elem()
assignFields(ff, elem, name)
ff = reflect.TypeOf(appCFG.Recovery)
elem = reflect.ValueOf(&appCFG.Recovery).Elem()
assignFields(ff, elem, name)
}
})
return nil
}