This repository has been archived by the owner on Jun 25, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new WalkPrefix function (#79)
* Adds a new WalkPrefix function
- Loading branch information
Showing
11 changed files
with
181 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ sudo: false | |
go: | ||
- 1.8 | ||
- 1.9 | ||
- "1.10" | ||
- tip | ||
|
||
matrix: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
module github.com/gobuffalo/packr | ||
|
||
require github.com/pkg/errors v0.8.0 | ||
require ( | ||
github.com/davecgh/go-spew v1.1.0 // indirect | ||
github.com/markbates/grift v1.0.0 | ||
github.com/pkg/errors v0.8.0 | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/spf13/cobra v0.0.3 | ||
github.com/spf13/pflag v1.0.1 // indirect | ||
github.com/stretchr/testify v1.2.2 | ||
golang.org/x/net v0.0.0-20180808004115-f9ce57c11b24 // indirect | ||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/markbates/grift v1.0.0/go.mod h1:6qyNEZSY8v6duE2tBtO/tPgBvxhT7g7DnQoIYpEyCfw= | ||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= | ||
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= | ||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
golang.org/x/net v0.0.0-20180808004115-f9ce57c11b24 h1:mEsFm194MmS9vCwxFy+zwu0EU7ZkxxMD1iH++vmGdUY= | ||
golang.org/x/net v0.0.0-20180808004115-f9ce57c11b24/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | ||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package packr | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type WalkFunc func(string, File) error | ||
|
||
// Walk will traverse the box and call the WalkFunc for each file in the box/folder. | ||
func (b Box) Walk(wf WalkFunc) error { | ||
if data[b.Path] == nil { | ||
base, err := filepath.EvalSymlinks(filepath.Join(b.callingDir, b.Path)) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
return filepath.Walk(base, func(path string, info os.FileInfo, err error) error { | ||
cleanName, err := filepath.Rel(base, path) | ||
if err != nil { | ||
cleanName = strings.TrimPrefix(path, base) | ||
} | ||
cleanName = filepath.ToSlash(filepath.Clean(cleanName)) | ||
cleanName = strings.TrimPrefix(cleanName, "/") | ||
cleanName = filepath.FromSlash(cleanName) | ||
if info == nil || info.IsDir() { | ||
return nil | ||
} | ||
|
||
file, err := fileFor(path, cleanName) | ||
if err != nil { | ||
return err | ||
} | ||
return wf(cleanName, file) | ||
}) | ||
} | ||
for n := range data[b.Path] { | ||
f, err := b.find(n) | ||
if err != nil { | ||
return err | ||
} | ||
err = wf(n, f) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// WalkPrefix will call box.Walk and call the WalkFunc when it finds paths that have a matching prefix | ||
func (b Box) WalkPrefix(prefix string, wf WalkFunc) error { | ||
opre := osPath(prefix) | ||
return b.Walk(func(path string, f File) error { | ||
if strings.HasPrefix(osPath(path), opre) { | ||
if err := wf(path, f); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
} | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package packr | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Box_Walk_Physical(t *testing.T) { | ||
r := require.New(t) | ||
count := 0 | ||
err := testBox.Walk(func(path string, f File) error { | ||
count++ | ||
return nil | ||
}) | ||
r.NoError(err) | ||
r.Equal(3, count) | ||
} | ||
|
||
func Test_Box_Walk_Virtual(t *testing.T) { | ||
r := require.New(t) | ||
count := 0 | ||
err := virtualBox.Walk(func(path string, f File) error { | ||
count++ | ||
return nil | ||
}) | ||
r.NoError(err) | ||
r.Equal(4, count) | ||
} | ||
|
||
func Test_Box_WalkPrefix_Physical(t *testing.T) { | ||
r := require.New(t) | ||
var files []string | ||
b := NewBox("../packr/fixtures") | ||
err := b.WalkPrefix("foo/", func(path string, f File) error { | ||
files = append(files, path) | ||
return nil | ||
}) | ||
r.NoError(err) | ||
r.Equal(2, len(files)) | ||
mustHave := osPaths("foo/a.txt", "foo/bar/b.txt") | ||
r.Equal(mustHave, files) | ||
} | ||
|
||
func Test_Box_WalkPrefix_Virtual(t *testing.T) { | ||
r := require.New(t) | ||
var files []string | ||
err := virtualBox.WalkPrefix("d", func(path string, f File) error { | ||
files = append(files, path) | ||
return nil | ||
}) | ||
r.NoError(err) | ||
r.Equal(1, len(files)) | ||
r.Equal([]string{"d/a"}, files) | ||
} |