-
Notifications
You must be signed in to change notification settings - Fork 182
/
type.go
72 lines (58 loc) · 1.32 KB
/
type.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
package sanity
import (
"fmt"
"github.com/spf13/viper"
"strings"
)
const (
FlagDisableSanity = "disable-sanity"
)
// item: app's flags
type item interface {
// label: get item's name
label() string
// check: whether the userSetting value is equal to the conflicts value
check() bool
// verbose: show the readable flag
verbose() string
}
type boolItem struct {
name string
value bool
}
func (b boolItem) label() string {
return b.name
}
func (b boolItem) check() bool {
return viper.GetBool(b.label()) == b.value
}
func (b boolItem) verbose() string {
return fmt.Sprintf("--%v=%v", b.name, b.value)
}
type stringItem struct {
name string
value string
}
func (s stringItem) label() string {
return s.name
}
func (s stringItem) check() bool {
return strings.ToLower(viper.GetString(s.label())) == s.value
}
func (s stringItem) verbose() string {
return fmt.Sprintf("--%v=%v", s.name, s.value)
}
// conflictPair: configA and configB are conflict pair
type conflictPair struct {
configA item
configB item
}
// checkConflict: check configA vs configB
// and the value is equal to the conflicts value then complain it
func (cp *conflictPair) checkConflict() error {
if cp.configA.check() &&
cp.configB.check() {
return fmt.Errorf(" %v conflict with %v", cp.configA.verbose(), cp.configB.verbose())
}
return nil
}