Skip to content

Commit

Permalink
Simplify ExpandPathsGlob code
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed May 17, 2018
1 parent 48fcab3 commit c6b1701
Showing 1 changed file with 13 additions and 34 deletions.
47 changes: 13 additions & 34 deletions pkg/skaffold/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -68,13 +67,14 @@ func StrSliceContains(sl []string, s string) bool {
// ExpandPathsGlob expands paths according to filepath.Glob patterns
// Returns a list of unique files that match the glob patterns passed in.
func ExpandPathsGlob(paths []string) ([]string, error) {
expandedPaths := map[string]struct{}{}
expandedPaths := make(map[string]bool)
for _, p := range paths {
if _, err := Fs.Stat(p); err == nil {
// This is a file reference, so just add it
expandedPaths[p] = struct{}{}
expandedPaths[p] = true
continue
}

files, err := afero.Glob(Fs, p)
if err != nil {
return nil, errors.Wrap(err, "glob")
Expand All @@ -84,48 +84,27 @@ func ExpandPathsGlob(paths []string) ([]string, error) {
}

for _, f := range files {
fi, err := Fs.Stat(f)
err := afero.Walk(Fs, f, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
expandedPaths[path] = true
}

return nil
})
if err != nil {
return nil, err
}
if err := addFileOrDir(Fs, f, fi, expandedPaths); err != nil {
return nil, errors.Wrap(err, "adding file or dir")
return nil, errors.Wrap(err, "filepath walk")
}
}
}
ret := []string{}

var ret []string
for k := range expandedPaths {
ret = append(ret, k)
}
sort.Strings(ret)
return ret, nil
}

func addFileOrDir(fs afero.Fs, ref string, info os.FileInfo, expandedPaths map[string]struct{}) error {
if info.IsDir() {
return addDir(fs, ref, expandedPaths)
}
expandedPaths[ref] = struct{}{}
return nil
}

func addDir(fs afero.Fs, dir string, expandedPaths map[string]struct{}) error {
logrus.Debugf("Recursively adding %s", dir)
if err := afero.Walk(fs, dir, func(path string, info os.FileInfo, err error) error {
if info == nil {
return nil
}
if info.IsDir() {
return nil
}
expandedPaths[path] = struct{}{}
return nil
}); err != nil {
return errors.Wrap(err, "filepath walk")
}
return nil
}

// BoolPtr returns a pointer to a bool
func BoolPtr(b bool) *bool {
o := b
Expand Down

0 comments on commit c6b1701

Please sign in to comment.