From 63e3816c1dd449de63500a2b5fec9c2a33a0894c Mon Sep 17 00:00:00 2001 From: Tibor Vass Date: Mon, 14 Dec 2015 11:15:00 +0100 Subject: [PATCH] utils: move dockerignore function to builder/dockerignore Signed-off-by: Tibor Vass --- api/client/build.go | 3 +- builder/dockerignore.go | 6 +-- builder/dockerignore/dockerignore.go | 34 ++++++++++++++ builder/dockerignore/dockerignore_test.go | 55 +++++++++++++++++++++++ utils/utils.go | 26 ----------- utils/utils_test.go | 54 +--------------------- 6 files changed, 95 insertions(+), 83 deletions(-) create mode 100644 builder/dockerignore/dockerignore.go create mode 100644 builder/dockerignore/dockerignore_test.go diff --git a/api/client/build.go b/api/client/build.go index 8b1b4fc57f..112a073a88 100644 --- a/api/client/build.go +++ b/api/client/build.go @@ -16,6 +16,7 @@ import ( "github.com/docker/distribution/reference" "github.com/docker/docker/api" "github.com/docker/docker/api/types" + "github.com/docker/docker/builder/dockerignore" Cli "github.com/docker/docker/cli" "github.com/docker/docker/opts" "github.com/docker/docker/pkg/archive" @@ -132,7 +133,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error { var excludes []string if err == nil { - excludes, err = utils.ReadDockerIgnore(f) + excludes, err = dockerignore.ReadAll(f) if err != nil { return err } diff --git a/builder/dockerignore.go b/builder/dockerignore.go index 7630274c99..2990770a4a 100644 --- a/builder/dockerignore.go +++ b/builder/dockerignore.go @@ -3,8 +3,8 @@ package builder import ( "os" + "github.com/docker/docker/builder/dockerignore" "github.com/docker/docker/pkg/fileutils" - "github.com/docker/docker/utils" ) // DockerIgnoreContext wraps a ModifiableContext to add a method @@ -27,7 +27,7 @@ type DockerIgnoreContext struct { // TODO: Don't require a ModifiableContext (use Context instead) and don't remove // files, instead handle a list of files to be excluded from the context. func (c DockerIgnoreContext) Process(filesToRemove []string) error { - dockerignore, err := c.Open(".dockerignore") + f, err := c.Open(".dockerignore") // Note that a missing .dockerignore file isn't treated as an error if err != nil { if os.IsNotExist(err) { @@ -35,7 +35,7 @@ func (c DockerIgnoreContext) Process(filesToRemove []string) error { } return err } - excludes, _ := utils.ReadDockerIgnore(dockerignore) + excludes, _ := dockerignore.ReadAll(f) filesToRemove = append([]string{".dockerignore"}, filesToRemove...) for _, fileToRemove := range filesToRemove { rm, _ := fileutils.Matches(fileToRemove, excludes) diff --git a/builder/dockerignore/dockerignore.go b/builder/dockerignore/dockerignore.go new file mode 100644 index 0000000000..8170f1ecf1 --- /dev/null +++ b/builder/dockerignore/dockerignore.go @@ -0,0 +1,34 @@ +package dockerignore + +import ( + "bufio" + "fmt" + "io" + "path/filepath" + "strings" +) + +// ReadAll reads a .dockerignore file and returns the list of file patterns +// to ignore. Note this will trim whitespace from each line as well +// as use GO's "clean" func to get the shortest/cleanest path for each. +func ReadAll(reader io.ReadCloser) ([]string, error) { + if reader == nil { + return nil, nil + } + defer reader.Close() + scanner := bufio.NewScanner(reader) + var excludes []string + + for scanner.Scan() { + pattern := strings.TrimSpace(scanner.Text()) + if pattern == "" { + continue + } + pattern = filepath.Clean(pattern) + excludes = append(excludes, pattern) + } + if err := scanner.Err(); err != nil { + return nil, fmt.Errorf("Error reading .dockerignore: %v", err) + } + return excludes, nil +} diff --git a/builder/dockerignore/dockerignore_test.go b/builder/dockerignore/dockerignore_test.go new file mode 100644 index 0000000000..361b041912 --- /dev/null +++ b/builder/dockerignore/dockerignore_test.go @@ -0,0 +1,55 @@ +package dockerignore + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "testing" +) + +func TestReadAll(t *testing.T) { + tmpDir, err := ioutil.TempDir("", "dockerignore-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + di, err := ReadAll(nil) + if err != nil { + t.Fatalf("Expected not to have error, got %v", err) + } + + if diLen := len(di); diLen != 0 { + t.Fatalf("Expected to have zero dockerignore entry, got %d", diLen) + } + + diName := filepath.Join(tmpDir, ".dockerignore") + content := fmt.Sprintf("test1\n/test2\n/a/file/here\n\nlastfile") + err = ioutil.WriteFile(diName, []byte(content), 0777) + if err != nil { + t.Fatal(err) + } + + diFd, err := os.Open(diName) + if err != nil { + t.Fatal(err) + } + di, err = ReadAll(diFd) + if err != nil { + t.Fatal(err) + } + + if di[0] != "test1" { + t.Fatalf("First element is not test1") + } + if di[1] != "/test2" { + t.Fatalf("Second element is not /test2") + } + if di[2] != "/a/file/here" { + t.Fatalf("Third element is not /a/file/here") + } + if di[3] != "lastfile" { + t.Fatalf("Fourth element is not lastfile") + } +} diff --git a/utils/utils.go b/utils/utils.go index 91246eafce..15d0b1cd73 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,7 +1,6 @@ package utils import ( - "bufio" "crypto/sha1" "encoding/hex" "fmt" @@ -244,31 +243,6 @@ func ValidateContextDirectory(srcPath string, excludes []string) error { }) } -// ReadDockerIgnore reads a .dockerignore file and returns the list of file patterns -// to ignore. Note this will trim whitespace from each line as well -// as use GO's "clean" func to get the shortest/cleanest path for each. -func ReadDockerIgnore(reader io.ReadCloser) ([]string, error) { - if reader == nil { - return nil, nil - } - defer reader.Close() - scanner := bufio.NewScanner(reader) - var excludes []string - - for scanner.Scan() { - pattern := strings.TrimSpace(scanner.Text()) - if pattern == "" { - continue - } - pattern = filepath.Clean(pattern) - excludes = append(excludes, pattern) - } - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("Error reading .dockerignore: %v", err) - } - return excludes, nil -} - // GetErrorMessage returns the human readable message associated with // the passed-in error. In some cases the default Error() func returns // something that is less than useful so based on its types this func diff --git a/utils/utils_test.go b/utils/utils_test.go index 3dc5af88af..ab3911e8b3 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -1,12 +1,6 @@ package utils -import ( - "fmt" - "io/ioutil" - "os" - "path/filepath" - "testing" -) +import "testing" func TestReplaceAndAppendEnvVars(t *testing.T) { var ( @@ -25,49 +19,3 @@ func TestReplaceAndAppendEnvVars(t *testing.T) { t.Fatalf("expected TERM=xterm got '%s'", env[1]) } } - -func TestReadDockerIgnore(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "dockerignore-test") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) - - di, err := ReadDockerIgnore(nil) - if err != nil { - t.Fatalf("Expected not to have error, got %v", err) - } - - if diLen := len(di); diLen != 0 { - t.Fatalf("Expected to have zero dockerignore entry, got %d", diLen) - } - - diName := filepath.Join(tmpDir, ".dockerignore") - content := fmt.Sprintf("test1\n/test2\n/a/file/here\n\nlastfile") - err = ioutil.WriteFile(diName, []byte(content), 0777) - if err != nil { - t.Fatal(err) - } - - diFd, err := os.Open(diName) - if err != nil { - t.Fatal(err) - } - di, err = ReadDockerIgnore(diFd) - if err != nil { - t.Fatal(err) - } - - if di[0] != "test1" { - t.Fatalf("First element is not test1") - } - if di[1] != "/test2" { - t.Fatalf("Second element is not /test2") - } - if di[2] != "/a/file/here" { - t.Fatalf("Third element is not /a/file/here") - } - if di[3] != "lastfile" { - t.Fatalf("Fourth element is not lastfile") - } -}