Skip to content

Commit 63e3816

Browse files
author
Tibor Vass
committed
utils: move dockerignore function to builder/dockerignore
Signed-off-by: Tibor Vass <tibor@docker.com>
1 parent 135cca6 commit 63e3816

File tree

6 files changed

+95
-83
lines changed

6 files changed

+95
-83
lines changed

api/client/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/docker/distribution/reference"
1717
"github.com/docker/docker/api"
1818
"github.com/docker/docker/api/types"
19+
"github.com/docker/docker/builder/dockerignore"
1920
Cli "github.com/docker/docker/cli"
2021
"github.com/docker/docker/opts"
2122
"github.com/docker/docker/pkg/archive"
@@ -132,7 +133,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
132133

133134
var excludes []string
134135
if err == nil {
135-
excludes, err = utils.ReadDockerIgnore(f)
136+
excludes, err = dockerignore.ReadAll(f)
136137
if err != nil {
137138
return err
138139
}

builder/dockerignore.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package builder
33
import (
44
"os"
55

6+
"github.com/docker/docker/builder/dockerignore"
67
"github.com/docker/docker/pkg/fileutils"
7-
"github.com/docker/docker/utils"
88
)
99

1010
// DockerIgnoreContext wraps a ModifiableContext to add a method
@@ -27,15 +27,15 @@ type DockerIgnoreContext struct {
2727
// TODO: Don't require a ModifiableContext (use Context instead) and don't remove
2828
// files, instead handle a list of files to be excluded from the context.
2929
func (c DockerIgnoreContext) Process(filesToRemove []string) error {
30-
dockerignore, err := c.Open(".dockerignore")
30+
f, err := c.Open(".dockerignore")
3131
// Note that a missing .dockerignore file isn't treated as an error
3232
if err != nil {
3333
if os.IsNotExist(err) {
3434
return nil
3535
}
3636
return err
3737
}
38-
excludes, _ := utils.ReadDockerIgnore(dockerignore)
38+
excludes, _ := dockerignore.ReadAll(f)
3939
filesToRemove = append([]string{".dockerignore"}, filesToRemove...)
4040
for _, fileToRemove := range filesToRemove {
4141
rm, _ := fileutils.Matches(fileToRemove, excludes)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package dockerignore
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io"
7+
"path/filepath"
8+
"strings"
9+
)
10+
11+
// ReadAll reads a .dockerignore file and returns the list of file patterns
12+
// to ignore. Note this will trim whitespace from each line as well
13+
// as use GO's "clean" func to get the shortest/cleanest path for each.
14+
func ReadAll(reader io.ReadCloser) ([]string, error) {
15+
if reader == nil {
16+
return nil, nil
17+
}
18+
defer reader.Close()
19+
scanner := bufio.NewScanner(reader)
20+
var excludes []string
21+
22+
for scanner.Scan() {
23+
pattern := strings.TrimSpace(scanner.Text())
24+
if pattern == "" {
25+
continue
26+
}
27+
pattern = filepath.Clean(pattern)
28+
excludes = append(excludes, pattern)
29+
}
30+
if err := scanner.Err(); err != nil {
31+
return nil, fmt.Errorf("Error reading .dockerignore: %v", err)
32+
}
33+
return excludes, nil
34+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package dockerignore
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
)
10+
11+
func TestReadAll(t *testing.T) {
12+
tmpDir, err := ioutil.TempDir("", "dockerignore-test")
13+
if err != nil {
14+
t.Fatal(err)
15+
}
16+
defer os.RemoveAll(tmpDir)
17+
18+
di, err := ReadAll(nil)
19+
if err != nil {
20+
t.Fatalf("Expected not to have error, got %v", err)
21+
}
22+
23+
if diLen := len(di); diLen != 0 {
24+
t.Fatalf("Expected to have zero dockerignore entry, got %d", diLen)
25+
}
26+
27+
diName := filepath.Join(tmpDir, ".dockerignore")
28+
content := fmt.Sprintf("test1\n/test2\n/a/file/here\n\nlastfile")
29+
err = ioutil.WriteFile(diName, []byte(content), 0777)
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
diFd, err := os.Open(diName)
35+
if err != nil {
36+
t.Fatal(err)
37+
}
38+
di, err = ReadAll(diFd)
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
if di[0] != "test1" {
44+
t.Fatalf("First element is not test1")
45+
}
46+
if di[1] != "/test2" {
47+
t.Fatalf("Second element is not /test2")
48+
}
49+
if di[2] != "/a/file/here" {
50+
t.Fatalf("Third element is not /a/file/here")
51+
}
52+
if di[3] != "lastfile" {
53+
t.Fatalf("Fourth element is not lastfile")
54+
}
55+
}

utils/utils.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package utils
22

33
import (
4-
"bufio"
54
"crypto/sha1"
65
"encoding/hex"
76
"fmt"
@@ -244,31 +243,6 @@ func ValidateContextDirectory(srcPath string, excludes []string) error {
244243
})
245244
}
246245

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-
272246
// GetErrorMessage returns the human readable message associated with
273247
// the passed-in error. In some cases the default Error() func returns
274248
// something that is less than useful so based on its types this func

utils/utils_test.go

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
package utils
22

3-
import (
4-
"fmt"
5-
"io/ioutil"
6-
"os"
7-
"path/filepath"
8-
"testing"
9-
)
3+
import "testing"
104

115
func TestReplaceAndAppendEnvVars(t *testing.T) {
126
var (
@@ -25,49 +19,3 @@ func TestReplaceAndAppendEnvVars(t *testing.T) {
2519
t.Fatalf("expected TERM=xterm got '%s'", env[1])
2620
}
2721
}
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-
}

0 commit comments

Comments
 (0)