Browse code

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 <rafael.colton@gmail.com>

Rafe Colton authored on 2014/09/30 15:21:41
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,26 @@
0
+package fileutils
1
+
2
+import (
3
+	"github.com/docker/docker/pkg/log"
4
+	"path/filepath"
5
+)
6
+
7
+// Matches returns true if relFilePath matches any of the patterns
8
+func Matches(relFilePath string, patterns []string) (bool, error) {
9
+	for _, exclude := range patterns {
10
+		matched, err := filepath.Match(exclude, relFilePath)
11
+		if err != nil {
12
+			log.Errorf("Error matching: %s (pattern: %s)", relFilePath, exclude)
13
+			return false, err
14
+		}
15
+		if matched {
16
+			if filepath.Clean(relFilePath) == "." {
17
+				log.Errorf("Can't exclude whole path, excluding pattern: %s", exclude)
18
+				continue
19
+			}
20
+			log.Debugf("Skipping excluded path: %s", relFilePath)
21
+			return true, nil
22
+		}
23
+	}
24
+	return false, nil
25
+}
... ...
@@ -21,6 +21,7 @@ import (
21 21
 	"syscall"
22 22
 
23 23
 	"github.com/docker/docker/dockerversion"
24
+	"github.com/docker/docker/pkg/fileutils"
24 25
 	"github.com/docker/docker/pkg/ioutils"
25 26
 	"github.com/docker/docker/pkg/log"
26 27
 )
... ...
@@ -493,7 +494,7 @@ func ValidateContextDirectory(srcPath string, excludes []string) error {
493 493
 		// skip this directory/file if it's not in the path, it won't get added to the context
494 494
 		if relFilePath, err := filepath.Rel(srcPath, filePath); err != nil {
495 495
 			return err
496
-		} else if skip, err := Matches(relFilePath, excludes); err != nil {
496
+		} else if skip, err := fileutils.Matches(relFilePath, excludes); err != nil {
497 497
 			return err
498 498
 		} else if skip {
499 499
 			if f.IsDir() {
... ...
@@ -537,23 +538,3 @@ func StringsContainsNoCase(slice []string, s string) bool {
537 537
 	}
538 538
 	return false
539 539
 }
540
-
541
-// Matches returns true if relFilePath matches any of the patterns
542
-func Matches(relFilePath string, patterns []string) (bool, error) {
543
-	for _, exclude := range patterns {
544
-		matched, err := filepath.Match(exclude, relFilePath)
545
-		if err != nil {
546
-			log.Errorf("Error matching: %s (pattern: %s)", relFilePath, exclude)
547
-			return false, err
548
-		}
549
-		if matched {
550
-			if filepath.Clean(relFilePath) == "." {
551
-				log.Errorf("Can't exclude whole path, excluding pattern: %s", exclude)
552
-				continue
553
-			}
554
-			log.Debugf("Skipping excluded path: %s", relFilePath)
555
-			return true, nil
556
-		}
557
-	}
558
-	return false, nil
559
-}