-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (47 loc) · 954 Bytes
/
main.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
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"regexp"
)
func main() {
err := run()
if err != nil {
log.Fatal(err)
}
}
func run() error {
dockerfile := "Dockerfile"
if len(os.Args) >= 2 {
dockerfile = os.Args[1]
}
f, err := os.Open(dockerfile)
if err != nil {
return fmt.Errorf("cannot open Dockerfile(%s): %w)", dockerfile, err)
}
defer f.Close()
stages, err := parse(f)
if err != nil {
return fmt.Errorf("Failed to parse Dockerfile(%s): %w)", dockerfile, err)
}
for _, stage := range stages {
fmt.Println(stage)
}
return nil
}
var matcher = regexp.MustCompile(`^(FROM|from) ([\w:.-]+) (AS|as) (\w+)$`)
func parse(r io.Reader) ([]string, error) {
scanner := bufio.NewScanner(r)
var result []string
for scanner.Scan() {
line := scanner.Bytes()
m := matcher.FindAllSubmatch(line, -1)
if len(m) > 0 && len(m[0]) > 4 {
result = append(result, string(m[0][4]))
}
}
return result, scanner.Err()
}