Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit e062b33

Browse files
committed
remove irrelevant apis and minor refactor
Signed-off-by: Max Peng <[email protected]>
1 parent ec15ca9 commit e062b33

File tree

2 files changed

+48
-80
lines changed

2 files changed

+48
-80
lines changed

mfs.go

Lines changed: 47 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,16 @@ import (
77
files "github.com/ipfs/go-ipfs-files"
88
)
99

10+
type FilesOpt func(*RequestBuilder) error
11+
1012
type MfsLsEntry struct {
1113
Name string
1214
Type uint8
1315
Size uint64
1416
Hash string
1517
}
1618

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 {
2620
Blocks int
2721
CumulativeSize uint64
2822
Hash string
@@ -33,7 +27,14 @@ type filesStatOutput struct {
3327
WithLocality bool
3428
}
3529

36-
type filesOpt func(*RequestBuilder) error
30+
type filesLsOutput struct {
31+
Entries []*MfsLsEntry
32+
}
33+
34+
type filesFlushOutput struct {
35+
Cid string
36+
}
37+
3738
type filesLs struct{}
3839
type filesChcid struct{}
3940
type filesMkdir struct{}
@@ -50,176 +51,160 @@ var (
5051
FilesStat filesStat
5152
)
5253

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 {
5556
return func(rb *RequestBuilder) error {
5657
rb.Option("long", long)
5758
return nil
5859
}
5960
}
6061

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-
6962
// CidVersion cid version to use. (experimental)
70-
func (filesChcid) CidVersion(version int) filesOpt {
63+
func (filesChcid) CidVersion(version int) FilesOpt {
7164
return func(rb *RequestBuilder) error {
7265
rb.Option("cid-version", version)
7366
return nil
7467
}
7568
}
7669

7770
// 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 {
7972
return func(rb *RequestBuilder) error {
8073
rb.Option("hash", hash)
8174
return nil
8275
}
8376
}
8477

8578
// Parents no error if existing, make parent directories as needed
86-
func (filesMkdir) Parents(parents bool) filesOpt {
79+
func (filesMkdir) Parents(parents bool) FilesOpt {
8780
return func(rb *RequestBuilder) error {
8881
rb.Option("parents", parents)
8982
return nil
9083
}
9184
}
9285

9386
// CidVersion cid version to use. (experimental)
94-
func (filesMkdir) CidVersion(version int) filesOpt {
87+
func (filesMkdir) CidVersion(version int) FilesOpt {
9588
return func(rb *RequestBuilder) error {
9689
rb.Option("cid-version", version)
9790
return nil
9891
}
9992
}
10093

10194
// 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 {
10396
return func(rb *RequestBuilder) error {
10497
rb.Option("hash", hash)
10598
return nil
10699
}
107100
}
108101

109102
// Offset byte offset to begin reading from
110-
func (filesRead) Offset(offset int64) filesOpt {
103+
func (filesRead) Offset(offset int64) FilesOpt {
111104
return func(rb *RequestBuilder) error {
112105
rb.Option("offset", offset)
113106
return nil
114107
}
115108
}
116109

117110
// Count maximum number of bytes to read
118-
func (filesRead) Count(count int64) filesOpt {
111+
func (filesRead) Count(count int64) FilesOpt {
119112
return func(rb *RequestBuilder) error {
120113
rb.Option("count", count)
121114
return nil
122115
}
123116
}
124117

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-
133118
// 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 {
135120
return func(rb *RequestBuilder) error {
136121
rb.Option("hash", hash)
137122
return nil
138123
}
139124
}
140125

141126
// 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 {
143128
return func(rb *RequestBuilder) error {
144129
rb.Option("size", size)
145130
return nil
146131
}
147132
}
148133

149134
// 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 {
151136
return func(rb *RequestBuilder) error {
152137
rb.Option("with-local", withLocal)
153138
return nil
154139
}
155140
}
156141

157142
// Offset byte offset to begin writing at
158-
func (filesWrite) Offset(offset int64) filesOpt {
143+
func (filesWrite) Offset(offset int64) FilesOpt {
159144
return func(rb *RequestBuilder) error {
160145
rb.Option("offset", offset)
161146
return nil
162147
}
163148
}
164149

165150
// Create create the file if it does not exist
166-
func (filesWrite) Create(create bool) filesOpt {
151+
func (filesWrite) Create(create bool) FilesOpt {
167152
return func(rb *RequestBuilder) error {
168153
rb.Option("create", create)
169154
return nil
170155
}
171156
}
172157

173158
// Parents make parent directories as needed
174-
func (filesWrite) Parents(parents bool) filesOpt {
159+
func (filesWrite) Parents(parents bool) FilesOpt {
175160
return func(rb *RequestBuilder) error {
176161
rb.Option("parents", parents)
177162
return nil
178163
}
179164
}
180165

181166
// Truncate truncate the file to size zero before writing
182-
func (filesWrite) Truncate(truncate bool) filesOpt {
167+
func (filesWrite) Truncate(truncate bool) FilesOpt {
183168
return func(rb *RequestBuilder) error {
184169
rb.Option("truncate", truncate)
185170
return nil
186171
}
187172
}
188173

189174
// Count maximum number of bytes to write
190-
func (filesWrite) Count(count int64) filesOpt {
175+
func (filesWrite) Count(count int64) FilesOpt {
191176
return func(rb *RequestBuilder) error {
192177
rb.Option("count", count)
193178
return nil
194179
}
195180
}
196181

197182
// RawLeaves use raw blocks for newly created leaf nodes. (experimental)
198-
func (filesWrite) RawLeaves(rawLeaves bool) filesOpt {
183+
func (filesWrite) RawLeaves(rawLeaves bool) FilesOpt {
199184
return func(rb *RequestBuilder) error {
200185
rb.Option("raw-leaves", rawLeaves)
201186
return nil
202187
}
203188
}
204189

205190
// CidVersion cid version to use. (experimental)
206-
func (filesWrite) CidVersion(version int) filesOpt {
191+
func (filesWrite) CidVersion(version int) FilesOpt {
207192
return func(rb *RequestBuilder) error {
208193
rb.Option("cid-version", version)
209194
return nil
210195
}
211196
}
212197

213198
// 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 {
215200
return func(rb *RequestBuilder) error {
216201
rb.Option("hash", hash)
217202
return nil
218203
}
219204
}
220205

221206
// 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 {
223208
if len(path) == 0 {
224209
path = "/"
225210
}
@@ -231,15 +216,13 @@ func (s *Shell) FilesChcid(path string, options ...filesOpt) error {
231216
}
232217
}
233218

234-
resp, err := rb.Send(context.Background())
235-
return handleResponse(resp, err)
219+
return rb.Exec(context.Background(), nil)
236220
}
237221

238222
// FilesCp copy any IPFS files and directories into MFS (or copy within MFS)
239223
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)
243226
}
244227

245228
// FilesFlush flush a given path's data to disk
@@ -257,7 +240,7 @@ func (s *Shell) FilesFlush(path string) (string, error) {
257240
}
258241

259242
// 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) {
261244
if len(path) == 0 {
262245
path = "/"
263246
}
@@ -276,27 +259,25 @@ func (s *Shell) FilesLs(path string, options ...filesOpt) ([]*MfsLsEntry, error)
276259
}
277260

278261
// FilesMkdir make directories
279-
func (s *Shell) FilesMkdir(path string, options ...filesOpt) error {
262+
func (s *Shell) FilesMkdir(path string, options ...FilesOpt) error {
280263
rb := s.Request("files/mkdir", path)
281264
for _, opt := range options {
282265
if err := opt(rb); err != nil {
283266
return err
284267
}
285268
}
286269

287-
resp, err := rb.Send(context.Background())
288-
return handleResponse(resp, err)
270+
return rb.Exec(context.Background(), nil)
289271
}
290272

291273
// FilesMv move files
292274
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)
296277
}
297278

298279
// 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) {
300281
rb := s.Request("files/read", path)
301282
for _, opt := range options {
302283
if err := opt(rb); err != nil {
@@ -317,15 +298,14 @@ func (s *Shell) FilesRead(path string, options ...filesOpt) (io.ReadCloser, erro
317298

318299
// FilesRm remove a file
319300
func (s *Shell) FilesRm(path string, force bool) error {
320-
resp, err := s.Request("files/rm", path).
301+
return s.Request("files/rm", path).
321302
Option("force", force).
322-
Send(context.Background())
323-
return handleResponse(resp, err)
303+
Exec(context.Background(), nil)
324304
}
325305

326306
// 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{}
329309

330310
rb := s.Request("files/stat", path)
331311
for _, opt := range options {
@@ -342,7 +322,7 @@ func (s *Shell) FilesStat(path string, options ...filesOpt) (*filesStatOutput, e
342322
}
343323

344324
// 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 {
346326
fr := files.NewReaderFile(data)
347327
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", fr)})
348328
fileReader := files.NewMultiFileReader(slf, true)
@@ -354,17 +334,5 @@ func (s *Shell) FilesWrite(path string, data io.Reader, options ...filesOpt) err
354334
}
355335
}
356336

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)
370338
}

mfs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestFilesLs(t *testing.T) {
7878
is := is.New(t)
7979
s := NewShell(shellUrl)
8080

81-
list, err := s.FilesLs("/testdata", FilesLs.Long(true), FilesLs.U(true))
81+
list, err := s.FilesLs("/testdata", FilesLs.Stat(true))
8282
is.Nil(err)
8383

8484
is.Equal(len(list), 2)

0 commit comments

Comments
 (0)