Skip to content

Commit b11a2ba

Browse files
author
Minori Tokuda
committed
initial commit
0 parents  commit b11a2ba

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/minoritea/docker-list-stages
2+
3+
go 1.16

main.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io"
7+
"log"
8+
"os"
9+
"regexp"
10+
)
11+
12+
func main() {
13+
err := run()
14+
if err != nil {
15+
log.Fatal(err)
16+
}
17+
}
18+
19+
func run() error {
20+
dockerfile := "Dockerfile"
21+
if len(os.Args) >= 2 {
22+
dockerfile = os.Args[1]
23+
}
24+
25+
f, err := os.Open(dockerfile)
26+
if err != nil {
27+
return fmt.Errorf("cannot open Dockerfile(%s): %w)", dockerfile, err)
28+
}
29+
defer f.Close()
30+
31+
stages, err := parse(f)
32+
if err != nil {
33+
return fmt.Errorf("Failed to parse Dockerfile(%s): %w)", dockerfile, err)
34+
}
35+
36+
for _, stage := range stages {
37+
fmt.Println(stage)
38+
}
39+
return nil
40+
}
41+
42+
var matcher = regexp.MustCompile(`^(FROM|from) \w+ (AS|as) (\w+)$`)
43+
44+
func parse(r io.Reader) ([]string, error) {
45+
scanner := bufio.NewScanner(r)
46+
var result []string
47+
for scanner.Scan() {
48+
line := scanner.Bytes()
49+
m := matcher.FindAllSubmatch(line, -1)
50+
if len(m) > 0 && len(m[0]) > 3 {
51+
result = append(result, string(m[0][3]))
52+
}
53+
}
54+
return result, scanner.Err()
55+
}

0 commit comments

Comments
 (0)