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

Commit f35b54f

Browse files
committed
extract context in parameter
Signed-off-by: Max Peng <[email protected]>
1 parent e062b33 commit f35b54f

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
lines changed

mfs.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (filesWrite) Hash(hash string) FilesOpt {
204204
}
205205

206206
// FilesChcid change the cid version or hash function of the root node of a given path
207-
func (s *Shell) FilesChcid(path string, options ...FilesOpt) error {
207+
func (s *Shell) FilesChcid(ctx context.Context, path string, options ...FilesOpt) error {
208208
if len(path) == 0 {
209209
path = "/"
210210
}
@@ -216,31 +216,30 @@ func (s *Shell) FilesChcid(path string, options ...FilesOpt) error {
216216
}
217217
}
218218

219-
return rb.Exec(context.Background(), nil)
219+
return rb.Exec(ctx, nil)
220220
}
221221

222222
// FilesCp copy any IPFS files and directories into MFS (or copy within MFS)
223-
func (s *Shell) FilesCp(src string, dest string) error {
224-
return s.Request("files/cp", src, dest).
225-
Exec(context.Background(), nil)
223+
func (s *Shell) FilesCp(ctx context.Context, src string, dest string) error {
224+
return s.Request("files/cp", src, dest).Exec(ctx, nil)
226225
}
227226

228227
// FilesFlush flush a given path's data to disk
229-
func (s *Shell) FilesFlush(path string) (string, error) {
228+
func (s *Shell) FilesFlush(ctx context.Context, path string) (string, error) {
230229
if len(path) == 0 {
231230
path = "/"
232231
}
233232
out := &filesFlushOutput{}
234233
if err := s.Request("files/flush", path).
235-
Exec(context.Background(), out); err != nil {
234+
Exec(ctx, out); err != nil {
236235
return "", err
237236
}
238237

239238
return out.Cid, nil
240239
}
241240

242241
// FilesLs list directories in the local mutable namespace
243-
func (s *Shell) FilesLs(path string, options ...FilesOpt) ([]*MfsLsEntry, error) {
242+
func (s *Shell) FilesLs(ctx context.Context, path string, options ...FilesOpt) ([]*MfsLsEntry, error) {
244243
if len(path) == 0 {
245244
path = "/"
246245
}
@@ -252,40 +251,39 @@ func (s *Shell) FilesLs(path string, options ...FilesOpt) ([]*MfsLsEntry, error)
252251
return nil, err
253252
}
254253
}
255-
if err := rb.Exec(context.Background(), &out); err != nil {
254+
if err := rb.Exec(ctx, &out); err != nil {
256255
return nil, err
257256
}
258257
return out.Entries, nil
259258
}
260259

261260
// FilesMkdir make directories
262-
func (s *Shell) FilesMkdir(path string, options ...FilesOpt) error {
261+
func (s *Shell) FilesMkdir(ctx context.Context, path string, options ...FilesOpt) error {
263262
rb := s.Request("files/mkdir", path)
264263
for _, opt := range options {
265264
if err := opt(rb); err != nil {
266265
return err
267266
}
268267
}
269268

270-
return rb.Exec(context.Background(), nil)
269+
return rb.Exec(ctx, nil)
271270
}
272271

273272
// FilesMv move files
274-
func (s *Shell) FilesMv(src string, dest string) error {
275-
return s.Request("files/mv", src, dest).
276-
Exec(context.Background(), nil)
273+
func (s *Shell) FilesMv(ctx context.Context, src string, dest string) error {
274+
return s.Request("files/mv", src, dest).Exec(ctx, nil)
277275
}
278276

279277
// FilesRead read a file in a given MFS
280-
func (s *Shell) FilesRead(path string, options ...FilesOpt) (io.ReadCloser, error) {
278+
func (s *Shell) FilesRead(ctx context.Context, path string, options ...FilesOpt) (io.ReadCloser, error) {
281279
rb := s.Request("files/read", path)
282280
for _, opt := range options {
283281
if err := opt(rb); err != nil {
284282
return nil, err
285283
}
286284
}
287285

288-
resp, err := rb.Send(context.Background())
286+
resp, err := rb.Send(ctx)
289287
if err != nil {
290288
return nil, err
291289
}
@@ -297,14 +295,14 @@ func (s *Shell) FilesRead(path string, options ...FilesOpt) (io.ReadCloser, erro
297295
}
298296

299297
// FilesRm remove a file
300-
func (s *Shell) FilesRm(path string, force bool) error {
298+
func (s *Shell) FilesRm(ctx context.Context, path string, force bool) error {
301299
return s.Request("files/rm", path).
302300
Option("force", force).
303-
Exec(context.Background(), nil)
301+
Exec(ctx, nil)
304302
}
305303

306304
// FilesStat display file status
307-
func (s *Shell) FilesStat(path string, options ...FilesOpt) (*FilesStatObject, error) {
305+
func (s *Shell) FilesStat(ctx context.Context, path string, options ...FilesOpt) (*FilesStatObject, error) {
308306
out := &FilesStatObject{}
309307

310308
rb := s.Request("files/stat", path)
@@ -314,15 +312,14 @@ func (s *Shell) FilesStat(path string, options ...FilesOpt) (*FilesStatObject, e
314312
}
315313
}
316314

317-
if err := rb.Exec(context.Background(), out); err != nil {
315+
if err := rb.Exec(ctx, out); err != nil {
318316
return nil, err
319317
}
320-
321318
return out, nil
322319
}
323320

324321
// FilesWrite write to a mutable file in a given filesystem
325-
func (s *Shell) FilesWrite(path string, data io.Reader, options ...FilesOpt) error {
322+
func (s *Shell) FilesWrite(ctx context.Context, path string, data io.Reader, options ...FilesOpt) error {
326323
fr := files.NewReaderFile(data)
327324
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", fr)})
328325
fileReader := files.NewMultiFileReader(slf, true)
@@ -334,5 +331,5 @@ func (s *Shell) FilesWrite(path string, data io.Reader, options ...FilesOpt) err
334331
}
335332
}
336333

337-
return rb.Body(fileReader).Exec(context.Background(), nil)
334+
return rb.Body(fileReader).Exec(ctx, nil)
338335
}

mfs_test.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package shell
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
6-
"github.com/cheekybits/is"
77
"io/ioutil"
88
"os"
99
"testing"
10+
11+
"github.com/cheekybits/is"
1012
)
1113

1214
func TestMain(m *testing.M) {
@@ -18,14 +20,14 @@ func TestMain(m *testing.M) {
1820
os.Exit(1)
1921
}
2022

21-
err = s.FilesWrite(fmt.Sprintf("/testdata/%s", f), file, FilesWrite.Parents(true), FilesWrite.Create(true))
23+
err = s.FilesWrite(context.Background(), fmt.Sprintf("/testdata/%s", f), file, FilesWrite.Parents(true), FilesWrite.Create(true))
2224
if err != nil {
2325
os.Exit(1)
2426
}
2527
}
2628

2729
exitVal := m.Run()
28-
if err := s.FilesRm("/testdata", true); err != nil {
30+
if err := s.FilesRm(context.Background(), "/testdata", true); err != nil {
2931
os.Exit(1)
3032
}
3133
os.Exit(exitVal)
@@ -35,17 +37,17 @@ func TestFilesChcid(t *testing.T) {
3537
is := is.New(t)
3638
s := NewShell(shellUrl)
3739

38-
err := s.FilesChcid("/testdata", FilesChcid.Hash("sha3-256"))
40+
err := s.FilesChcid(context.Background(), "/testdata", FilesChcid.Hash("sha3-256"))
3941
is.Nil(err)
4042

41-
stat, err := s.FilesStat("/testdata")
43+
stat, err := s.FilesStat(context.Background(), "/testdata")
4244
is.Nil(err)
4345
is.Equal(stat.Hash, "bafybmigo44bvq5f4u2oswr7cilvlilftjekr4iilwxuxjj326hchztmk2m")
4446

45-
err = s.FilesChcid("/testdata", FilesChcid.CidVersion(0))
47+
err = s.FilesChcid(context.Background(), "/testdata", FilesChcid.CidVersion(0))
4648
is.Nil(err)
4749

48-
stat, err = s.FilesStat("/testdata")
50+
stat, err = s.FilesStat(context.Background(), "/testdata")
4951
is.Nil(err)
5052
is.Equal(stat.Hash, "QmfZtacPc5nch976ZsiBw6nhLmTzy5JjW2pzZg8j7GjqWq")
5153
}
@@ -54,22 +56,22 @@ func TestFilesCp(t *testing.T) {
5456
is := is.New(t)
5557
s := NewShell(shellUrl)
5658

57-
err := s.FilesCp("/testdata/readme", "/testdata/readme2")
59+
err := s.FilesCp(context.Background(), "/testdata/readme", "/testdata/readme2")
5860
is.Nil(err)
5961

60-
stat, err := s.FilesStat("/testdata/readme2")
62+
stat, err := s.FilesStat(context.Background(), "/testdata/readme2")
6163
is.Nil(err)
6264
is.Equal(stat.Hash, "QmfZt7xPekp7npSM6DHDUnFseAiNZQs7wq6muH9o99RsCB")
6365

64-
err = s.FilesRm("/testdata/readme2", true)
66+
err = s.FilesRm(context.Background(), "/testdata/readme2", true)
6567
is.Nil(err)
6668
}
6769

6870
func TestFilesFlush(t *testing.T) {
6971
is := is.New(t)
7072
s := NewShell(shellUrl)
7173

72-
cid, err := s.FilesFlush("/testdata")
74+
cid, err := s.FilesFlush(context.Background(), "/testdata")
7375
is.Nil(err)
7476
is.Equal(cid, "QmfZtacPc5nch976ZsiBw6nhLmTzy5JjW2pzZg8j7GjqWq")
7577
}
@@ -78,7 +80,7 @@ func TestFilesLs(t *testing.T) {
7880
is := is.New(t)
7981
s := NewShell(shellUrl)
8082

81-
list, err := s.FilesLs("/testdata", FilesLs.Stat(true))
83+
list, err := s.FilesLs(context.Background(), "/testdata", FilesLs.Stat(true))
8284
is.Nil(err)
8385

8486
is.Equal(len(list), 2)
@@ -92,39 +94,39 @@ func TestFilesMkdir(t *testing.T) {
9294
is := is.New(t)
9395
s := NewShell(shellUrl)
9496

95-
err := s.FilesMkdir("/testdata/dir1/dir2", FilesMkdir.Parents(true), FilesMkdir.CidVersion(1), FilesMkdir.Hash("sha3-256"))
97+
err := s.FilesMkdir(context.Background(), "/testdata/dir1/dir2", FilesMkdir.Parents(true), FilesMkdir.CidVersion(1), FilesMkdir.Hash("sha3-256"))
9698
is.Nil(err)
9799

98-
err = s.FilesMkdir("/testdata/dir3/dir4")
100+
err = s.FilesMkdir(context.Background(), "/testdata/dir3/dir4")
99101
is.NotNil(err)
100102

101-
err = s.FilesRm("/testdata/dir1", true)
103+
err = s.FilesRm(context.Background(), "/testdata/dir1", true)
102104
is.Nil(err)
103105
}
104106

105107
func TestFilesMv(t *testing.T) {
106108
is := is.New(t)
107109
s := NewShell(shellUrl)
108110

109-
err := s.FilesMv("/testdata/readme", "/testdata/readme2")
111+
err := s.FilesMv(context.Background(), "/testdata/readme", "/testdata/readme2")
110112
is.Nil(err)
111113

112-
stat, err := s.FilesStat("/testdata/readme2")
114+
stat, err := s.FilesStat(context.Background(), "/testdata/readme2")
113115
is.Nil(err)
114116
is.Equal(stat.Hash, "QmfZt7xPekp7npSM6DHDUnFseAiNZQs7wq6muH9o99RsCB")
115117

116-
stat, err = s.FilesStat("/testdata/readme")
118+
stat, err = s.FilesStat(context.Background(), "/testdata/readme")
117119
is.NotNil(err)
118120

119-
err = s.FilesMv("/testdata/readme2", "/testdata/readme")
121+
err = s.FilesMv(context.Background(), "/testdata/readme2", "/testdata/readme")
120122
is.Nil(err)
121123
}
122124

123125
func TestFilesRead(t *testing.T) {
124126
is := is.New(t)
125127
s := NewShell(shellUrl)
126128

127-
reader, err := s.FilesRead("/testdata/readme", FilesRead.Offset(0), FilesRead.Count(5))
129+
reader, err := s.FilesRead(context.Background(), "/testdata/readme", FilesRead.Offset(0), FilesRead.Count(5))
128130
is.Nil(err)
129131

130132
resBytes, err := ioutil.ReadAll(reader)
@@ -137,27 +139,27 @@ func TestFilesRm(t *testing.T) {
137139
s := NewShell(shellUrl)
138140

139141
file, _ := ioutil.ReadFile("./testdata/ping")
140-
err := s.FilesWrite("/testdata/dir1/ping", bytes.NewBuffer(file), FilesWrite.Parents(true), FilesWrite.Create(true))
142+
err := s.FilesWrite(context.Background(), "/testdata/dir1/ping", bytes.NewBuffer(file), FilesWrite.Parents(true), FilesWrite.Create(true))
141143
is.Nil(err)
142144

143-
err = s.FilesRm("/testdata/dir1", false)
145+
err = s.FilesRm(context.Background(), "/testdata/dir1", false)
144146
is.NotNil(err)
145147

146-
err = s.FilesRm("/testdata/dir1", true)
148+
err = s.FilesRm(context.Background(), "/testdata/dir1", true)
147149
is.Nil(err)
148150
}
149151

150152
func TestFilesStat(t *testing.T) {
151153
is := is.New(t)
152154
s := NewShell(shellUrl)
153155

154-
res, err := s.FilesStat("/testdata")
156+
res, err := s.FilesStat(context.Background(), "/testdata")
155157
is.Nil(err)
156158
is.Equal(res.Hash, "QmfZtacPc5nch976ZsiBw6nhLmTzy5JjW2pzZg8j7GjqWq")
157159
is.Equal(res.Size, 0)
158160
is.Equal(res.Type, "directory")
159161

160-
res, err = s.FilesStat("/testdata", FilesStat.WithLocal(true))
162+
res, err = s.FilesStat(context.Background(), "/testdata", FilesStat.WithLocal(true))
161163
is.Nil(err)
162164
is.Equal(res.WithLocality, true)
163165
is.Equal(res.Local, true)
@@ -171,21 +173,21 @@ func TestFilesWrite(t *testing.T) {
171173
file, err := ioutil.ReadFile("./testdata/ping")
172174
is.Nil(err)
173175

174-
err = s.FilesWrite("/testdata/ping", bytes.NewBuffer(file), FilesWrite.Create(true), FilesWrite.RawLeaves(true), FilesWrite.CidVersion(1), FilesWrite.Hash("sha3-256"))
176+
err = s.FilesWrite(context.Background(), "/testdata/ping", bytes.NewBuffer(file), FilesWrite.Create(true), FilesWrite.RawLeaves(true), FilesWrite.CidVersion(1), FilesWrite.Hash("sha3-256"))
175177
is.Nil(err)
176178

177-
reader, err := s.FilesRead("/testdata/ping")
179+
reader, err := s.FilesRead(context.Background(), "/testdata/ping")
178180
is.Nil(err)
179181

180182
resBytes, err := ioutil.ReadAll(reader)
181183
is.Nil(err)
182184
is.Equal(string(resBytes), "ipfs")
183185

184186
file, err = ioutil.ReadFile("./testdata/ping")
185-
err = s.FilesWrite("/testdata/ping", bytes.NewBuffer(file), FilesWrite.Offset(0), FilesWrite.Count(2), FilesWrite.Truncate(true))
187+
err = s.FilesWrite(context.Background(), "/testdata/ping", bytes.NewBuffer(file), FilesWrite.Offset(0), FilesWrite.Count(2), FilesWrite.Truncate(true))
186188
is.Nil(err)
187189

188-
reader, err = s.FilesRead("/testdata/ping")
190+
reader, err = s.FilesRead(context.Background(), "/testdata/ping")
189191
is.Nil(err)
190192

191193
resBytes, err = ioutil.ReadAll(reader)

0 commit comments

Comments
 (0)