Skip to content

Commit

Permalink
walk: add a walk function
Browse files Browse the repository at this point in the history
This commit was moved from ipfs/go-ipfs-files@e1ba4f6
  • Loading branch information
Stebalien committed Aug 16, 2019
1 parent caa31c7 commit 694bbf0
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions files/walk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package files

import (
"path/filepath"
)

// Walk walks a file tree, like `os.Walk`.
func Walk(nd Node, cb func(fpath string, nd Node) error) error {
var helper func(string, Node) error
helper = func(path string, nd Node) error {
if err := cb(path, nd); err != nil {
return err
}
dir, ok := nd.(Directory)
if !ok {
return nil
}
iter := dir.Entries()
for iter.Next() {
if err := helper(filepath.Join(path, iter.Name()), iter.Node()); err != nil {
return err
}
}
return iter.Err()
}
return helper("", nd)
}

0 comments on commit 694bbf0

Please sign in to comment.