-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
dockerignore.go
47 lines (40 loc) · 1.37 KB
/
dockerignore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package build
import (
"fmt"
"os"
"path/filepath"
"github.com/moby/patternmatcher"
"github.com/moby/patternmatcher/ignorefile"
)
// ReadDockerignore reads the .dockerignore file in the context directory and
// returns the list of paths to exclude
func ReadDockerignore(contextDir string) ([]string, error) {
var excludes []string
f, err := os.Open(filepath.Join(contextDir, ".dockerignore"))
switch {
case os.IsNotExist(err):
return excludes, nil
case err != nil:
return nil, err
}
defer f.Close()
patterns, err := ignorefile.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("error reading .dockerignore: %w", err)
}
return patterns, nil
}
// TrimBuildFilesFromExcludes removes the named Dockerfile and .dockerignore from
// the list of excluded files. The daemon will remove them from the final context
// but they must be in available in the context when passed to the API.
func TrimBuildFilesFromExcludes(excludes []string, dockerfile string, dockerfileFromStdin bool) []string {
if keep, _ := patternmatcher.Matches(".dockerignore", excludes); keep {
excludes = append(excludes, "!.dockerignore")
}
// canonicalize dockerfile name to be platform-independent.
dockerfile = filepath.ToSlash(dockerfile)
if keep, _ := patternmatcher.Matches(dockerfile, excludes); keep && !dockerfileFromStdin {
excludes = append(excludes, "!"+dockerfile)
}
return excludes
}