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

Commit a976407

Browse files
acashmoneyhacdias
andauthored
feat: add options to AddDir (#285)
Co-authored-by: Henrique Dias <[email protected]>
1 parent 594b994 commit a976407

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

add.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (s *Shell) AddLink(target string) (string, error) {
9797
}
9898

9999
// AddDir adds a directory recursively with all of the files under it
100-
func (s *Shell) AddDir(dir string) (string, error) {
100+
func (s *Shell) AddDir(dir string, options ...AddOpts) (string, error) {
101101
stat, err := os.Lstat(dir)
102102
if err != nil {
103103
return "", err
@@ -110,14 +110,18 @@ func (s *Shell) AddDir(dir string) (string, error) {
110110
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry(filepath.Base(dir), sf)})
111111
reader := files.NewMultiFileReader(slf, true)
112112

113-
resp, err := s.Request("add").
114-
Option("recursive", true).
115-
Body(reader).
116-
Send(context.Background())
113+
rb := s.Request("add").Option("recursive", true)
114+
for _, option := range options {
115+
option(rb)
116+
}
117+
118+
// Here we cannot use .Exec because "add" streams responses back for each file
119+
// within the directory, and we only care about the last one, which is the directory
120+
// itself.
121+
resp, err := rb.Body(reader).Send(context.Background())
117122
if err != nil {
118123
return "", err
119124
}
120-
121125
defer resp.Close()
122126

123127
if resp.Error != nil {

shell_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ func TestAddDir(t *testing.T) {
125125
is.Equal(cid, "QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")
126126
}
127127

128+
func TestAddDirWithCidV1(t *testing.T) {
129+
is := is.New(t)
130+
s := NewShell(shellUrl)
131+
132+
cid, err := s.AddDir("./testdata", CidVersion(1))
133+
is.Nil(err)
134+
is.Equal(cid, "bafybeibgegl5yqme2jehvvneapbq7he5ahi3tmk4cpmlagrggeji6hzayq")
135+
}
136+
128137
func TestAddDirOffline(t *testing.T) {
129138
is := is.New(t)
130139
s := NewShell("0.0.0.0:1234") // connect to an invalid address

0 commit comments

Comments
 (0)