-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.go
382 lines (346 loc) · 8.8 KB
/
commands.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Code generated by goagen v1.3.1, DO NOT EDIT.
//
// API "user": CLI Commands
//
// Command:
// $ goagen
// --design=github.com/Microkubes/microservice-registration/design
// --out=$(GOPATH)/src/github.com/Microkubes/microservice-registration
// --version=v1.3.1
package cli
import (
"context"
"encoding/json"
"fmt"
"github.com/Microkubes/microservice-registration/client"
"github.com/goadesign/goa"
goaclient "github.com/goadesign/goa/client"
uuid "github.com/goadesign/goa/uuid"
"github.com/spf13/cobra"
"log"
"os"
"path"
"strconv"
"strings"
"time"
)
type (
// RegisterUserCommand is the command line data structure for the register action of user
RegisterUserCommand struct {
Payload string
ContentType string
PrettyPrint bool
}
// ResendVerificationUserCommand is the command line data structure for the resendVerification action of user
ResendVerificationUserCommand struct {
Payload string
ContentType string
PrettyPrint bool
}
// DownloadCommand is the command line data structure for the download command.
DownloadCommand struct {
// OutFile is the path to the download output file.
OutFile string
}
)
// RegisterCommands registers the resource action CLI commands.
func RegisterCommands(app *cobra.Command, c *client.Client) {
var command, sub *cobra.Command
command = &cobra.Command{
Use: "register",
Short: `Creates user`,
}
tmp1 := new(RegisterUserCommand)
sub = &cobra.Command{
Use: `user ["/users/register"]`,
Short: ``,
Long: `
Payload example:
{
"active": true,
"email": "breana@rennerkoepp.com",
"externalId": "At consequatur saepe.",
"fullname": "ApPq",
"password": "md877qphg",
"roles": [
"Quo quo amet occaecati ut."
],
"token": "Adipisci dicta facere dolorem distinctio cupiditate."
}`,
RunE: func(cmd *cobra.Command, args []string) error { return tmp1.Run(c, args) },
}
tmp1.RegisterFlags(sub, c)
sub.PersistentFlags().BoolVar(&tmp1.PrettyPrint, "pp", false, "Pretty print response body")
command.AddCommand(sub)
app.AddCommand(command)
command = &cobra.Command{
Use: "resend-verification",
Short: `Resends verification email and resets valiation tokens`,
}
tmp2 := new(ResendVerificationUserCommand)
sub = &cobra.Command{
Use: `user ["/users/register/resend-verification"]`,
Short: ``,
Long: `
Payload example:
{
"email": "Voluptas repellat doloremque aut sed."
}`,
RunE: func(cmd *cobra.Command, args []string) error { return tmp2.Run(c, args) },
}
tmp2.RegisterFlags(sub, c)
sub.PersistentFlags().BoolVar(&tmp2.PrettyPrint, "pp", false, "Pretty print response body")
command.AddCommand(sub)
app.AddCommand(command)
dl := new(DownloadCommand)
dlc := &cobra.Command{
Use: "download [PATH]",
Short: "Download file with given path",
RunE: func(cmd *cobra.Command, args []string) error {
return dl.Run(c, args)
},
}
dlc.Flags().StringVar(&dl.OutFile, "out", "", "Output file")
app.AddCommand(dlc)
}
func intFlagVal(name string, parsed int) *int {
if hasFlag(name) {
return &parsed
}
return nil
}
func float64FlagVal(name string, parsed float64) *float64 {
if hasFlag(name) {
return &parsed
}
return nil
}
func boolFlagVal(name string, parsed bool) *bool {
if hasFlag(name) {
return &parsed
}
return nil
}
func stringFlagVal(name string, parsed string) *string {
if hasFlag(name) {
return &parsed
}
return nil
}
func hasFlag(name string) bool {
for _, arg := range os.Args[1:] {
if strings.HasPrefix(arg, "--"+name) {
return true
}
}
return false
}
func jsonVal(val string) (*interface{}, error) {
var t interface{}
err := json.Unmarshal([]byte(val), &t)
if err != nil {
return nil, err
}
return &t, nil
}
func jsonArray(ins []string) ([]interface{}, error) {
if ins == nil {
return nil, nil
}
var vals []interface{}
for _, id := range ins {
val, err := jsonVal(id)
if err != nil {
return nil, err
}
vals = append(vals, val)
}
return vals, nil
}
func timeVal(val string) (*time.Time, error) {
t, err := time.Parse(time.RFC3339, val)
if err != nil {
return nil, err
}
return &t, nil
}
func timeArray(ins []string) ([]time.Time, error) {
if ins == nil {
return nil, nil
}
var vals []time.Time
for _, id := range ins {
val, err := timeVal(id)
if err != nil {
return nil, err
}
vals = append(vals, *val)
}
return vals, nil
}
func uuidVal(val string) (*uuid.UUID, error) {
t, err := uuid.FromString(val)
if err != nil {
return nil, err
}
return &t, nil
}
func uuidArray(ins []string) ([]uuid.UUID, error) {
if ins == nil {
return nil, nil
}
var vals []uuid.UUID
for _, id := range ins {
val, err := uuidVal(id)
if err != nil {
return nil, err
}
vals = append(vals, *val)
}
return vals, nil
}
func float64Val(val string) (*float64, error) {
t, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, err
}
return &t, nil
}
func float64Array(ins []string) ([]float64, error) {
if ins == nil {
return nil, nil
}
var vals []float64
for _, id := range ins {
val, err := float64Val(id)
if err != nil {
return nil, err
}
vals = append(vals, *val)
}
return vals, nil
}
func boolVal(val string) (*bool, error) {
t, err := strconv.ParseBool(val)
if err != nil {
return nil, err
}
return &t, nil
}
func boolArray(ins []string) ([]bool, error) {
if ins == nil {
return nil, nil
}
var vals []bool
for _, id := range ins {
val, err := boolVal(id)
if err != nil {
return nil, err
}
vals = append(vals, *val)
}
return vals, nil
}
// Run downloads files with given paths.
func (cmd *DownloadCommand) Run(c *client.Client, args []string) error {
var (
fnf func(context.Context, string) (int64, error)
fnd func(context.Context, string, string) (int64, error)
rpath = args[0]
outfile = cmd.OutFile
logger = goa.NewLogger(log.New(os.Stderr, "", log.LstdFlags))
ctx = goa.WithLogger(context.Background(), logger)
err error
)
if rpath[0] != '/' {
rpath = "/" + rpath
}
if rpath == "/swagger.json" {
fnf = c.DownloadSwaggerJSON
if outfile == "" {
outfile = "swagger.json"
}
goto found
}
if strings.HasPrefix(rpath, "/swagger-ui/") {
fnd = c.DownloadSwaggerUI
rpath = rpath[12:]
if outfile == "" {
_, outfile = path.Split(rpath)
}
goto found
}
return fmt.Errorf("don't know how to download %s", rpath)
found:
ctx = goa.WithLogContext(ctx, "file", outfile)
if fnf != nil {
_, err = fnf(ctx, outfile)
} else {
_, err = fnd(ctx, rpath, outfile)
}
if err != nil {
goa.LogError(ctx, "failed", "err", err)
return err
}
return nil
}
// Run makes the HTTP request corresponding to the RegisterUserCommand command.
func (cmd *RegisterUserCommand) Run(c *client.Client, args []string) error {
var path string
if len(args) > 0 {
path = args[0]
} else {
path = "/users/register"
}
var payload client.UserPayload
if cmd.Payload != "" {
err := json.Unmarshal([]byte(cmd.Payload), &payload)
if err != nil {
return fmt.Errorf("failed to deserialize payload: %s", err)
}
}
logger := goa.NewLogger(log.New(os.Stderr, "", log.LstdFlags))
ctx := goa.WithLogger(context.Background(), logger)
resp, err := c.RegisterUser(ctx, path, &payload, cmd.ContentType)
if err != nil {
goa.LogError(ctx, "failed", "err", err)
return err
}
goaclient.HandleResponse(c.Client, resp, cmd.PrettyPrint)
return nil
}
// RegisterFlags registers the command flags with the command line.
func (cmd *RegisterUserCommand) RegisterFlags(cc *cobra.Command, c *client.Client) {
cc.Flags().StringVar(&cmd.Payload, "payload", "", "Request body encoded in JSON")
cc.Flags().StringVar(&cmd.ContentType, "content", "", "Request content type override, e.g. 'application/x-www-form-urlencoded'")
}
// Run makes the HTTP request corresponding to the ResendVerificationUserCommand command.
func (cmd *ResendVerificationUserCommand) Run(c *client.Client, args []string) error {
var path string
if len(args) > 0 {
path = args[0]
} else {
path = "/users/register/resend-verification"
}
var payload client.ResendVerificationPayload
if cmd.Payload != "" {
err := json.Unmarshal([]byte(cmd.Payload), &payload)
if err != nil {
return fmt.Errorf("failed to deserialize payload: %s", err)
}
}
logger := goa.NewLogger(log.New(os.Stderr, "", log.LstdFlags))
ctx := goa.WithLogger(context.Background(), logger)
resp, err := c.ResendVerificationUser(ctx, path, &payload, cmd.ContentType)
if err != nil {
goa.LogError(ctx, "failed", "err", err)
return err
}
goaclient.HandleResponse(c.Client, resp, cmd.PrettyPrint)
return nil
}
// RegisterFlags registers the command flags with the command line.
func (cmd *ResendVerificationUserCommand) RegisterFlags(cc *cobra.Command, c *client.Client) {
cc.Flags().StringVar(&cmd.Payload, "payload", "", "Request body encoded in JSON")
cc.Flags().StringVar(&cmd.ContentType, "content", "", "Request content type override, e.g. 'application/x-www-form-urlencoded'")
}