Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
Adds a new WalkPrefix function (#79)
Browse files Browse the repository at this point in the history
* Adds a new WalkPrefix function
  • Loading branch information
markbates authored Aug 8, 2018
1 parent 9e76dbe commit 48bc2d0
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 70 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ sudo: false
go:
- 1.8
- 1.9
- "1.10"
- tip

matrix:
Expand Down
40 changes: 0 additions & 40 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,46 +149,6 @@ func (b Box) find(name string) (File, error) {
return fileFor(p, cleanName)
}

type WalkFunc func(string, File) error

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
}

// Open returns a File using the http.File interface
func (b Box) Open(name string) (http.File, error) {
return b.find(name)
Expand Down
24 changes: 1 addition & 23 deletions box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,6 @@ func Test_Box_Has(t *testing.T) {
r.False(testBox.Has("idontexist.txt"))
}

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_List_Virtual(t *testing.T) {
r := require.New(t)
mustHave := []string{"a", "b", "c", "d/a"}
Expand All @@ -74,7 +52,7 @@ func Test_List_Virtual(t *testing.T) {

func Test_List_Physical(t *testing.T) {
r := require.New(t)
mustHave := []string{"goodbye.txt", "hello.txt", "index.html"}
mustHave := osPaths("foo/a.txt", "foo/bar/b.txt", "goodbye.txt", "hello.txt", "index.html")
actual := testBox.List()
r.Equal(mustHave, actual)
}
Expand Down
24 changes: 18 additions & 6 deletions env.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package packr

import (
"go/build"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
)

var goPath = filepath.Join(os.Getenv("HOME"), "go")

func init() {
var once sync.Once
once.Do(func() {
cmd := exec.Command("go", "env", "GOPATH")
b, err := cmd.CombinedOutput()
if err != nil {
return
}
goPath = strings.TrimSpace(string(b))
})
}

// GoPath returns the current GOPATH env var
// or if it's missing, the default.
func GoPath() string {
go_path := strings.Split(os.Getenv("GOPATH"), string(os.PathListSeparator))
if len(go_path) == 0 || go_path[0] == "" {
return build.Default.GOPATH
}
return go_path[0]
return goPath
}

// GoBin returns the current GO_BIN env var
Expand Down
Empty file added fixtures/foo/a.txt
Empty file.
Empty file added fixtures/foo/bar/b.txt
Empty file.
12 changes: 11 additions & 1 deletion go.mod
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
)
13 changes: 13 additions & 0 deletions go.sum
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=
19 changes: 19 additions & 0 deletions packr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"compress/gzip"
"encoding/json"
"runtime"
"strings"
"sync"
)

Expand Down Expand Up @@ -53,3 +55,20 @@ func UnpackBytes(box string) {
defer gil.Unlock()
delete(data, box)
}

func osPaths(paths ...string) []string {
if runtime.GOOS == "windows" {
for i, path := range paths {
paths[i] = strings.Replace(path, "/", "\\", -1)
}
}

return paths
}

func osPath(path string) string {
if runtime.GOOS == "windows" {
return strings.Replace(path, "/", "\\", -1)
}
return path
}
63 changes: 63 additions & 0 deletions walk.go
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
})
}
55 changes: 55 additions & 0 deletions walk_test.go
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)
}

0 comments on commit 48bc2d0

Please sign in to comment.