summaryrefslogtreecommitdiff
path: root/funcs
diff options
context:
space:
mode:
authorChristopher <christopher@rstor.io>2018-04-13 13:56:19 +0200
committerDave Henderson <dhenderson@gmail.com>2018-04-13 07:56:19 -0400
commitf867f3564dc2e3337f9481c684edd0c33df25648 (patch)
treeaefbf97bb02485fcdc2e9c2aee731a6791a996f1 /funcs
parent0d857a14e86acd66ebaf9e4a2f1b738fe05099e5 (diff)
New function proposal: `files.Walk` (#281)
* Update file.go Glob function * returns all the paths of every file inside a directory * added TrimPrefix function * avoiding errors when no directory or file is founded * adapted TrimPrefix to work on pipelines * tests added for string.TrimPrefix * strings.TrimSuffix function removed from this PR * Walk function created * WalkTest modified to make it compatible with other OS * first path must be os agnostic too
Diffstat (limited to 'funcs')
-rw-r--r--funcs/file.go13
-rw-r--r--funcs/file_test.go23
2 files changed, 36 insertions, 0 deletions
diff --git a/funcs/file.go b/funcs/file.go
index 1f1a30bd..04de4e85 100644
--- a/funcs/file.go
+++ b/funcs/file.go
@@ -55,3 +55,16 @@ func (f *FileFuncs) IsDir(path string) bool {
func (f *FileFuncs) ReadDir(path string) ([]string, error) {
return file.ReadDir(path)
}
+
+// Walk -
+func (f *FileFuncs) Walk(path string) ([]string, error) {
+ files := make([]string, 0)
+ afero.Walk(f.fs, path, func(subpath string, finfo os.FileInfo, err error) error {
+ if err != nil {
+ return err
+ }
+ files = append(files, subpath)
+ return nil
+ })
+ return files, nil
+}
diff --git a/funcs/file_test.go b/funcs/file_test.go
index 2a3ce9a9..56651421 100644
--- a/funcs/file_test.go
+++ b/funcs/file_test.go
@@ -1,6 +1,7 @@
package funcs
import (
+ "path/filepath"
"testing"
"github.com/spf13/afero"
@@ -30,3 +31,25 @@ func TestFileIsDir(t *testing.T) {
assert.True(t, ff.IsDir("/tmp"))
assert.False(t, ff.IsDir("/tmp/foo"))
}
+
+func TestFileWalk(t *testing.T) {
+ fs := afero.NewMemMapFs()
+ ff := &FileFuncs{fs}
+
+ _ = fs.Mkdir("/tmp", 0777)
+ _ = fs.Mkdir("/tmp/bar", 0777)
+ _ = fs.Mkdir("/tmp/bar/baz", 0777)
+ f, _ := fs.Create("/tmp/bar/baz/foo")
+ _, _ = f.Write([]byte("foo"))
+
+ expectedLists := [][]string{{"tmp"}, {"tmp", "bar" }, {"tmp", "bar", "baz"}, {"tmp", "bar", "baz", "foo"}}
+ expectedPaths := make([]string, 0)
+ for _, path := range expectedLists {
+ expectedPaths = append(expectedPaths, string(filepath.Separator) + filepath.Join(path...))
+ }
+
+ actualPaths, err := ff.Walk(string(filepath.Separator) + "tmp")
+
+ assert.NoError(t, err)
+ assert.Equal(t, expectedPaths, actualPaths)
+}