Skip to content

Commit

Permalink
Merge pull request #19 from moov-io/actually-skip-dir
Browse files Browse the repository at this point in the history
fix: respect SkipDir to bypass directories
  • Loading branch information
adamdecaf authored Jan 22, 2025
2 parents 9887a74 + 78d77e2 commit 323561d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
20 changes: 19 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,19 +426,37 @@ func (cc *client) Walk(dir string, fn fs.WalkDirFunc) error {

// Setup a Walker for each file
walker := conn.Walk(dir)

var skippedDirs []string
for walker.Next() {
if err := walker.Err(); err != nil {
return err
}

var skip bool
for _, sd := range skippedDirs {
matched := strings.HasPrefix(walker.Path(), sd)
if matched {
skip = true
break
}
}
if skip {
walker.SkipDir()
continue
}

entry := Entry{
fd: walker.Stat(),
}
if entry.fd == nil || entry.IsDir() {
if entry.fd == nil {
continue
}
err = fn(walker.Path(), entry, walker.Err())
if err != nil {
if err == fs.SkipDir {
skippedDirs = append(skippedDirs, filepath.Join(dir, ""))

walker.SkipDir()
} else {
return fmt.Errorf("walking %s failed: %w", walker.Path(), err)
Expand Down
25 changes: 22 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ func TestClient(t *testing.T) {
})
require.NoError(t, err)
require.ElementsMatch(t, found, []string{
"Upper/names.txt", "bigdata/large.txt",
"Upper", "Upper/names.txt", "bigdata", "bigdata/large.txt",
"first.txt", "second.txt", "empty.txt",
"archive/old.txt", "archive/empty2.txt",
"with-empty/EMPTY1.txt", "with-empty/empty_file2.txt",
"archive", "archive/old.txt", "archive/empty2.txt",
"with-empty", "with-empty/EMPTY1.txt", "with-empty/empty_file2.txt",
"with-empty/data.txt", "with-empty/data2.txt",
})
})
Expand All @@ -232,6 +232,25 @@ func TestClient(t *testing.T) {
})
})

t.Run("walk skipdir", func(t *testing.T) {
var found []string
err := client.Walk(".", func(path string, d fs.DirEntry, err error) error {
found = append(found, path)
if strings.Contains(path, "with-empty") {
return fs.SkipDir
}
return nil
})
require.NoError(t, err)
require.ElementsMatch(t, found, []string{
"with-empty",
"second.txt", "first.txt", "empty.txt",
"bigdata", "bigdata/large.txt",
"archive", "archive/old.txt", "archive/empty2.txt",
"Upper", "Upper/names.txt",
})
})

require.NoError(t, client.Close())
}

Expand Down

0 comments on commit 323561d

Please sign in to comment.