From 73f4bfed810b65943f1d9d038a8db9bd834067fa Mon Sep 17 00:00:00 2001 From: Rafe Colton Date: Mon, 29 Sep 2014 23:21:41 -0700 Subject: [PATCH] Move Matches() file path matching function into pkg/fileutils This is the second of two steps to break the archive package's dependence on utils so that archive may be moved into pkg. `Matches()` is also a good candidate pkg in that it is small, concise, and not specific to docker internals Signed-off-by: Rafe Colton --- pkg/fileutils/fileutils.go | 26 ++++++++++++++++++++++++++ utils/utils.go | 23 ++--------------------- 2 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 pkg/fileutils/fileutils.go diff --git a/pkg/fileutils/fileutils.go b/pkg/fileutils/fileutils.go new file mode 100644 index 0000000000..acc27f55b5 --- /dev/null +++ b/pkg/fileutils/fileutils.go @@ -0,0 +1,26 @@ +package fileutils + +import ( + "github.com/docker/docker/pkg/log" + "path/filepath" +) + +// Matches returns true if relFilePath matches any of the patterns +func Matches(relFilePath string, patterns []string) (bool, error) { + for _, exclude := range patterns { + matched, err := filepath.Match(exclude, relFilePath) + if err != nil { + log.Errorf("Error matching: %s (pattern: %s)", relFilePath, exclude) + return false, err + } + if matched { + if filepath.Clean(relFilePath) == "." { + log.Errorf("Can't exclude whole path, excluding pattern: %s", exclude) + continue + } + log.Debugf("Skipping excluded path: %s", relFilePath) + return true, nil + } + } + return false, nil +} diff --git a/utils/utils.go b/utils/utils.go index 3a14c41699..792b80bd51 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -21,6 +21,7 @@ import ( "syscall" "github.com/docker/docker/dockerversion" + "github.com/docker/docker/pkg/fileutils" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/log" ) @@ -493,7 +494,7 @@ func ValidateContextDirectory(srcPath string, excludes []string) error { // skip this directory/file if it's not in the path, it won't get added to the context if relFilePath, err := filepath.Rel(srcPath, filePath); err != nil { return err - } else if skip, err := Matches(relFilePath, excludes); err != nil { + } else if skip, err := fileutils.Matches(relFilePath, excludes); err != nil { return err } else if skip { if f.IsDir() { @@ -537,23 +538,3 @@ func StringsContainsNoCase(slice []string, s string) bool { } return false } - -// Matches returns true if relFilePath matches any of the patterns -func Matches(relFilePath string, patterns []string) (bool, error) { - for _, exclude := range patterns { - matched, err := filepath.Match(exclude, relFilePath) - if err != nil { - log.Errorf("Error matching: %s (pattern: %s)", relFilePath, exclude) - return false, err - } - if matched { - if filepath.Clean(relFilePath) == "." { - log.Errorf("Can't exclude whole path, excluding pattern: %s", exclude) - continue - } - log.Debugf("Skipping excluded path: %s", relFilePath) - return true, nil - } - } - return false, nil -}