Skip to content

Commit

Permalink
fix: prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Nov 6, 2020
1 parent 11ce768 commit 93c4dab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
16 changes: 9 additions & 7 deletions glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zglob
import (
"fmt"
"os"
"strings"

"github.com/gobwas/glob"
"github.com/spf13/afero"
Expand All @@ -15,6 +16,7 @@ func Glob(pattern string) ([]string, error) {
func GlobWithFs(fs afero.Fs, pattern string) ([]string, error) {
var matches []string

pattern = strings.TrimPrefix(pattern, "./")
matcher, err := glob.Compile(pattern)
if err != nil {
return matches, err
Expand All @@ -30,7 +32,8 @@ func GlobWithFs(fs afero.Fs, pattern string) ([]string, error) {
// if the prefix does not exist, the whole
// glob pattern won't match anything
return []string{}, nil
} else if err != nil {
}
if err != nil {
return nil, fmt.Errorf("stat prefix: %w", err)
}

Expand All @@ -44,34 +47,33 @@ func GlobWithFs(fs afero.Fs, pattern string) ([]string, error) {
return []string{}, nil
}

return matches, afero.Walk(fs, prefix, func(currentPath string, info os.FileInfo,
err error) error {
return matches, afero.Walk(fs, prefix, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !matcher.Match(currentPath) {
if !matcher.Match(path) {
return nil
}

if info.IsDir() {
// a direct match on a directory implies that all files inside match
filesInDir, err := filesInDirectory(fs, currentPath)
filesInDir, err := filesInDirectory(fs, path)
if err != nil {
return err
}

matches = append(matches, filesInDir...)
} else {
matches = append(matches, currentPath)
matches = append(matches, path)
}

return nil
})
}

func filesInDirectory(fs afero.Fs, dir string) ([]string, error) {
files := []string{}
var files []string

err := afero.Walk(fs, dir, func(currentPath string, info os.FileInfo, err error) error {
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func TestGlob(t *testing.T) {
require.Equal(t, []string{
"a/b/file1.txt",
"a/d/file1.txt",
"a/e/f",
"a/e/f/file1.txt",
}, matches)
})
Expand Down

0 comments on commit 93c4dab

Please sign in to comment.