-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
172 lines (153 loc) · 3.53 KB
/
request.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
161
162
163
164
165
166
167
168
169
170
171
172
package clo
import (
"errors"
"fmt"
)
type request struct {
Command string
Arguments map[string][]string
lastArgument string
}
func (req *request) hasArguments() bool {
return req != nil && len(req.Arguments) > 0
}
func (req *request) setDefaultContext(tokenType int, def *definitions) error {
switch tokenType {
case argument:
if req.Command == "" {
dc := def.defaultCommand()
if dc != "" {
return req.update(trimAttrs(dc), command)
}
}
case value:
if req.lastArgument == "" {
da, err := def.defaultArgument(req.Command)
if err != nil {
return err
}
if da != "" {
return req.update(trimAttrs(da), argument)
}
}
}
return nil
}
func (req *request) update(token string, tokenType int) error {
switch tokenType {
case command:
if req.Command != "" {
return errors.New("request already has a command specified")
}
req.Command = token
case argument:
arg := trimArgPrefix(token)
req.lastArgument = arg
if req.Arguments == nil {
req.Arguments = map[string][]string{}
}
if req.Arguments[req.lastArgument] == nil {
req.Arguments[req.lastArgument] = []string{}
}
case value:
if req.lastArgument == "" {
return fmt.Errorf("cannot update value for a request with no arguments")
}
req.Arguments[req.lastArgument] = append(req.Arguments[req.lastArgument], token)
default:
return fmt.Errorf(
"cannot update request for a token '%v' of type '%v'",
token,
tokenString(tokenType))
}
return nil
}
func (req *request) commandHasRequiredArgs(def *definitions) error {
if def == nil {
return errors.New("cannot validate required arguments using nil definitions")
}
if req == nil {
return errors.New("cannot validate required arguments using nil request")
}
dc, err := def.definedCmd(req.Command)
if err != nil {
return err
}
if dc == "" {
return fmt.Errorf("cannot validate required arguments without a command")
}
for _, arg := range def.Cmd[dc] {
if !isRequired(arg) {
continue
}
tArg := trimAttrs(arg)
if _, ok := req.Arguments[tArg]; !ok {
return fmt.Errorf("required argument '%v' is missing for the command '%v'", tArg, req.Command)
}
}
return nil
}
func (req *request) argumentsMultipleValues(def *definitions) error {
if def == nil {
return errors.New("cannot validate required argument using nil definitions")
}
if req == nil {
return errors.New("cannot validate nil request for required arguments")
}
for arg, values := range req.Arguments {
if arg == "" {
continue
}
da, err := def.definedArg(req.Command, arg)
if err != nil {
return err
}
if da == "" {
continue
}
if !isMultiple(da) && len(values) > 1 {
return fmt.Errorf("argument '%v' has multiple values, supports no more than one", arg)
}
}
return nil
}
func (req *request) validate(def *definitions) error {
if def == nil {
return errors.New("cannot validate required argument using nil definitions")
}
if req == nil {
return errors.New("cannot validate nil request for required arguments")
}
err := req.commandHasRequiredArgs(def)
if err != nil {
return err
}
err = req.argumentsMultipleValues(def)
if err != nil {
return err
}
return nil
}
func (req *request) ArgVal(arg string) string {
if req == nil {
return ""
}
vs := req.Arguments[arg]
if len(vs) > 0 {
return vs[0]
}
return ""
}
func (req *request) ArgValues(arg string) []string {
if req == nil {
return []string{}
}
return req.Arguments[arg]
}
func (req *request) Flag(arg string) bool {
if req == nil {
return false
}
_, ok := req.Arguments[arg]
return ok
}