@@ -7,22 +7,16 @@ import (
7
7
files "github.com/ipfs/go-ipfs-files"
8
8
)
9
9
10
+ type FilesOpt func (* RequestBuilder ) error
11
+
10
12
type MfsLsEntry struct {
11
13
Name string
12
14
Type uint8
13
15
Size uint64
14
16
Hash string
15
17
}
16
18
17
- type filesLsOutput struct {
18
- Entries []* MfsLsEntry
19
- }
20
-
21
- type filesFlushOutput struct {
22
- Cid string
23
- }
24
-
25
- type filesStatOutput struct {
19
+ type FilesStatObject struct {
26
20
Blocks int
27
21
CumulativeSize uint64
28
22
Hash string
@@ -33,7 +27,14 @@ type filesStatOutput struct {
33
27
WithLocality bool
34
28
}
35
29
36
- type filesOpt func (* RequestBuilder ) error
30
+ type filesLsOutput struct {
31
+ Entries []* MfsLsEntry
32
+ }
33
+
34
+ type filesFlushOutput struct {
35
+ Cid string
36
+ }
37
+
37
38
type filesLs struct {}
38
39
type filesChcid struct {}
39
40
type filesMkdir struct {}
@@ -50,176 +51,160 @@ var (
50
51
FilesStat filesStat
51
52
)
52
53
53
- // Long use long listing format
54
- func (filesLs ) Long (long bool ) filesOpt {
54
+ // Stat use long listing format
55
+ func (filesLs ) Stat (long bool ) FilesOpt {
55
56
return func (rb * RequestBuilder ) error {
56
57
rb .Option ("long" , long )
57
58
return nil
58
59
}
59
60
}
60
61
61
- // U do not sort; list entries in directory order
62
- func (filesLs ) U (u bool ) filesOpt {
63
- return func (rb * RequestBuilder ) error {
64
- rb .Option ("U" , u )
65
- return nil
66
- }
67
- }
68
-
69
62
// CidVersion cid version to use. (experimental)
70
- func (filesChcid ) CidVersion (version int ) filesOpt {
63
+ func (filesChcid ) CidVersion (version int ) FilesOpt {
71
64
return func (rb * RequestBuilder ) error {
72
65
rb .Option ("cid-version" , version )
73
66
return nil
74
67
}
75
68
}
76
69
77
70
// Hash hash function to use. Will set Cid version to 1 if used. (experimental)
78
- func (filesChcid ) Hash (hash string ) filesOpt {
71
+ func (filesChcid ) Hash (hash string ) FilesOpt {
79
72
return func (rb * RequestBuilder ) error {
80
73
rb .Option ("hash" , hash )
81
74
return nil
82
75
}
83
76
}
84
77
85
78
// Parents no error if existing, make parent directories as needed
86
- func (filesMkdir ) Parents (parents bool ) filesOpt {
79
+ func (filesMkdir ) Parents (parents bool ) FilesOpt {
87
80
return func (rb * RequestBuilder ) error {
88
81
rb .Option ("parents" , parents )
89
82
return nil
90
83
}
91
84
}
92
85
93
86
// CidVersion cid version to use. (experimental)
94
- func (filesMkdir ) CidVersion (version int ) filesOpt {
87
+ func (filesMkdir ) CidVersion (version int ) FilesOpt {
95
88
return func (rb * RequestBuilder ) error {
96
89
rb .Option ("cid-version" , version )
97
90
return nil
98
91
}
99
92
}
100
93
101
94
// Hash hash function to use. Will set Cid version to 1 if used. (experimental)
102
- func (filesMkdir ) Hash (hash string ) filesOpt {
95
+ func (filesMkdir ) Hash (hash string ) FilesOpt {
103
96
return func (rb * RequestBuilder ) error {
104
97
rb .Option ("hash" , hash )
105
98
return nil
106
99
}
107
100
}
108
101
109
102
// Offset byte offset to begin reading from
110
- func (filesRead ) Offset (offset int64 ) filesOpt {
103
+ func (filesRead ) Offset (offset int64 ) FilesOpt {
111
104
return func (rb * RequestBuilder ) error {
112
105
rb .Option ("offset" , offset )
113
106
return nil
114
107
}
115
108
}
116
109
117
110
// Count maximum number of bytes to read
118
- func (filesRead ) Count (count int64 ) filesOpt {
111
+ func (filesRead ) Count (count int64 ) FilesOpt {
119
112
return func (rb * RequestBuilder ) error {
120
113
rb .Option ("count" , count )
121
114
return nil
122
115
}
123
116
}
124
117
125
- // Format print statistics in given format. Allowed tokens: <hash> <size> <cumulsize> <type> <childs>. Conflicts with other format options.
126
- func (filesStat ) Format (format string ) filesOpt {
127
- return func (rb * RequestBuilder ) error {
128
- rb .Option ("format" , format )
129
- return nil
130
- }
131
- }
132
-
133
118
// Hash print only hash. Implies '--format=<hash>'. Conflicts with other format options.
134
- func (filesStat ) Hash (hash bool ) filesOpt {
119
+ func (filesStat ) Hash (hash bool ) FilesOpt {
135
120
return func (rb * RequestBuilder ) error {
136
121
rb .Option ("hash" , hash )
137
122
return nil
138
123
}
139
124
}
140
125
141
126
// Size print only size. Implies '--format=<cumulsize>'. Conflicts with other format options.
142
- func (filesStat ) Size (size bool ) filesOpt {
127
+ func (filesStat ) Size (size bool ) FilesOpt {
143
128
return func (rb * RequestBuilder ) error {
144
129
rb .Option ("size" , size )
145
130
return nil
146
131
}
147
132
}
148
133
149
134
// WithLocal compute the amount of the dag that is local, and if possible the total size.
150
- func (filesStat ) WithLocal (withLocal bool ) filesOpt {
135
+ func (filesStat ) WithLocal (withLocal bool ) FilesOpt {
151
136
return func (rb * RequestBuilder ) error {
152
137
rb .Option ("with-local" , withLocal )
153
138
return nil
154
139
}
155
140
}
156
141
157
142
// Offset byte offset to begin writing at
158
- func (filesWrite ) Offset (offset int64 ) filesOpt {
143
+ func (filesWrite ) Offset (offset int64 ) FilesOpt {
159
144
return func (rb * RequestBuilder ) error {
160
145
rb .Option ("offset" , offset )
161
146
return nil
162
147
}
163
148
}
164
149
165
150
// Create create the file if it does not exist
166
- func (filesWrite ) Create (create bool ) filesOpt {
151
+ func (filesWrite ) Create (create bool ) FilesOpt {
167
152
return func (rb * RequestBuilder ) error {
168
153
rb .Option ("create" , create )
169
154
return nil
170
155
}
171
156
}
172
157
173
158
// Parents make parent directories as needed
174
- func (filesWrite ) Parents (parents bool ) filesOpt {
159
+ func (filesWrite ) Parents (parents bool ) FilesOpt {
175
160
return func (rb * RequestBuilder ) error {
176
161
rb .Option ("parents" , parents )
177
162
return nil
178
163
}
179
164
}
180
165
181
166
// Truncate truncate the file to size zero before writing
182
- func (filesWrite ) Truncate (truncate bool ) filesOpt {
167
+ func (filesWrite ) Truncate (truncate bool ) FilesOpt {
183
168
return func (rb * RequestBuilder ) error {
184
169
rb .Option ("truncate" , truncate )
185
170
return nil
186
171
}
187
172
}
188
173
189
174
// Count maximum number of bytes to write
190
- func (filesWrite ) Count (count int64 ) filesOpt {
175
+ func (filesWrite ) Count (count int64 ) FilesOpt {
191
176
return func (rb * RequestBuilder ) error {
192
177
rb .Option ("count" , count )
193
178
return nil
194
179
}
195
180
}
196
181
197
182
// RawLeaves use raw blocks for newly created leaf nodes. (experimental)
198
- func (filesWrite ) RawLeaves (rawLeaves bool ) filesOpt {
183
+ func (filesWrite ) RawLeaves (rawLeaves bool ) FilesOpt {
199
184
return func (rb * RequestBuilder ) error {
200
185
rb .Option ("raw-leaves" , rawLeaves )
201
186
return nil
202
187
}
203
188
}
204
189
205
190
// CidVersion cid version to use. (experimental)
206
- func (filesWrite ) CidVersion (version int ) filesOpt {
191
+ func (filesWrite ) CidVersion (version int ) FilesOpt {
207
192
return func (rb * RequestBuilder ) error {
208
193
rb .Option ("cid-version" , version )
209
194
return nil
210
195
}
211
196
}
212
197
213
198
// Hash hash function to use. Will set Cid version to 1 if used. (experimental)
214
- func (filesWrite ) Hash (hash string ) filesOpt {
199
+ func (filesWrite ) Hash (hash string ) FilesOpt {
215
200
return func (rb * RequestBuilder ) error {
216
201
rb .Option ("hash" , hash )
217
202
return nil
218
203
}
219
204
}
220
205
221
206
// FilesChcid change the cid version or hash function of the root node of a given path
222
- func (s * Shell ) FilesChcid (path string , options ... filesOpt ) error {
207
+ func (s * Shell ) FilesChcid (path string , options ... FilesOpt ) error {
223
208
if len (path ) == 0 {
224
209
path = "/"
225
210
}
@@ -231,15 +216,13 @@ func (s *Shell) FilesChcid(path string, options ...filesOpt) error {
231
216
}
232
217
}
233
218
234
- resp , err := rb .Send (context .Background ())
235
- return handleResponse (resp , err )
219
+ return rb .Exec (context .Background (), nil )
236
220
}
237
221
238
222
// FilesCp copy any IPFS files and directories into MFS (or copy within MFS)
239
223
func (s * Shell ) FilesCp (src string , dest string ) error {
240
- resp , err := s .Request ("files/cp" , src , dest ).
241
- Send (context .Background ())
242
- return handleResponse (resp , err )
224
+ return s .Request ("files/cp" , src , dest ).
225
+ Exec (context .Background (), nil )
243
226
}
244
227
245
228
// FilesFlush flush a given path's data to disk
@@ -257,7 +240,7 @@ func (s *Shell) FilesFlush(path string) (string, error) {
257
240
}
258
241
259
242
// FilesLs list directories in the local mutable namespace
260
- func (s * Shell ) FilesLs (path string , options ... filesOpt ) ([]* MfsLsEntry , error ) {
243
+ func (s * Shell ) FilesLs (path string , options ... FilesOpt ) ([]* MfsLsEntry , error ) {
261
244
if len (path ) == 0 {
262
245
path = "/"
263
246
}
@@ -276,27 +259,25 @@ func (s *Shell) FilesLs(path string, options ...filesOpt) ([]*MfsLsEntry, error)
276
259
}
277
260
278
261
// FilesMkdir make directories
279
- func (s * Shell ) FilesMkdir (path string , options ... filesOpt ) error {
262
+ func (s * Shell ) FilesMkdir (path string , options ... FilesOpt ) error {
280
263
rb := s .Request ("files/mkdir" , path )
281
264
for _ , opt := range options {
282
265
if err := opt (rb ); err != nil {
283
266
return err
284
267
}
285
268
}
286
269
287
- resp , err := rb .Send (context .Background ())
288
- return handleResponse (resp , err )
270
+ return rb .Exec (context .Background (), nil )
289
271
}
290
272
291
273
// FilesMv move files
292
274
func (s * Shell ) FilesMv (src string , dest string ) error {
293
- resp , err := s .Request ("files/mv" , src , dest ).
294
- Send (context .Background ())
295
- return handleResponse (resp , err )
275
+ return s .Request ("files/mv" , src , dest ).
276
+ Exec (context .Background (), nil )
296
277
}
297
278
298
279
// FilesRead read a file in a given MFS
299
- func (s * Shell ) FilesRead (path string , options ... filesOpt ) (io.ReadCloser , error ) {
280
+ func (s * Shell ) FilesRead (path string , options ... FilesOpt ) (io.ReadCloser , error ) {
300
281
rb := s .Request ("files/read" , path )
301
282
for _ , opt := range options {
302
283
if err := opt (rb ); err != nil {
@@ -317,15 +298,14 @@ func (s *Shell) FilesRead(path string, options ...filesOpt) (io.ReadCloser, erro
317
298
318
299
// FilesRm remove a file
319
300
func (s * Shell ) FilesRm (path string , force bool ) error {
320
- resp , err := s .Request ("files/rm" , path ).
301
+ return s .Request ("files/rm" , path ).
321
302
Option ("force" , force ).
322
- Send (context .Background ())
323
- return handleResponse (resp , err )
303
+ Exec (context .Background (), nil )
324
304
}
325
305
326
306
// FilesStat display file status
327
- func (s * Shell ) FilesStat (path string , options ... filesOpt ) (* filesStatOutput , error ) {
328
- out := & filesStatOutput {}
307
+ func (s * Shell ) FilesStat (path string , options ... FilesOpt ) (* FilesStatObject , error ) {
308
+ out := & FilesStatObject {}
329
309
330
310
rb := s .Request ("files/stat" , path )
331
311
for _ , opt := range options {
@@ -342,7 +322,7 @@ func (s *Shell) FilesStat(path string, options ...filesOpt) (*filesStatOutput, e
342
322
}
343
323
344
324
// FilesWrite write to a mutable file in a given filesystem
345
- func (s * Shell ) FilesWrite (path string , data io.Reader , options ... filesOpt ) error {
325
+ func (s * Shell ) FilesWrite (path string , data io.Reader , options ... FilesOpt ) error {
346
326
fr := files .NewReaderFile (data )
347
327
slf := files .NewSliceDirectory ([]files.DirEntry {files .FileEntry ("" , fr )})
348
328
fileReader := files .NewMultiFileReader (slf , true )
@@ -354,17 +334,5 @@ func (s *Shell) FilesWrite(path string, data io.Reader, options ...filesOpt) err
354
334
}
355
335
}
356
336
357
- resp , err := rb .Body (fileReader ).Send (context .Background ())
358
- return handleResponse (resp , err )
359
- }
360
-
361
- func handleResponse (resp * Response , err error ) error {
362
- if err != nil {
363
- return err
364
- }
365
- if resp .Error != nil {
366
- return resp .Error
367
- }
368
-
369
- return nil
337
+ return rb .Body (fileReader ).Exec (context .Background (), nil )
370
338
}
0 commit comments