forked from PinataCloud/pinata-go-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
560 lines (554 loc) · 14.9 KB
/
main.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package main
import (
"errors"
"log"
"os"
"strconv"
"strings"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "pinata",
Usage: "A CLI for uploading files to Pinata! To get started make an API key at https://app.pinata.cloud/keys, then authorize the CLI with the auth command with your JWT",
Commands: []*cli.Command{
{
Name: "auth",
Aliases: []string{"a"},
Usage: "Authorize the CLI with your Pinata JWT",
ArgsUsage: "[your Pinata JWT]",
Action: func(ctx *cli.Context) error {
err := SaveJWT()
return err
},
},
{
Name: "upload",
Aliases: []string{"u"},
Usage: "Upload a file to Pinata",
ArgsUsage: "[path to file]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "group",
Aliases: []string{"g"},
Value: "",
Usage: "Upload a file to a specific group by passing in the groupId",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Value: "nil",
Usage: "Add a name for the file you are uploading. By default it will use the filename on your system.",
},
&cli.BoolFlag{
Name: "verbose",
Usage: "Show upload progress",
},
},
Action: func(ctx *cli.Context) error {
filePath := ctx.Args().First()
groupId := ctx.String("group")
name := ctx.String("name")
verbose := ctx.Bool("verbose")
if filePath == "" {
return errors.New("no file path provided")
}
_, err := Upload(filePath, groupId, name, verbose)
return err
},
},
{
Name: "groups",
Aliases: []string{"g"},
Usage: "Interact with file groups",
Subcommands: []*cli.Command{
{
Name: "create",
Aliases: []string{"c"},
Usage: "Create a new group",
ArgsUsage: "[name of group]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "public",
Aliases: []string{"p"},
Value: false,
Usage: "Determine if the group should be public",
},
},
Action: func(ctx *cli.Context) error {
name := ctx.Args().First()
public := ctx.Bool("public")
if name == "" {
return errors.New("Group name required")
}
_, err := CreateGroup(name, public)
return err
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "List groups on your account",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "public",
Aliases: []string{"p"},
Usage: "List only public groups",
},
&cli.StringFlag{
Name: "amount",
Aliases: []string{"a"},
Value: "10",
Usage: "The number of groups you would like to return",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Filter groups by name",
},
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Usage: "Paginate through results using the pageToken",
},
},
Action: func(ctx *cli.Context) error {
public := ctx.Bool("public")
amount := ctx.String("amount")
name := ctx.String("name")
token := ctx.String("token")
_, err := ListGroups(amount, public, name, token)
return err
},
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "Update a group",
ArgsUsage: "[ID of group]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "public",
Aliases: []string{"p"},
Value: false,
Usage: "Determine if the group should be public",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Update the name of a group",
},
},
Action: func(ctx *cli.Context) error {
groupId := ctx.Args().First()
name := ctx.String("name")
public := ctx.Bool("public")
if groupId == "" {
return errors.New("no ID provided")
}
_, err := UpdateGroup(groupId, name, public)
return err
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "Delete a group by ID",
ArgsUsage: "[ID of group]",
Action: func(ctx *cli.Context) error {
groupId := ctx.Args().First()
if groupId == "" {
return errors.New("no ID provided")
}
err := DeleteGroup(groupId)
return err
},
},
{
Name: "get",
Aliases: []string{"g"},
Usage: "Get group info by ID",
ArgsUsage: "[ID of group]",
Action: func(ctx *cli.Context) error {
groupId := ctx.Args().First()
if groupId == "" {
return errors.New("no ID provided")
}
_, err := GetGroup(groupId)
return err
},
},
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add a file to a group",
ArgsUsage: "[group id] [file id]",
Action: func(ctx *cli.Context) error {
groupId := ctx.Args().First()
fileId := ctx.Args().Get(1)
if groupId == "" {
return errors.New("no group id provided")
}
if fileId == "" {
return errors.New("no file id provided")
}
err := AddFile(groupId, fileId)
return err
},
},
{
Name: "remove",
Aliases: []string{"r"},
Usage: "Remove a file from a group",
ArgsUsage: "[group id] [file id]",
Action: func(ctx *cli.Context) error {
groupId := ctx.Args().First()
fileId := ctx.Args().Get(1)
if groupId == "" {
return errors.New("no group id provided")
}
if fileId == "" {
return errors.New("no file id provided")
}
err := RemoveFile(groupId, fileId)
return err
},
},
},
},
{
Name: "files",
Aliases: []string{"f"},
Usage: "Interact with your files on Pinata",
Subcommands: []*cli.Command{
{
Name: "delete",
Aliases: []string{"d"},
Usage: "Delete a file by ID",
ArgsUsage: "[ID of file]",
Action: func(ctx *cli.Context) error {
fileId := ctx.Args().First()
if fileId == "" {
return errors.New("no file ID provided")
}
err := DeleteFile(fileId)
return err
},
},
{
Name: "get",
Aliases: []string{"g"},
Usage: "Get file info by ID",
ArgsUsage: "[ID of file]",
Action: func(ctx *cli.Context) error {
fileId := ctx.Args().First()
if fileId == "" {
return errors.New("no CID provided")
}
_, err := GetFile(fileId)
return err
},
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "Update a file by ID",
ArgsUsage: "[ID of file]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Update the name of a file",
},
},
Action: func(ctx *cli.Context) error {
fileId := ctx.Args().First()
name := ctx.String("name")
if fileId == "" {
return errors.New("no ID provided")
}
_, err := UpdateFile(fileId, name)
return err
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "List most recent files",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Filter by name of the target file",
},
&cli.StringFlag{
Name: "cid",
Aliases: []string{"c"},
Usage: "Filter results by CID",
},
&cli.StringFlag{
Name: "group",
Aliases: []string{"g"},
Usage: "Filter results by group ID",
},
&cli.StringFlag{
Name: "mime",
Aliases: []string{"m"},
Usage: "Filter results by file mime type",
},
&cli.StringFlag{
Name: "amount",
Aliases: []string{"a"},
Usage: "The number of files you would like to return",
},
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Usage: "Paginate through file results using the pageToken",
},
&cli.BoolFlag{
Name: "cidPending",
Value: false,
Usage: "Filter results based on whether or not the CID is pending",
},
&cli.StringSliceFlag{
Name: "keyvalues",
Aliases: []string{"kv"},
Usage: "Filter results by metadata keyvalues (format: key=value)",
},
},
Action: func(ctx *cli.Context) error {
amount := ctx.String("amount")
token := ctx.String("token")
name := ctx.String("name")
cid := ctx.String("cid")
group := ctx.String("group")
mime := ctx.String("mime")
cidPending := ctx.Bool("cidPending")
keyvaluesSlice := ctx.StringSlice("keyvalues")
keyvalues := make(map[string]string)
for _, kv := range keyvaluesSlice {
parts := strings.SplitN(kv, "=", 2)
if len(parts) == 2 {
keyvalues[parts[0]] = parts[1]
}
}
_, err := ListFiles(amount, token, cidPending, name, cid, group, mime, keyvalues)
return err
},
},
},
},
{
Name: "swaps",
Aliases: []string{"s"},
Usage: "Interact and manage hot swaps on Pinata",
Subcommands: []*cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "List swaps for a given gateway domain or for your config gateway domain",
ArgsUsage: "[cid] [optional gateway domain]",
Action: func(ctx *cli.Context) error {
cid := ctx.Args().First()
domain := ctx.Args().Get(1)
if cid == "" {
return errors.New("No CID provided")
}
_, err := GetSwapHistory(cid, domain)
return err
},
},
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add a swap for a CID",
ArgsUsage: "[cid] [swap cid]",
Action: func(ctx *cli.Context) error {
cid := ctx.Args().First()
swapCid := ctx.Args().Get(1)
if cid == "" {
return errors.New("No CID provided")
}
if swapCid == "" {
return errors.New("No swap CID provided")
}
_, err := AddSwap(cid, swapCid)
return err
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "Remeove a swap for a CID",
ArgsUsage: "[cid]",
Action: func(ctx *cli.Context) error {
cid := ctx.Args().First()
if cid == "" {
return errors.New("No CID provided")
}
err := RemoveSwap(cid)
return err
},
},
},
},
{
Name: "gateways",
Aliases: []string{"gw"},
Usage: "Interact with your gateways on Pinata",
Subcommands: []*cli.Command{
{
Name: "set",
Aliases: []string{"s"},
Usage: "Set your default gateway to be used by the CLI",
ArgsUsage: "[domain of the gateway]",
Action: func(ctx *cli.Context) error {
domain := ctx.Args().First()
err := SetGateway(domain)
return err
},
},
{
Name: "open",
Aliases: []string{"o"},
Usage: "Open a file in the browser",
ArgsUsage: "[CID of the file]",
Action: func(ctx *cli.Context) error {
cid := ctx.Args().First()
if cid == "" {
return errors.New("No CID provided")
}
err := OpenCID(cid)
return err
},
},
{
Name: "sign",
Aliases: []string{"s"},
Usage: "Get a signed URL for a file by CID",
ArgsUsage: "[cid of the file, seconds the url is valid for]",
Action: func(ctx *cli.Context) error {
cid := ctx.Args().First()
if cid == "" {
return errors.New("No CID provided")
}
expires := ctx.Args().Get(1)
if expires == "" {
expires = "30"
}
expiresInt, err := strconv.Atoi(expires)
if err != nil {
return errors.New("Invalid expire time")
}
_, err = GetSignedURL(cid, expiresInt)
return err
},
},
},
},
{
Name: "keys",
Aliases: []string{"k"},
Usage: "Create and manage generated API keys",
Subcommands: []*cli.Command{
{
Name: "create",
Aliases: []string{"c"},
Usage: "Create an API key with admin or scoped permissions",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Name of the API key",
Required: true,
},
&cli.BoolFlag{
Name: "admin",
Aliases: []string{"a"},
Usage: "Set the key as Admin",
Value: false,
},
&cli.IntFlag{
Name: "uses",
Aliases: []string{"u"},
Usage: "Max uses a key can use",
},
&cli.StringSliceFlag{
Name: "endpoints",
Aliases: []string{"e"},
Usage: "Optional array of endpoints the key is allowed to use",
},
},
Action: func(ctx *cli.Context) error {
name := ctx.String("name")
admin := ctx.Bool("admin")
uses := ctx.Int("uses")
endpoints := ctx.StringSlice("endpoints")
_, err := CreateKey(name, admin, uses, endpoints)
return err
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "List and filter API key",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Name of the API key",
},
&cli.BoolFlag{
Name: "revoked",
Aliases: []string{"r"},
Usage: "Set the key as Admin",
},
&cli.BoolFlag{
Name: "exhausted",
Aliases: []string{"e"},
Usage: "Filter keys that are exhausted or not",
},
&cli.BoolFlag{
Name: "uses",
Aliases: []string{"u"},
Usage: "Filter keys that do or don't have limited uses",
},
&cli.StringFlag{
Name: "offset",
Aliases: []string{"o"},
Usage: "Offset the number of results to paginate",
},
},
Action: func(ctx *cli.Context) error {
name := ctx.String("name")
offset := ctx.String("offset")
revoked := ctx.Bool("revoked")
uses := ctx.Bool("uses")
exhausted := ctx.Bool("exhausted")
_, err := ListKeys(name, revoked, uses, exhausted, offset)
return err
},
},
{
Name: "revoke",
Aliases: []string{"r"},
Usage: "Revoke an API key",
ArgsUsage: "[key]",
Action: func(ctx *cli.Context) error {
key := ctx.Args().First()
if key == "" {
return errors.New("No key provided")
}
err := RevokeKey(key)
return err
},
},
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}