/
cmds_call.go
97 lines (83 loc) 路 2.13 KB
/
cmds_call.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
package model
import (
"regexp"
log "github.com/sirupsen/logrus"
)
type CallCmds map[string]*CallCmdSpec
func (cmds CallCmds) Validate(ctx AppContext, spec *Spec) bool {
validateLog := log.WithFields(log.Fields{
"section": "CallCmds",
"func": "Validate",
})
for name, cmd := range cmds {
if _, ok := spec.uniqueNames[name]; ok {
validateLog.WithField("name", name).Errorln("cmd name is not unique")
return false
}
spec.uniqueNames[name] = struct{}{}
if ctx.AppCommand() == name {
if !cmd.Validate(ctx, name, spec) {
return false
}
}
}
return true
}
func (cmds CallCmds) CallCmdSpec(name string) (*CallCmdSpec, bool) {
spec, ok := cmds[name]
return spec, ok
}
type CallCmdSpec struct {
ParamSpec `yaml:",inline"`
Description string `yaml:"desc"`
Wallet string `yaml:"wallet"`
Method string `yaml:"method"`
walletRx *regexp.Regexp `yaml:"-"`
matching []*WalletSpec `yaml:"-"`
}
func (spec *CallCmdSpec) Validate(ctx AppContext, name string, root *Spec) bool {
validateLog := log.WithFields(log.Fields{
"section": "CallCommands",
"command": name,
})
var hasWalletName bool
if len(spec.Wallet) > 0 {
if isWalletRef(spec.Wallet) {
validateLog.Errorln("wallet reference is not allowed in 'wallet' field, must be name")
return false
}
hasWalletName = true
}
rx, err := regexp.Compile(spec.Wallet)
if err != nil {
validateLog.WithError(err).Errorln("failed to compile wallet regexp")
return false
}
spec.walletRx = rx
if hasWalletName {
spec.matching = root.Wallets.GetAll(spec.walletRx)
if len(spec.matching) == 0 {
validateLog.Errorln("no wallets are matching the specified regexp")
return false
}
}
if len(spec.Method) == 0 {
validateLog.Errorln("no method name is specified")
return false
}
if !spec.ParamSpec.Validate(ctx, name, root) {
return false
}
return true
}
func (spec *CallCmdSpec) MatchingWallets() []*WalletSpec {
return spec.matching
}
func (spec *CallCmdSpec) CountArgsUsing(set map[int]struct{}) {
spec.ParamSpec.CountArgsUsing(set)
}
func (spec *CallCmdSpec) ArgCount() int {
set := make(map[int]struct{})
spec.CountArgsUsing(set)
return len(set)
}