-
Notifications
You must be signed in to change notification settings - Fork 234
/
dockerfile.go
57 lines (44 loc) · 1.11 KB
/
dockerfile.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
package scanner
import (
"os"
"path/filepath"
"regexp"
"strconv"
)
const defaultPort = 8080
var portRegex = regexp.MustCompile(`(?m)^EXPOSE\s+(?P<port>\d+)`)
func configureDockerfile(sourceDir string, config *ScannerConfig) (*SourceInfo, error) {
return ScanDockerfile(filepath.Join(sourceDir, "Dockerfile"), config)
}
func ScanDockerfile(dockerfilePath string, config *ScannerConfig) (*SourceInfo, error) {
if !absFileExists(dockerfilePath) {
return nil, nil
}
var portFromDockerfile int
s := &SourceInfo{
DockerfilePath: dockerfilePath,
Family: "Dockerfile",
Port: config.ExistingPort,
}
dockerfile, err := os.ReadFile(s.DockerfilePath)
if err != nil {
// just maintaining existing behaviour from old code.
return s, nil
}
m := portRegex.FindStringSubmatch(string(dockerfile))
for i, name := range portRegex.SubexpNames() {
if len(m) > 0 && name == "port" {
portFromDockerfile, err = strconv.Atoi(m[i])
if err != nil {
continue
}
}
}
if portFromDockerfile != 0 {
s.Port = portFromDockerfile
}
if s.Port == 0 {
s.Port = defaultPort
}
return s, nil
}