-
Notifications
You must be signed in to change notification settings - Fork 7
/
stage.go
181 lines (159 loc) · 3.61 KB
/
stage.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package stage
import (
"github.com/combust-labs/firebuild-shared/build/commands"
bcErrors "github.com/combust-labs/firebuild/pkg/build/errors"
)
// ReadStages reads the stages out of the source commands.
func ReadStages(inputs []interface{}) (Stages, []error) {
stages := newStages()
errs := []error{}
danglingCommands := []interface{}{}
for _, input := range inputs {
switch input.(type) {
case commands.From:
// a FROM command resets the processing stage
stages.closePrevious()
stages.setCurrent(newEmptyStage())
for _, danglingCommand := range danglingCommands {
stages.addCommand(danglingCommand)
}
stages.addCommand(input)
default:
if !stages.addCommand(input) {
// if there is an ARG, ENV or LABEL prior to any FROM,
// remember it and add these to any further stage first
switch input.(type) {
case commands.Arg:
danglingCommands = append(danglingCommands, input)
case commands.Env:
danglingCommands = append(danglingCommands, input)
case commands.Label:
danglingCommands = append(danglingCommands, input)
default:
errs = append(errs, &bcErrors.CommandOutOfScopeError{Command: input})
}
}
}
}
stages.closePrevious()
return stages, errs
}
// Stages represents all build stages parsed out of the Dockerfile.
// Items are ordered accordingly to how they are defnied in the Dockerfile.
type Stages interface {
addCommand(interface{}) bool
setCurrent(Stage)
closePrevious()
// Public interface:
All() []Stage
NamedStage(string) Stage
Named() []Stage
Unnamed() []Stage
}
type stages struct {
current Stage
stages []Stage
}
func newStages() Stages {
return &stages{
stages: []Stage{},
}
}
func (ps *stages) addCommand(command interface{}) bool {
if ps.current != nil {
ps.current.addCommand(command)
return true
}
return false
}
func (ps *stages) closePrevious() {
if ps.current != nil {
ps.stages = append(ps.stages, ps.current)
ps.current = nil
}
}
func (ps *stages) setCurrent(s Stage) {
ps.current = s
}
func (ps *stages) All() []Stage {
return ps.stages
}
func (ps *stages) NamedStage(scopeName string) Stage {
for _, s := range ps.stages {
if s.Name() == scopeName {
return s
}
}
return nil
}
func (ps *stages) Named() (scs []Stage) {
for _, s := range ps.stages {
if s.IsNamed() {
scs = append(scs, s)
}
}
return scs
}
func (ps *stages) Unnamed() (scs []Stage) {
for _, s := range ps.stages {
if !s.IsNamed() {
scs = append(scs, s)
}
}
return scs
}
// Stage represents a single FROM with dependent commands.
type Stage interface {
addCommand(interface{})
// Public interface:
Commands() []interface{}
DependsOn() []string
IsNamed() bool
IsValid() bool
Name() string
}
type stage struct {
commands []interface{}
dependsOn map[string]bool
hasValidFrom bool
name string
}
func newEmptyStage() Stage {
return &stage{
commands: []interface{}{},
dependsOn: map[string]bool{},
}
}
func (ps *stage) addCommand(cmd interface{}) {
switch tcmd := cmd.(type) {
case commands.From:
ps.name = tcmd.StageName
ps.hasValidFrom = tcmd.BaseImage != ""
case commands.Copy:
if tcmd.Stage != "" {
ps.dependsOn[tcmd.Stage] = true
}
}
ps.commands = append(ps.commands, cmd)
}
func (ps *stage) Commands() []interface{} {
return ps.commands
}
func (ps *stage) DependsOn() []string {
return func() []string {
keys := []string{}
for k := range ps.dependsOn {
keys = append(keys, k)
}
return keys
}()
}
func (ps *stage) IsNamed() bool {
return ps.name != ""
}
func (ps *stage) IsValid() bool {
return ps.hasValidFrom
}
func (ps *stage) Name() string {
return ps.name
}