-
Notifications
You must be signed in to change notification settings - Fork 107
/
flags.go
151 lines (123 loc) · 4.49 KB
/
flags.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
// Package flags implements common flags used across multiple commands and
// backends.
package flags
import (
"strings"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
// CfgDebugDontBlameOasis is the flag used to opt-in to unsafe/debug/test
// behavior.
CfgDebugDontBlameOasis = "debug.dont_blame_oasis"
// CfgDebugTestEntity is the command line flag to enable the debug test
// entity.
CfgDebugTestEntity = "debug.test_entity"
// CfgGenesisFile is the flag used to specify a genesis file.
CfgGenesisFile = "genesis.file"
// CfgConsensusValidator is the flag used to opt-in to being a validator.
CfgConsensusValidator = "consensus.validator"
cfgVerbose = "verbose"
cfgForce = "force"
cfgRetries = "retries"
// CfgDryRun is the flag used to specify a dry-run of an operation.
CfgDryRun = "dry_run"
)
var (
// VerboseFlags has the verbose flag.
VerboseFlags = flag.NewFlagSet("", flag.ContinueOnError)
// ForceFlags has the force flag.
ForceFlags = flag.NewFlagSet("", flag.ContinueOnError)
// RetriesFlags has the retries flag.
RetriesFlags = flag.NewFlagSet("", flag.ContinueOnError)
// DebugTestEntityFlags has the test entity enable flag.
DebugTestEntityFlags = flag.NewFlagSet("", flag.ContinueOnError)
// GenesisFileFlags has the genesis file flag.
GenesisFileFlags = flag.NewFlagSet("", flag.ContinueOnError)
// ConsensusValidatorFlag has the consensus validator flag.
ConsensusValidatorFlag = flag.NewFlagSet("", flag.ContinueOnError)
// DebugDontBlameOasisFlag has the "don't blame oasis" flag.
DebugDontBlameOasisFlag = flag.NewFlagSet("", flag.ContinueOnError)
// DryRunFlag has the dry-run flag.
DryRunFlag = flag.NewFlagSet("", flag.ContinueOnError)
)
// Verbose returns true iff the verbose flag is set.
func Verbose() bool {
return viper.GetBool(cfgVerbose)
}
// Force returns true iff the force flag is set.
func Force() bool {
return viper.GetBool(cfgForce)
}
// Retries returns the retries flag value.
func Retries() int {
return viper.GetInt(cfgRetries)
}
// ConsensusValidator returns true iff the node is opting in to be a consensus
// validator.
func ConsensusValidator() bool {
return viper.GetBool(CfgConsensusValidator)
}
// DebugTestEntity returns true iff the test entity enable flag is set.
func DebugTestEntity() bool {
return DebugDontBlameOasis() && viper.GetBool(CfgDebugTestEntity)
}
// GenesisFile returns the set genesis file.
func GenesisFile() string {
return viper.GetString(CfgGenesisFile)
}
// DebugDontBlameOasis returns true iff the "don't blame oasis" flag is set.
func DebugDontBlameOasis() bool {
return viper.GetBool(CfgDebugDontBlameOasis)
}
// DryRun returns true iff the dry-run flag is set.
func DryRun() bool {
return viper.GetBool(CfgDryRun)
}
// GetStringMapString is a drop-in replacement for viper.GetStringMapString due to https://github.com/spf13/viper/issues/608.
func GetStringMapString(key string) map[string]string {
// Use original GetStringMapString, if it works (e.g. for yaml-based configs).
labels := viper.GetStringMapString(key)
if len(labels) != 0 {
return labels
}
// Otherwise, take string and parse it manually.
val := viper.GetString(key)
// viper.GetString() wraps the string inside [] parenthesis, unwrap it.
if len(val) >= 2 && val[0] == '[' && val[len(val)-1] == ']' {
val = val[1 : len(val)-1]
}
// Assume format key1=value1,key2=value2,...
for _, lPair := range strings.Split(val, ",") {
kv := strings.Split(lPair, "=")
if len(kv) != 2 || kv[0] == "" {
continue
}
labels[kv[0]] = kv[1]
}
return labels
}
func init() {
VerboseFlags.BoolP(cfgVerbose, "v", false, "verbose output")
ForceFlags.Bool(cfgForce, false, "force")
RetriesFlags.Int(cfgRetries, 0, "retries (-1 = forever)")
ConsensusValidatorFlag.Bool(CfgConsensusValidator, false, "node is a consensus validator")
DebugTestEntityFlags.Bool(CfgDebugTestEntity, false, "use the test entity (UNSAFE)")
_ = DebugTestEntityFlags.MarkHidden(CfgDebugTestEntity)
GenesisFileFlags.StringP(CfgGenesisFile, "g", "genesis.json", "path to genesis file")
DebugDontBlameOasisFlag.Bool(CfgDebugDontBlameOasis, false, "Enable debug/unsafe/insecure options")
_ = DebugDontBlameOasisFlag.MarkHidden(CfgDebugDontBlameOasis)
DryRunFlag.BoolP(CfgDryRun, "n", false, "don't actually do anything, just show what will be done")
for _, v := range []*flag.FlagSet{
VerboseFlags,
ForceFlags,
RetriesFlags,
DebugTestEntityFlags,
GenesisFileFlags,
ConsensusValidatorFlag,
DebugDontBlameOasisFlag,
DryRunFlag,
} {
_ = viper.BindPFlags(v)
}
}