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

Commit 08fd2b6

Browse files
vkosthacdias
andauthored
feat: add missing "parents" option to FilesCp command (#274)
Signed-off-by: vkost <[email protected]> Co-authored-by: Henrique Dias <[email protected]>
1 parent c356d0d commit 08fd2b6

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

mfs.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type filesMkdir struct{}
4141
type filesRead struct{}
4242
type filesWrite struct{}
4343
type filesStat struct{}
44+
type filesCp struct{}
4445

4546
var (
4647
FilesLs filesLs
@@ -49,6 +50,7 @@ var (
4950
FilesRead filesRead
5051
FilesWrite filesWrite
5152
FilesStat filesStat
53+
FilesCp filesCp
5254
)
5355

5456
// Stat use long listing format
@@ -203,6 +205,14 @@ func (filesWrite) Hash(hash string) FilesOpt {
203205
}
204206
}
205207

208+
// Parents make parent directories as needed
209+
func (filesCp) Parents(parents bool) FilesOpt {
210+
return func(rb *RequestBuilder) error {
211+
rb.Option("parents", parents)
212+
return nil
213+
}
214+
}
215+
206216
// FilesChcid change the cid version or hash function of the root node of a given path
207217
func (s *Shell) FilesChcid(ctx context.Context, path string, options ...FilesOpt) error {
208218
if len(path) == 0 {
@@ -220,8 +230,14 @@ func (s *Shell) FilesChcid(ctx context.Context, path string, options ...FilesOpt
220230
}
221231

222232
// FilesCp copy any IPFS files and directories into MFS (or copy within MFS)
223-
func (s *Shell) FilesCp(ctx context.Context, src string, dest string) error {
224-
return s.Request("files/cp", src, dest).Exec(ctx, nil)
233+
func (s *Shell) FilesCp(ctx context.Context, src string, dest string, options ...FilesOpt) error {
234+
rb := s.Request("files/cp", src, dest)
235+
for _, opt := range options {
236+
if err := opt(rb); err != nil {
237+
return err
238+
}
239+
}
240+
return rb.Exec(ctx, nil)
225241
}
226242

227243
// FilesFlush flush a given path's data to disk

mfs_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ func TestFilesCp(t *testing.T) {
8282
is.Nil(err)
8383
}
8484

85+
func TestFilesCParents(t *testing.T) {
86+
is := is.New(t)
87+
s := NewShell(shellUrl)
88+
89+
err := s.FilesCp(context.Background(), "/testdata/readme", "/dirs/should/be/created/readme", FilesCp.Parents(true))
90+
is.Nil(err)
91+
92+
stat, err := s.FilesStat(context.Background(), "/dirs/should/be/created/readme")
93+
is.Nil(err)
94+
is.Equal(stat.Hash, "QmfZt7xPekp7npSM6DHDUnFseAiNZQs7wq6muH9o99RsCB")
95+
96+
err = s.FilesRm(context.Background(), "/dirs/should/be/created/readme", true)
97+
is.Nil(err)
98+
}
99+
85100
func TestFilesFlush(t *testing.T) {
86101
is := is.New(t)
87102
s := NewShell(shellUrl)

0 commit comments

Comments
 (0)