/
validate.go
206 lines (161 loc) · 4.34 KB
/
validate.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"fmt"
"github.com/manifoldco/go-manifold"
"github.com/urfave/cli"
"github.com/manifoldco/manifold-cli/errs"
)
func validateTitle(cliCtx *cli.Context, option string, typeName ...string) (string, error) {
val := cliCtx.String(option)
if val == "" {
return val, nil
}
if len(typeName) > 1 {
panic("Only one typeName can be provided")
} else if len(typeName) == 0 {
typeName = make([]string, 1)
typeName[0] = option
}
title := manifold.Name(val)
if err := title.Validate(nil); err != nil {
return "", errs.NewUsageExitError(cliCtx, cli.NewExitError(
fmt.Sprintf("You've provided an invalid %s title!", typeName[0]), -1,
))
}
return val, nil
}
func validateName(cliCtx *cli.Context, option string) (string, error) {
val := cliCtx.String(option)
if val == "" {
return val, nil
}
name := manifold.Label(val)
if err := name.Validate(nil); err != nil {
return "", errs.NewUsageExitError(cliCtx, cli.NewExitError(
fmt.Sprintf("You've provided an invalid %s!", option), -1,
))
}
return val, nil
}
func requiredName(cliCtx *cli.Context, option string) (string, error) {
val := cliCtx.String(option)
if val == "" {
return "", errs.NewUsageExitError(cliCtx, cli.NewExitError(
fmt.Sprintf("--%s is required", option), -1,
))
}
return validateName(cliCtx, option)
}
func requiredArgName(cliCtx *cli.Context, idx int, option string) (string, error) {
args := cliCtx.Args()
if len(args) < idx+1 {
return "", errs.ErrTooFewArgs
}
val := args[idx]
name := manifold.Label(val)
email := manifold.Email(val)
nErr := name.Validate(nil)
eErr := email.Validate(nil)
if nErr != nil && eErr != nil {
return "", errs.NewUsageExitError(cliCtx, cli.NewExitError(
fmt.Sprintf("You've provided an invalid %s!", option), -1,
))
}
return val, nil
}
func optionalArgID(cliCtx *cli.Context, idx int, name string) (*manifold.ID, error) {
args := cliCtx.Args()
if len(args) < idx+1 {
return nil, nil
}
val := args[idx]
id, err := manifold.DecodeIDFromString(val)
if err != nil {
return nil, cli.NewExitError(fmt.Sprintf("Invalid %s ID: %s", name, err), -1)
}
return &id, nil
}
func optionalArgTitle(cliCtx *cli.Context, idx int, title string) (string, error) {
args := cliCtx.Args()
if len(args) < idx+1 {
return "", nil
}
val := args[idx]
l := manifold.Name(val)
if err := l.Validate(nil); err != nil {
return "", errs.NewUsageExitError(cliCtx, cli.NewExitError(
fmt.Sprintf("You've provided an invalid %s!", title), -1,
))
}
return val, nil
}
func optionalArgName(cliCtx *cli.Context, idx int, name string) (string, error) {
args := cliCtx.Args()
if len(args) < idx+1 {
return "", nil
}
val := args[idx]
l := manifold.Label(val)
if err := l.Validate(nil); err != nil {
return "", errs.NewUsageExitError(cliCtx, cli.NewExitError(
fmt.Sprintf("You've provided an invalid %s!", name), -1,
))
}
return val, nil
}
func optionalArgEmail(cliCtx *cli.Context, idx int, name string) (string, error) {
args := cliCtx.Args()
if len(args) < idx+1 {
return "", nil
}
val := args[idx]
n := manifold.Email(val)
if err := n.Validate(nil); err != nil {
return "", errs.NewUsageExitError(cliCtx, cli.NewExitError(
fmt.Sprintf("You've provided an invalid %s email", name), -1,
))
}
return val, nil
}
func optionalArgCode(cliCtx *cli.Context, idx int, name string) (string, error) {
args := cliCtx.Args()
if len(args) < idx+1 {
return "", nil
}
val := args[idx]
c := manifold.Code(val)
if err := c.Validate(nil); err != nil {
return "", errs.NewUsageExitError(cliCtx, cli.NewExitError(
fmt.Sprintf("You've provided an invalid %s code!", name), -1,
))
}
return val, nil
}
func maxOptionalArgsLength(cliCtx *cli.Context, size int) error {
args := cliCtx.Args()
if len(args) > size {
return errs.ErrTooManyArgs
}
return nil
}
func exactArgsLength(cliCtx *cli.Context, size int) error {
args := cliCtx.Args()
if len(args) > size {
return errs.ErrTooManyArgs
} else if len(args) < size {
return errs.ErrTooFewArgs
}
return nil
}
func validateTeamID(cliCtx *cli.Context) (*manifold.ID, error) {
var teamID *manifold.ID
cliTeamID := cliCtx.String("team-id")
if cliTeamID != "" {
id, err := manifold.DecodeIDFromString(cliTeamID)
if err != nil {
return nil, cli.NewExitError(fmt.Sprintf("Invalid team ID: %s", err), -1)
}
teamID = &id
}
return teamID, nil
}