-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (68 loc) · 1.66 KB
/
main.go
File metadata and controls
80 lines (68 loc) · 1.66 KB
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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func isMultiStage(lines []string) bool {
fromCount := 0
for _, line := range lines {
if strings.HasPrefix(strings.TrimSpace(line), "FROM ") {
fromCount++
}
}
return fromCount > 1
}
func usesBaseImage(lines []string, baseImage string) bool {
lastStageIndex := 0
for idx, line := range lines {
if strings.HasPrefix(strings.TrimSpace(line), "FROM ") {
lastStageIndex = idx
}
}
return strings.Contains(strings.TrimSpace(lines[lastStageIndex]), baseImage)
}
func hasInlineComments(lines []string) bool {
for _, line := range lines {
if !strings.Contains(line, "#") {
return false
}
}
return true
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <path-to-dockerfile>")
return
}
filePath := os.Args[1]
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Error:", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if isMultiStage(lines) {
fmt.Println("Dockerfile is multi-stage.")
} else {
fmt.Println("Dockerfile is not multi-stage.")
}
if usesBaseImage(lines, "scratch") {
fmt.Println("Dockerfile uses scratch as the final base image.")
}
if usesBaseImage(lines, "alpine") {
fmt.Println("Dockerfile uses alpine as the final base image.")
}
// Add more checks for other base images like wolfi-linux, golang, node, python, R, JavaScript, Bun, Rust, C, and Lua
if hasInlineComments(lines) {
fmt.Println("Dockerfile has inline comments for each line.")
} else {
fmt.Println("Dockerfile is missing inline comments for some lines.")
}
}