-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtemplate_data.go
306 lines (240 loc) · 8.12 KB
/
template_data.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package dockerfilegenerator
import (
"fmt"
"strings"
)
// RunForm specifies in which form the instruction string should be constructed.
// Check Dockerfile exec and shell forms for more information
type RunForm string
const (
// ExecForm is essentially a json array of string, e.g. ["echo", "1"]
ExecForm RunForm = "ExecForm"
// ShellForm is the form of a usual terminal command, e.g. echo 1
ShellForm RunForm = "ShellForm"
// RunCommandDefaultRunForm is the default RunForm for RunCommand
RunCommandDefaultRunForm = ShellForm
// CmdDefaultRunForm is the default RunForm for Cmd
CmdDefaultRunForm = ExecForm
// EntrypointDefaultRunForm is the default RunForm for Entrypoint
EntrypointDefaultRunForm = ExecForm
)
// Instruction represents a Dockerfile instruction, e.g. FROM alpine:latest
type Instruction interface {
Render() string
}
// DockerfileData struct can hold multiple stages for a multi-staged Dockerfile
// Check https://docs.docker.com/develop/develop-images/multistage-build/ for more information
type DockerfileData struct {
Stages []Stage `yaml:"stages,omitempty"`
}
// Stage is a set of instructions, the purpose is to keep the order of the given instructions
// and generate a Dockerfile using the output of these instructions
type Stage []Instruction
// UnmarshalYAML implements an interface to let go-yaml be able to decode Stages in to Stage struct
func (s *Stage) UnmarshalYAML(unmarshal func(interface{}) error) error {
var data []interface{}
var result []Instruction
err := unmarshal(&data)
if err != nil {
return err
}
*s = append(result, cleanUpInterfaceArray(data)...)
return nil
}
// Arg represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#arg
type Arg struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
Test bool `yaml:"test,omitempty"`
EnvVariable bool `yaml:"envVariable,omitempty"`
}
// Render returns a string in the form of ARG <name>[=<default value>]
func (a Arg) Render() string {
res := fmt.Sprintf("ARG %s", a.Name)
if a.Value != "" {
res = fmt.Sprintf("%s=%s", res, a.Value)
}
if a.Test {
res = fmt.Sprintf("%s\nRUN test -n \"${%s}\"", res, a.Name)
}
if a.EnvVariable {
res = fmt.Sprintf("%s\nENV %s=\"${%s}\"", res, a.Name, a.Name)
}
return res
}
// From represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#from
type From struct {
Image string `yaml:"image"`
As string `yaml:"as"`
}
// Render returns a string in the form of FROM <image> [AS <name>]
func (f From) Render() string {
res := fmt.Sprintf("FROM %s", f.Image)
if f.As != "" {
res = fmt.Sprintf("%s as %s", res, f.As)
}
return res
}
// Label represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#from
type Label struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
// Render returns a string in the form of LABEL <key>=<value>
func (l Label) Render() string {
return fmt.Sprintf("LABEL %s=%s", l.Name, l.Value)
}
// Volume represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#volume
type Volume struct {
Source string `yaml:"source"`
Destination string `yaml:"destination"`
}
// Render returns a string in the form of VOLUME <source> <destination>
func (v Volume) Render() string {
return fmt.Sprintf("VOLUME %s %s", v.Source, v.Destination)
}
// RunCommand represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#run
type RunCommand struct {
Params `yaml:"params"`
RunForm `yaml:"runForm"`
}
// Render returns a string in the form of RUN <command>
func (r RunCommand) Render() string {
if r.RunForm == "" {
r.RunForm = RunCommandDefaultRunForm
}
if r.RunForm == ExecForm {
return fmt.Sprintf("RUN %s", r.ExecForm())
}
return fmt.Sprintf("RUN %s", r.ShellForm())
}
// EnvVariable represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#env
type EnvVariable struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
// Render returns a string in the form of ENV <key> <value>
func (e EnvVariable) Render() string {
return fmt.Sprintf("ENV %s=%s", e.Name, e.Value)
}
// CopyCommand represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#copy
type CopyCommand struct {
Sources []string `yaml:"sources"`
Destination string `yaml:"destination"`
Chown string `yaml:"chown"`
From string `yaml:"from"`
}
// Render returns a string in the form of COPY [--chown=<user>:<group>] <src>... <dest>
func (c CopyCommand) Render() string {
res := "COPY"
if c.From != "" {
res = fmt.Sprintf("%s --from=%s", res, c.From)
}
if c.Chown != "" {
res = fmt.Sprintf("%s --chown=%s", res, c.Chown)
}
sources := strings.Join(c.Sources, " ")
res = fmt.Sprintf("%s %s %s", res, sources, c.Destination)
return res
}
// Cmd represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#cmd
type Cmd struct {
Params `yaml:"params"`
RunForm `yaml:"runForm"`
}
// Render returns a string in the form of CMD ["executable","param1","param2"]
func (c Cmd) Render() string {
if c.RunForm == "" {
c.RunForm = ExecForm
}
if c.RunForm == ExecForm {
return fmt.Sprintf("CMD %s", c.ExecForm())
}
return fmt.Sprintf("CMD %s", c.ShellForm())
}
// Entrypoint represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#entrypoint
type Entrypoint struct {
Params `yaml:"params"`
RunForm `yaml:"runForm"`
}
// Render returns a string in the form of ENTRYPOINT ["executable", "param1", "param2"]
func (e Entrypoint) Render() string {
if e.RunForm == "" {
e.RunForm = ExecForm
}
if e.RunForm == ExecForm {
return fmt.Sprintf("ENTRYPOINT %s", e.ExecForm())
}
return fmt.Sprintf("ENTRYPOINT %s", e.ShellForm())
}
// Onbuild represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#onbuuild
type Onbuild struct {
Params `yaml:"params"`
}
// Render returns a string in the form of ONBUILD [INSTRUCTION]
func (o Onbuild) Render() string {
return fmt.Sprintf("ONBUILD %s", o.ShellForm())
}
// HealthCheck represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#healthcheck
type HealthCheck struct {
Params `yaml:"params"`
}
// Render returns a string in the form of HEALTHCHECK [OPTIONS] CMD command
func (h HealthCheck) Render() string {
return fmt.Sprintf("HEALTHCHECK %s", h.ShellForm())
}
// Shell represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#shell
type Shell struct {
Params `yaml:"params"`
}
// Render returns a string in the form of SHELL ["executable", "parameters"]
func (s Shell) Render() string {
return fmt.Sprintf("SHELL %s", s.ExecForm())
}
// Workdir represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#workdir
type Workdir struct {
Dir string `yaml:"dir"`
}
// Render returns a string in the form of WORKDIR /path/to/workdir
func (w Workdir) Render() string {
return fmt.Sprintf("WORKDIR %s", w.Dir)
}
// User represents a Dockerfile instruction, see https://docs.docker.com/engine/reference/builder/#user
type User struct {
User string `yaml:user`
Group string `yaml:group`
}
// Render returns a string in the form of WORKDIR /path/to/workdir
func (u User) Render() string {
res := "USER"
if u.User == "" && u.Group == "" {
return ""
}
res = fmt.Sprintf("%s %s", res, u.User)
if u.Group != "" {
res = fmt.Sprintf("%s:%s", res, u.Group)
}
return res
}
// Params is struct that supports rendering exec and shell form string
type Params []string
func (p Params) mapParams(f func(string) string) []string {
res := make([]string, len(p))
for i, v := range p {
res[i] = f(v)
}
return res
}
// ExecForm joins params slice in exec form
func (p Params) ExecForm() string {
params := p.mapParams(func(s string) string {
return fmt.Sprintf("\"%s\"", s)
})
paramsString := strings.Join(params, ", ")
execFormString := fmt.Sprintf("[%s]", paramsString)
return execFormString
}
// ShellForm joins params slice in shell form
func (p Params) ShellForm() string {
return strings.Join(p, " ")
}