/
app_build_config.go
255 lines (213 loc) · 5.33 KB
/
app_build_config.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
package domain
import (
"fmt"
"strings"
"github.com/friendsofgo/errors"
"github.com/google/shlex"
"github.com/samber/lo"
)
type BuildType int
const (
BuildTypeRuntimeBuildpack BuildType = iota
BuildTypeRuntimeCmd
BuildTypeRuntimeDockerfile
BuildTypeStaticBuildpack
BuildTypeStaticCmd
BuildTypeStaticDockerfile
)
func (b BuildType) DeployType() DeployType {
switch b {
case BuildTypeRuntimeBuildpack, BuildTypeRuntimeCmd, BuildTypeRuntimeDockerfile:
return DeployTypeRuntime
case BuildTypeStaticBuildpack, BuildTypeStaticCmd, BuildTypeStaticDockerfile:
return DeployTypeStatic
default:
panic(fmt.Sprintf("unknown build type: %v", b))
}
}
type RuntimeConfig struct {
UseMariaDB bool
UseMongoDB bool
Entrypoint string
Command string
}
const shellSpecialCharacters = "`" + `~#$&*()\|[]{};'"<>?!=`
func ParseArgs(s string) ([]string, error) {
if s == "" {
return nil, nil
}
args, err := shlex.Split(s)
if err != nil {
return nil, errors.Wrap(err, "cannot parse command")
}
shellSyntax := lo.ContainsBy(args, func(arg string) bool {
return strings.ContainsAny(arg, shellSpecialCharacters)
})
if shellSyntax {
return []string{"sh", "-c", s}, nil
}
return args, nil
}
func (rc *RuntimeConfig) Validate() error {
if _, err := ParseArgs(rc.Entrypoint); err != nil {
return errors.Wrap(err, "entrypoint")
}
if _, err := ParseArgs(rc.Command); err != nil {
return errors.Wrap(err, "command")
}
return nil
}
func (rc *RuntimeConfig) MariaDB() bool {
return rc.UseMariaDB
}
func (rc *RuntimeConfig) MongoDB() bool {
return rc.UseMongoDB
}
func (rc *RuntimeConfig) GetRuntimeConfig() RuntimeConfig {
return *rc
}
func (rc *RuntimeConfig) GetStaticConfig() StaticConfig {
panic("not static config")
}
type StaticConfig struct {
ArtifactPath string
SPA bool
}
func (sc *StaticConfig) Validate() error {
if sc.ArtifactPath == "" {
return errors.New("artifact_path is required for static builds")
}
return nil
}
func (sc *StaticConfig) MariaDB() bool {
return false
}
func (sc *StaticConfig) MongoDB() bool {
return false
}
func (sc *StaticConfig) GetRuntimeConfig() RuntimeConfig {
panic("not runtime config")
}
func (sc *StaticConfig) GetStaticConfig() StaticConfig {
return *sc
}
type BuildConfig interface {
isBuildConfig()
BuildType() BuildType
Validate() error
MariaDB() bool
MongoDB() bool
GetRuntimeConfig() RuntimeConfig
GetStaticConfig() StaticConfig
}
type buildConfigEmbed struct{}
func (buildConfigEmbed) isBuildConfig() {}
type BuildConfigRuntimeBuildpack struct {
RuntimeConfig
Context string
buildConfigEmbed
}
func (bc *BuildConfigRuntimeBuildpack) BuildType() BuildType {
return BuildTypeRuntimeBuildpack
}
func (bc *BuildConfigRuntimeBuildpack) Validate() error {
if err := bc.RuntimeConfig.Validate(); err != nil {
return err
}
// NOTE: context is not necessary
return nil
}
type BuildConfigRuntimeCmd struct {
RuntimeConfig
BaseImage string
BuildCmd string
buildConfigEmbed
}
func (bc *BuildConfigRuntimeCmd) BuildType() BuildType {
return BuildTypeRuntimeCmd
}
func (bc *BuildConfigRuntimeCmd) Validate() error {
if err := bc.RuntimeConfig.Validate(); err != nil {
return err
}
// NOTE: Base image could have no entrypoint/command but is impossible to catch only from config
if bc.BaseImage == "" && bc.Entrypoint == "" && bc.Command == "" {
return errors.New("entrypoint or command is required")
}
// NOTE: base image is not necessary (default: scratch)
// NOTE: build cmd is not necessary
return nil
}
type BuildConfigRuntimeDockerfile struct {
RuntimeConfig
DockerfileName string
Context string
buildConfigEmbed
}
func (bc *BuildConfigRuntimeDockerfile) BuildType() BuildType {
return BuildTypeRuntimeDockerfile
}
func (bc *BuildConfigRuntimeDockerfile) Validate() error {
if err := bc.RuntimeConfig.Validate(); err != nil {
return err
}
if bc.DockerfileName == "" {
return errors.New("dockerfile_name is required")
}
// NOTE: Runtime Dockerfile build could have no entrypoint/command but is impossible to catch only from config
// (can only catch at runtime)
return nil
}
type BuildConfigStaticBuildpack struct {
StaticConfig
Context string
buildConfigEmbed
}
func (bc *BuildConfigStaticBuildpack) BuildType() BuildType {
return BuildTypeStaticBuildpack
}
func (bc *BuildConfigStaticBuildpack) Validate() error {
if err := bc.StaticConfig.Validate(); err != nil {
return err
}
// NOTE: context is not necessary
return nil
}
type BuildConfigStaticCmd struct {
StaticConfig
BaseImage string
BuildCmd string
buildConfigEmbed
}
func (bc *BuildConfigStaticCmd) BuildType() BuildType {
return BuildTypeStaticCmd
}
func (bc *BuildConfigStaticCmd) Validate() error {
if err := bc.StaticConfig.Validate(); err != nil {
return err
}
// NOTE: base image is not necessary (default: scratch)
// NOTE: build cmd is not necessary
if bc.ArtifactPath == "" {
return errors.New("artifact_path is required")
}
return nil
}
type BuildConfigStaticDockerfile struct {
StaticConfig
DockerfileName string
Context string
buildConfigEmbed
}
func (bc *BuildConfigStaticDockerfile) BuildType() BuildType {
return BuildTypeStaticDockerfile
}
func (bc *BuildConfigStaticDockerfile) Validate() error {
if err := bc.StaticConfig.Validate(); err != nil {
return err
}
if bc.DockerfileName == "" {
return errors.New("dockerfile_name is required")
}
return nil
}