This repository was archived by the owner on Jul 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathtypes.go
More file actions
364 lines (318 loc) · 9.04 KB
/
types.go
File metadata and controls
364 lines (318 loc) · 9.04 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package types
import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
composeloader "github.com/docker/cli/cli/compose/loader"
composetypes "github.com/docker/cli/cli/compose/types"
"github.com/docker/app/internal"
"github.com/docker/app/types/metadata"
"github.com/docker/app/types/parameters"
"github.com/docker/app/types/secrets"
)
// AppSourceKind represents what format the app was in when read
type AppSourceKind int
const (
// AppSourceSplit represents an Application in multiple file format
AppSourceSplit AppSourceKind = iota
// AppSourceImage represents an Application pulled from an image
AppSourceImage
// AppSourceArchive represents an Application in an archive format
AppSourceArchive
)
// ShouldRunInsideDirectory returns whether the package is run from a directory on disk
func (a AppSourceKind) ShouldRunInsideDirectory() bool {
return a == AppSourceSplit || a == AppSourceImage || a == AppSourceArchive
}
// App represents an app
type App struct {
Name string
Path string
Cleanup func()
Source AppSourceKind
composesContent [][]byte
parametersContent [][]byte
parameters parameters.Parameters
secrets secrets.Secrets
metadataContent []byte
metadata metadata.AppMetadata
attachments []Attachment
hasCRLF bool
}
// Attachment is a summary of an attachment (attached file) stored in the app definition
type Attachment struct {
path string
size int64
}
// Path returns the local file path
func (f *Attachment) Path() string {
return f.path
}
// Size returns the file size in bytes
func (f *Attachment) Size() int64 {
return f.size
}
// Composes returns compose files content
func (a *App) Composes() [][]byte {
return a.composesContent
}
// ParametersRaw returns parameter files content
func (a *App) ParametersRaw() [][]byte {
return a.parametersContent
}
// Parameters returns map of parameters
func (a *App) Parameters() parameters.Parameters {
return a.parameters
}
// MetadataRaw returns metadata file content
func (a *App) MetadataRaw() []byte {
return a.metadataContent
}
// Metadata returns the metadata struct
func (a *App) Metadata() metadata.AppMetadata {
return a.metadata
}
// Attachments returns the external files list
func (a *App) Attachments() []Attachment {
return a.attachments
}
// Secrets returns a map of secrets
func (a *App) Secrets() secrets.Secrets {
return a.secrets
}
func (a *App) HasCRLF() bool {
return a.hasCRLF
}
// Extract writes the app in the specified folder
func (a *App) Extract(path string) error {
if err := ioutil.WriteFile(filepath.Join(path, internal.MetadataFileName), a.MetadataRaw(), 0644); err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(path, internal.ComposeFileName), a.Composes()[0], 0644); err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(path, internal.ParametersFileName), a.ParametersRaw()[0], 0644); err != nil {
return err
}
return nil
}
func noop() {}
// NewApp creates a new docker app with the specified path and struct modifiers
func NewApp(path string, ops ...func(*App) error) (*App, error) {
app := &App{
Name: path,
Path: path,
Cleanup: noop,
composesContent: [][]byte{},
parametersContent: [][]byte{},
metadataContent: []byte{},
}
for _, op := range ops {
if err := op(app); err != nil {
return nil, err
}
}
return app, nil
}
// NewAppFromDefaultFiles creates a new docker app using the default files in the specified path.
// If one of those file doesn't exists, it will error out.
func NewAppFromDefaultFiles(path string, ops ...func(*App) error) (*App, error) {
appOps := append([]func(*App) error{
MetadataFile(filepath.Join(path, internal.MetadataFileName)),
WithComposeFiles(filepath.Join(path, internal.ComposeFileName)),
WithParametersFiles(filepath.Join(path, internal.ParametersFileName)),
WithAttachments(path),
}, ops...)
return NewApp(path, appOps...)
}
// WithName sets the application name
func WithName(name string) func(*App) error {
return func(app *App) error {
app.Name = name
return nil
}
}
// WithPath sets the original path of the app
func WithPath(path string) func(*App) error {
return func(app *App) error {
app.Path = path
return nil
}
}
// WithCleanup sets the cleanup function of the app
func WithCleanup(f func()) func(*App) error {
return func(app *App) error {
app.Cleanup = f
return nil
}
}
// WithSource sets the source of the app
func WithSource(source AppSourceKind) func(*App) error {
return func(app *App) error {
app.Source = source
return nil
}
}
func WithCRLF(hasCRLF bool) func(*App) error {
return func(app *App) error {
app.hasCRLF = hasCRLF
return nil
}
}
// WithParametersFiles adds the specified parameters files to the app
func WithParametersFiles(files ...string) func(*App) error {
return parametersLoader(func() ([][]byte, error) { return readFiles(files...) })
}
// WithAttachments adds all local files (exc. main files) to the app
func WithAttachments(rootAppDir string) func(*App) error {
return func(app *App) error {
return filepath.Walk(rootAppDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
localFilePath, err := filepath.Rel(rootAppDir, path)
if err != nil {
return err
}
switch localFilePath {
case internal.ComposeFileName:
case internal.MetadataFileName:
case internal.ParametersFileName:
default:
externalFile := Attachment{
// Standardise on forward slashes for windows boxes
path: filepath.ToSlash(localFilePath),
size: info.Size(),
}
app.attachments = append(app.attachments, externalFile)
}
return nil
})
}
}
// WithParameters adds the specified parameters readers to the app
func WithParameters(readers ...io.Reader) func(*App) error {
return parametersLoader(func() ([][]byte, error) { return readReaders(readers...) })
}
func parametersLoader(f func() ([][]byte, error)) func(*App) error {
return func(app *App) error {
parametersContent, err := f()
if err != nil {
return err
}
parametersContents := append(app.parametersContent, parametersContent...)
loaded, err := parameters.LoadMultiple(parametersContents)
if err != nil {
return err
}
app.parameters = loaded
app.parametersContent = parametersContents
return nil
}
}
// MetadataFile adds the specified metadata file to the app
func MetadataFile(file string) func(*App) error {
return metadataLoader(func() ([]byte, error) { return ioutil.ReadFile(file) })
}
// Metadata adds the specified metadata reader to the app
func Metadata(r io.Reader) func(*App) error {
return metadataLoader(func() ([]byte, error) { return ioutil.ReadAll(r) })
}
func metadataLoader(f func() ([]byte, error)) func(app *App) error {
return func(app *App) error {
d, err := f()
if err != nil {
return err
}
loaded, err := metadata.Load(d)
if err != nil {
return err
}
app.metadata = loaded
app.metadataContent = d
app.hasCRLF = bytes.Contains(d, []byte{'\r', '\n'})
return nil
}
}
// WithComposeFiles adds the specified compose files to the app
func WithComposeFiles(files ...string) func(*App) error {
return composeLoader(func() ([][]byte, error) { return readFiles(files...) })
}
// WithComposes adds the specified compose readers to the app
func WithComposes(readers ...io.Reader) func(*App) error {
return composeLoader(func() ([][]byte, error) { return readReaders(readers...) })
}
func composeLoader(f func() ([][]byte, error)) func(app *App) error {
return func(app *App) error {
composesContent, err := f()
if err != nil {
return err
}
app.composesContent = append(app.composesContent, composesContent...)
app.secrets = secrets.New()
for _, c := range app.composesContent {
parsedCompose, err := composeloader.ParseYAML(c)
if err != nil {
return err
}
cfg, err := composeloader.Load(composetypes.ConfigDetails{
ConfigFiles: []composetypes.ConfigFile{
{Filename: "docker-compose.yml", Config: parsedCompose},
},
}, withSkipInterpolation)
if err != nil {
return err
}
for name, secret := range cfg.Secrets {
app.secrets[name] = secrets.Secret{
Name: secret.Name,
Path: secret.File,
External: secret.External.External,
}
}
}
return nil
}
}
func withSkipInterpolation(opts *composeloader.Options) {
opts.SkipInterpolation = true
}
func readReaders(readers ...io.Reader) ([][]byte, error) {
content := make([][]byte, len(readers))
var errs []string
for i, r := range readers {
d, err := ioutil.ReadAll(r)
if err != nil {
errs = append(errs, err.Error())
continue
}
content[i] = d
}
return content, newErrGroup(errs)
}
func readFiles(files ...string) ([][]byte, error) {
content := make([][]byte, len(files))
var errs []string
for i, file := range files {
d, err := ioutil.ReadFile(file)
if err != nil {
errs = append(errs, err.Error())
continue
}
content[i] = d
}
return content, newErrGroup(errs)
}
func newErrGroup(errs []string) error {
if len(errs) == 0 {
return nil
}
return errors.New(strings.Join(errs, "\n"))
}