From 764ea72b5717ec24920b1c0c86b1e08ddfea5547 Mon Sep 17 00:00:00 2001 From: Viktor Kostadinov Date: Thu, 15 Sep 2022 13:33:22 -0400 Subject: [PATCH 1/2] Add missing parents option for FilesCp command Signed-off-by: vkost --- mfs.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/mfs.go b/mfs.go index 482b953b7..4f4317e42 100644 --- a/mfs.go +++ b/mfs.go @@ -41,6 +41,7 @@ type filesMkdir struct{} type filesRead struct{} type filesWrite struct{} type filesStat struct{} +type filesCp struct{} var ( FilesLs filesLs @@ -49,6 +50,7 @@ var ( FilesRead filesRead FilesWrite filesWrite FilesStat filesStat + FilesCp filesCp ) // Stat use long listing format @@ -203,6 +205,14 @@ func (filesWrite) Hash(hash string) FilesOpt { } } +// Parents make parent directories as needed +func (filesCp) Parents(parents bool) FilesOpt { + return func(rb *RequestBuilder) error { + rb.Option("parents", parents) + return nil + } +} + // FilesChcid change the cid version or hash function of the root node of a given path func (s *Shell) FilesChcid(ctx context.Context, path string, options ...FilesOpt) error { if len(path) == 0 { @@ -220,8 +230,14 @@ func (s *Shell) FilesChcid(ctx context.Context, path string, options ...FilesOpt } // FilesCp copy any IPFS files and directories into MFS (or copy within MFS) -func (s *Shell) FilesCp(ctx context.Context, src string, dest string) error { - return s.Request("files/cp", src, dest).Exec(ctx, nil) +func (s *Shell) FilesCp(ctx context.Context, src string, dest string, options ...FilesOpt) error { + rb := s.Request("files/cp", src, dest) + for _, opt := range options { + if err := opt(rb); err != nil { + return err + } + } + return rb.Exec(ctx, nil) } // FilesFlush flush a given path's data to disk From cc755e88de611b4112285b9207508efc8b8d3b4c Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 28 Mar 2023 11:33:04 +0200 Subject: [PATCH 2/2] feat: add files cp parents test --- mfs_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mfs_test.go b/mfs_test.go index bb1cf7326..2257d0784 100644 --- a/mfs_test.go +++ b/mfs_test.go @@ -82,6 +82,21 @@ func TestFilesCp(t *testing.T) { is.Nil(err) } +func TestFilesCParents(t *testing.T) { + is := is.New(t) + s := NewShell(shellUrl) + + err := s.FilesCp(context.Background(), "/testdata/readme", "/dirs/should/be/created/readme", FilesCp.Parents(true)) + is.Nil(err) + + stat, err := s.FilesStat(context.Background(), "/dirs/should/be/created/readme") + is.Nil(err) + is.Equal(stat.Hash, "QmfZt7xPekp7npSM6DHDUnFseAiNZQs7wq6muH9o99RsCB") + + err = s.FilesRm(context.Background(), "/dirs/should/be/created/readme", true) + is.Nil(err) +} + func TestFilesFlush(t *testing.T) { is := is.New(t) s := NewShell(shellUrl)