-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
237 lines (225 loc) · 6.17 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
package main
import (
"errors"
"log"
"os"
"github.com/urfave/cli/v2"
)
type UploadResponse struct {
IpfsHash string `json:"IpfsHash"`
PinSize int `json:"PinSize"`
Timestamp string `json:"Timestamp"`
IsDuplicate bool `json:"isDuplicate"`
}
type PinByCIDResponse struct {
Id string `json:"id"`
CID string `json:"ipfsHash"`
Status string `json:"status"`
Name string `json:"name"`
}
type Options struct {
CidVersion int `json:"cidVersion"`
}
type Metadata struct {
Name string `json:"name"`
KeyValues map[string]interface{} `json:"keyvalues"`
}
type Pin struct {
Id string `json:"id"`
IPFSPinHash string `json:"ipfs_pin_hash"`
Size int `json:"size"`
UserId string `json:"user_id"`
DatePinned string `json:"date_pinned"`
DateUnpinned *string `json:"date_unpinned"`
Metadata Metadata `json:"metadata"`
MimeType string `json:"mime_type"`
NumberOfFiles int `json:"number_of_files"`
}
type ListResponse struct {
Rows []Pin `json:"rows"`
}
type Request struct {
Id string `json:"id"`
CID string `json:"ipfs_pin_hash"`
StartDate string `json:"date_queued"`
Name string `json:"name"`
Status string `json:"status"`
}
type RequestsResponse struct {
Rows []Request `json:"rows"`
}
func main() {
app := &cli.App{
Name: "pinata-web3",
Usage: "A CLI for uploading files to IPFS through 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 {
jwt := ctx.Args().First()
if jwt == "" {
return errors.New("no jwt supplied")
}
err := SaveJWT(jwt)
return err
},
},
{
Name: "upload",
Aliases: []string{"u"},
Usage: "Upload a file or folder to Pinata",
ArgsUsage: "[path to file]",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "version",
Aliases: []string{"v"},
Value: 1,
Usage: "Set desired CID version to either 0 or 1. Default is 1.",
},
&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: "cid-only",
Usage: "Use if you only want the CID returned after an upload",
},
},
Action: func(ctx *cli.Context) error {
filePath := ctx.Args().First()
version := ctx.Int("version")
name := ctx.String("name")
cidOnly := ctx.Bool("cid-only")
if filePath == "" {
return errors.New("no file path provided")
}
_, err := Upload(filePath, version, name, cidOnly)
return err
},
},
{
Name: "pin",
Aliases: []string{"p"},
Usage: "Pin an existing CID on IPFS to Pinata",
ArgsUsage: "[CID of file on IPFS]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Value: "null",
Usage: "Add a name for the file you are trying to pin.",
},
},
Action: func(ctx *cli.Context) error {
cid := ctx.Args().First()
name := ctx.String("name")
if cid == "" {
return errors.New("no cid provided")
}
_, err := PinByCID(cid, name)
return err
},
},
{
Name: "requests",
Aliases: []string{"r"},
Usage: "List pin by CID requests.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cid",
Aliases: []string{"c"},
Value: "null",
Usage: "Search pin by CID requests by CID",
},
&cli.StringFlag{
Name: "status",
Aliases: []string{"s"},
Value: "null",
Usage: "Search by status for pin by CID requests. See https://docs.pinata.cloud/reference/get_pinning-pinjobs for more info.",
},
&cli.StringFlag{
Name: "pageOffset",
Aliases: []string{"p"},
Value: "null",
Usage: "Allows you to paginate through requests by number of requests.",
},
},
Action: func(ctx *cli.Context) error {
cid := ctx.String("cid")
status := ctx.String("status")
offset := ctx.String("pageOffset")
_, err := Requests(cid, status, offset)
return err
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "Delete a file by CID",
ArgsUsage: "[CID of file]",
Action: func(ctx *cli.Context) error {
cid := ctx.Args().First()
if cid == "" {
return errors.New("no CID provided")
}
err := Delete(cid)
return err
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "List most recent files",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cid",
Aliases: []string{"c"},
Value: "null",
Usage: "Search files by CID",
},
&cli.StringFlag{
Name: "amount",
Aliases: []string{"a"},
Value: "10",
Usage: "The number of files you would like to return, default 10 max 1000",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Value: "null",
Usage: "The name of the file",
},
&cli.StringFlag{
Name: "status",
Aliases: []string{"s"},
Value: "pinned",
Usage: "Status of the file. Options are 'pinned', 'unpinned', or 'all'. Default: 'pinned'",
},
&cli.StringFlag{
Name: "pageOffset",
Aliases: []string{"p"},
Value: "null",
Usage: "Allows you to paginate through files. If your file amount is 10, then you could set the pageOffset to '10' to see the next 10 files.",
},
},
Action: func(ctx *cli.Context) error {
cid := ctx.String("cid")
amount := ctx.String("amount")
name := ctx.String("name")
status := ctx.String("status")
offset := ctx.String("pageOffset")
_, err := ListFiles(amount, cid, name, status, offset)
return err
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}