Skip to content

Commit c5e76db

Browse files
committed
chore: simplify dockerfile
Signed-off-by: Vasek - Tom C <tom@quartz.technology>
1 parent 0d0882b commit c5e76db

File tree

1 file changed

+54
-62
lines changed

1 file changed

+54
-62
lines changed

docker_sdk/src/dockerfile/dockerfile.go

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import (
1010

1111
type Dockerfile struct {
1212
filename string
13-
content *parser.Result
13+
content *parser.Result
14+
15+
stages []string
16+
args map[string]string
17+
secrets []string
1418
}
1519

1620
func New(filename string, file *os.File) (*Dockerfile, error) {
@@ -19,9 +23,53 @@ func New(filename string, file *os.File) (*Dockerfile, error) {
1923
return nil, err
2024
}
2125

26+
stages := []string{}
27+
args := map[string]string{}
28+
secrets := []string{}
29+
30+
for _, child := range content.AST.Children {
31+
switch child.Value {
32+
case "FROM":
33+
args := []string{}
34+
for next := child.Next; next != nil; next = next.Next {
35+
args = append(args, next.Value)
36+
}
37+
38+
// We skip if there's no stage defined
39+
if len(args) != 3 {
40+
continue
41+
}
42+
43+
stages = append(stages, args[2])
44+
case "ARG":
45+
// Args does not handle self interpolation for simplicity.
46+
// TODO: handle self interpolation (ARG XXX="XX-${XXXX}")
47+
entry := strings.SplitN(child.Next.Value, "=", 2)
48+
switch len(entry) {
49+
case 1:
50+
args[entry[0]] = ""
51+
case 2:
52+
args[entry[0]] = entry[1]
53+
default:
54+
panic(fmt.Errorf("invalid ARG: %s", child.Next.Value))
55+
}
56+
case "RUN":
57+
for _, flag := range child.Flags {
58+
if !strings.Contains(flag, "--mount=type=secret,id=") {
59+
continue
60+
}
61+
62+
secrets = append(secrets, strings.TrimPrefix(flag, "--mount=type=secret,id="))
63+
}
64+
}
65+
}
66+
2267
return &Dockerfile{
2368
filename: filename,
24-
content: content,
69+
content: content,
70+
stages: stages,
71+
args: args,
72+
secrets: secrets,
2573
}, nil
2674
}
2775

@@ -30,71 +78,15 @@ func (d *Dockerfile) Filename() string {
3078
}
3179

3280
func (d *Dockerfile) Stages() []string {
33-
stages := []string{}
34-
35-
for _, child := range d.content.AST.Children {
36-
if child.Value != "FROM" {
37-
continue
38-
}
39-
40-
args := []string{}
41-
for next := child.Next; next != nil; next = next.Next {
42-
args = append(args, next.Value)
43-
}
44-
45-
// We skip if there's no stage defined
46-
if len(args) != 3 {
47-
continue
48-
}
49-
50-
stages = append(stages, args[2])
51-
}
52-
53-
return stages
81+
return d.stages
5482
}
5583

56-
// Args does not handle self interpolation for simplicity.
57-
// TODO: handle self interpolation (ARG XXX="XX-${XXXX}")
5884
func (d *Dockerfile) Args() map[string]string {
59-
args := map[string]string{}
60-
61-
for _, child := range d.content.AST.Children {
62-
if child.Value != "ARG" {
63-
continue
64-
}
65-
66-
entry := strings.SplitN(child.Next.Value, "=", 2)
67-
switch len(entry) {
68-
case 1:
69-
args[entry[0]] = ""
70-
case 2:
71-
args[entry[0]] = entry[1]
72-
default:
73-
panic(fmt.Errorf("invalid ARG: %s", child.Next.Value))
74-
}
75-
}
76-
77-
return args
85+
return d.args
7886
}
7987

8088
func (d *Dockerfile) Secrets() []string {
81-
secrets := []string{}
82-
83-
for _, child := range d.content.AST.Children {
84-
if child.Value != "RUN" {
85-
continue
86-
}
87-
88-
for _, flag := range child.Flags {
89-
if !strings.Contains(flag, "--mount=type=secret,id=") {
90-
continue
91-
}
92-
93-
secrets = append(secrets, strings.TrimPrefix(flag, "--mount=type=secret,id="))
94-
}
95-
}
96-
97-
return secrets
89+
return d.secrets
9890
}
9991

10092
func (d *Dockerfile) String() string {
@@ -122,4 +114,4 @@ func (d *Dockerfile) String() string {
122114
}
123115

124116
return result
125-
}
117+
}

0 commit comments

Comments
 (0)