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

feat: add missing "parents" option to FilesCp command #274

Merged
merged 2 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions mfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type filesMkdir struct{}
type filesRead struct{}
type filesWrite struct{}
type filesStat struct{}
type filesCp struct{}

var (
FilesLs filesLs
Expand All @@ -49,6 +50,7 @@ var (
FilesRead filesRead
FilesWrite filesWrite
FilesStat filesStat
FilesCp filesCp
)

// Stat use long listing format
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions mfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down