-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.go
114 lines (101 loc) · 2.74 KB
/
scan.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package scan
import (
"io/fs"
"os"
"path"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/backplane/contextdir/files"
"github.com/backplane/contextdir/utils"
"github.com/moby/patternmatcher"
)
const (
DefaultDockerfileName = "Dockerfile"
DefaultDockerignoreName = ".dockerignore"
)
type ScanResults struct {
Dir *files.Directory
DockerIgnoreEntries []string
IgnoredFiles []*files.File
FoundDockerfile bool
FoundDockerignore bool
}
func ScanContextDir(contextDir string) (*ScanResults, error) {
var results *ScanResults = &ScanResults{
DockerIgnoreEntries: make([]string, 0),
IgnoredFiles: make([]*files.File, 0),
}
// handle the special files in the root of the context, like .dockerignore and Dockerfile
rootFiles, err := os.ReadDir(contextDir)
if err != nil {
return nil, err
}
for _, dirEnt := range rootFiles {
if !dirEnt.Type().IsRegular() {
continue
}
switch dirEnt.Name() {
case DefaultDockerfileName:
results.FoundDockerfile = true
case DefaultDockerignoreName:
results.FoundDockerignore = true
log.Debugf("context dir \"%s\" contains %s; parsing it", contextDir, dirEnt.Name())
results.DockerIgnoreEntries, err = utils.LoadDockerIgnore(path.Join(contextDir, dirEnt.Name()))
if err != nil {
return nil, err
}
}
}
if !results.FoundDockerfile {
log.Infof("context dir \"%s\" does not contain a file named %s", contextDir, DefaultDockerfileName)
}
if !results.FoundDockerignore {
log.Debugf("context dir \"%s\" does not contain a file named %s", contextDir, DefaultDockerignoreName)
}
results.Dir = files.NewDirectory(contextDir)
// walk the context dir, excluding files as requested in the ignore files
err = filepath.WalkDir(contextDir, func(fullPath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(contextDir, fullPath)
if err != nil {
return err
}
if relPath == "." {
return nil
}
fileInfo, err := d.Info()
if err != nil {
return err
}
f, err := files.NewFile(fullPath, relPath, fileInfo)
if err != nil {
return err
}
if len(results.DockerIgnoreEntries) > 0 {
matched, err := patternmatcher.MatchesOrParentMatches(relPath, results.DockerIgnoreEntries)
if err != nil {
return err
}
if matched {
results.IgnoredFiles = append(results.IgnoredFiles, f)
if d.IsDir() {
log.Debugf("skipping excluded directory: %q\n", relPath)
return filepath.SkipDir
}
log.Debugf("debug: skipping excluded file: %q\n", relPath)
return nil
}
}
if err := results.Dir.Append(f); err != nil {
return err
}
return nil
})
// checking error from filepath.WalkDir
if err != nil {
return nil, err
}
return results, nil
}