Browse code

utils: move dockerignore function to builder/dockerignore

Signed-off-by: Tibor Vass <tibor@docker.com>

Tibor Vass authored on 2015/12/14 19:15:00
Showing 6 changed files
... ...
@@ -16,6 +16,7 @@ import (
16 16
 	"github.com/docker/distribution/reference"
17 17
 	"github.com/docker/docker/api"
18 18
 	"github.com/docker/docker/api/types"
19
+	"github.com/docker/docker/builder/dockerignore"
19 20
 	Cli "github.com/docker/docker/cli"
20 21
 	"github.com/docker/docker/opts"
21 22
 	"github.com/docker/docker/pkg/archive"
... ...
@@ -132,7 +133,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
132 132
 
133 133
 	var excludes []string
134 134
 	if err == nil {
135
-		excludes, err = utils.ReadDockerIgnore(f)
135
+		excludes, err = dockerignore.ReadAll(f)
136 136
 		if err != nil {
137 137
 			return err
138 138
 		}
... ...
@@ -3,8 +3,8 @@ package builder
3 3
 import (
4 4
 	"os"
5 5
 
6
+	"github.com/docker/docker/builder/dockerignore"
6 7
 	"github.com/docker/docker/pkg/fileutils"
7
-	"github.com/docker/docker/utils"
8 8
 )
9 9
 
10 10
 // DockerIgnoreContext wraps a ModifiableContext to add a method
... ...
@@ -27,7 +27,7 @@ type DockerIgnoreContext struct {
27 27
 // TODO: Don't require a ModifiableContext (use Context instead) and don't remove
28 28
 // files, instead handle a list of files to be excluded from the context.
29 29
 func (c DockerIgnoreContext) Process(filesToRemove []string) error {
30
-	dockerignore, err := c.Open(".dockerignore")
30
+	f, err := c.Open(".dockerignore")
31 31
 	// Note that a missing .dockerignore file isn't treated as an error
32 32
 	if err != nil {
33 33
 		if os.IsNotExist(err) {
... ...
@@ -35,7 +35,7 @@ func (c DockerIgnoreContext) Process(filesToRemove []string) error {
35 35
 		}
36 36
 		return err
37 37
 	}
38
-	excludes, _ := utils.ReadDockerIgnore(dockerignore)
38
+	excludes, _ := dockerignore.ReadAll(f)
39 39
 	filesToRemove = append([]string{".dockerignore"}, filesToRemove...)
40 40
 	for _, fileToRemove := range filesToRemove {
41 41
 		rm, _ := fileutils.Matches(fileToRemove, excludes)
42 42
new file mode 100644
... ...
@@ -0,0 +1,34 @@
0
+package dockerignore
1
+
2
+import (
3
+	"bufio"
4
+	"fmt"
5
+	"io"
6
+	"path/filepath"
7
+	"strings"
8
+)
9
+
10
+// ReadAll reads a .dockerignore file and returns the list of file patterns
11
+// to ignore. Note this will trim whitespace from each line as well
12
+// as use GO's "clean" func to get the shortest/cleanest path for each.
13
+func ReadAll(reader io.ReadCloser) ([]string, error) {
14
+	if reader == nil {
15
+		return nil, nil
16
+	}
17
+	defer reader.Close()
18
+	scanner := bufio.NewScanner(reader)
19
+	var excludes []string
20
+
21
+	for scanner.Scan() {
22
+		pattern := strings.TrimSpace(scanner.Text())
23
+		if pattern == "" {
24
+			continue
25
+		}
26
+		pattern = filepath.Clean(pattern)
27
+		excludes = append(excludes, pattern)
28
+	}
29
+	if err := scanner.Err(); err != nil {
30
+		return nil, fmt.Errorf("Error reading .dockerignore: %v", err)
31
+	}
32
+	return excludes, nil
33
+}
0 34
new file mode 100644
... ...
@@ -0,0 +1,55 @@
0
+package dockerignore
1
+
2
+import (
3
+	"fmt"
4
+	"io/ioutil"
5
+	"os"
6
+	"path/filepath"
7
+	"testing"
8
+)
9
+
10
+func TestReadAll(t *testing.T) {
11
+	tmpDir, err := ioutil.TempDir("", "dockerignore-test")
12
+	if err != nil {
13
+		t.Fatal(err)
14
+	}
15
+	defer os.RemoveAll(tmpDir)
16
+
17
+	di, err := ReadAll(nil)
18
+	if err != nil {
19
+		t.Fatalf("Expected not to have error, got %v", err)
20
+	}
21
+
22
+	if diLen := len(di); diLen != 0 {
23
+		t.Fatalf("Expected to have zero dockerignore entry, got %d", diLen)
24
+	}
25
+
26
+	diName := filepath.Join(tmpDir, ".dockerignore")
27
+	content := fmt.Sprintf("test1\n/test2\n/a/file/here\n\nlastfile")
28
+	err = ioutil.WriteFile(diName, []byte(content), 0777)
29
+	if err != nil {
30
+		t.Fatal(err)
31
+	}
32
+
33
+	diFd, err := os.Open(diName)
34
+	if err != nil {
35
+		t.Fatal(err)
36
+	}
37
+	di, err = ReadAll(diFd)
38
+	if err != nil {
39
+		t.Fatal(err)
40
+	}
41
+
42
+	if di[0] != "test1" {
43
+		t.Fatalf("First element is not test1")
44
+	}
45
+	if di[1] != "/test2" {
46
+		t.Fatalf("Second element is not /test2")
47
+	}
48
+	if di[2] != "/a/file/here" {
49
+		t.Fatalf("Third element is not /a/file/here")
50
+	}
51
+	if di[3] != "lastfile" {
52
+		t.Fatalf("Fourth element is not lastfile")
53
+	}
54
+}
... ...
@@ -1,7 +1,6 @@
1 1
 package utils
2 2
 
3 3
 import (
4
-	"bufio"
5 4
 	"crypto/sha1"
6 5
 	"encoding/hex"
7 6
 	"fmt"
... ...
@@ -244,31 +243,6 @@ func ValidateContextDirectory(srcPath string, excludes []string) error {
244 244
 	})
245 245
 }
246 246
 
247
-// ReadDockerIgnore reads a .dockerignore file and returns the list of file patterns
248
-// to ignore. Note this will trim whitespace from each line as well
249
-// as use GO's "clean" func to get the shortest/cleanest path for each.
250
-func ReadDockerIgnore(reader io.ReadCloser) ([]string, error) {
251
-	if reader == nil {
252
-		return nil, nil
253
-	}
254
-	defer reader.Close()
255
-	scanner := bufio.NewScanner(reader)
256
-	var excludes []string
257
-
258
-	for scanner.Scan() {
259
-		pattern := strings.TrimSpace(scanner.Text())
260
-		if pattern == "" {
261
-			continue
262
-		}
263
-		pattern = filepath.Clean(pattern)
264
-		excludes = append(excludes, pattern)
265
-	}
266
-	if err := scanner.Err(); err != nil {
267
-		return nil, fmt.Errorf("Error reading .dockerignore: %v", err)
268
-	}
269
-	return excludes, nil
270
-}
271
-
272 247
 // GetErrorMessage returns the human readable message associated with
273 248
 // the passed-in error. In some cases the default Error() func returns
274 249
 // something that is less than useful so based on its types this func
... ...
@@ -1,12 +1,6 @@
1 1
 package utils
2 2
 
3
-import (
4
-	"fmt"
5
-	"io/ioutil"
6
-	"os"
7
-	"path/filepath"
8
-	"testing"
9
-)
3
+import "testing"
10 4
 
11 5
 func TestReplaceAndAppendEnvVars(t *testing.T) {
12 6
 	var (
... ...
@@ -25,49 +19,3 @@ func TestReplaceAndAppendEnvVars(t *testing.T) {
25 25
 		t.Fatalf("expected TERM=xterm got '%s'", env[1])
26 26
 	}
27 27
 }
28
-
29
-func TestReadDockerIgnore(t *testing.T) {
30
-	tmpDir, err := ioutil.TempDir("", "dockerignore-test")
31
-	if err != nil {
32
-		t.Fatal(err)
33
-	}
34
-	defer os.RemoveAll(tmpDir)
35
-
36
-	di, err := ReadDockerIgnore(nil)
37
-	if err != nil {
38
-		t.Fatalf("Expected not to have error, got %v", err)
39
-	}
40
-
41
-	if diLen := len(di); diLen != 0 {
42
-		t.Fatalf("Expected to have zero dockerignore entry, got %d", diLen)
43
-	}
44
-
45
-	diName := filepath.Join(tmpDir, ".dockerignore")
46
-	content := fmt.Sprintf("test1\n/test2\n/a/file/here\n\nlastfile")
47
-	err = ioutil.WriteFile(diName, []byte(content), 0777)
48
-	if err != nil {
49
-		t.Fatal(err)
50
-	}
51
-
52
-	diFd, err := os.Open(diName)
53
-	if err != nil {
54
-		t.Fatal(err)
55
-	}
56
-	di, err = ReadDockerIgnore(diFd)
57
-	if err != nil {
58
-		t.Fatal(err)
59
-	}
60
-
61
-	if di[0] != "test1" {
62
-		t.Fatalf("First element is not test1")
63
-	}
64
-	if di[1] != "/test2" {
65
-		t.Fatalf("Second element is not /test2")
66
-	}
67
-	if di[2] != "/a/file/here" {
68
-		t.Fatalf("Third element is not /a/file/here")
69
-	}
70
-	if di[3] != "lastfile" {
71
-		t.Fatalf("Fourth element is not lastfile")
72
-	}
73
-}