-
Notifications
You must be signed in to change notification settings - Fork 1
/
compose_template.go
130 lines (111 loc) · 2.81 KB
/
compose_template.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
package beluga
import (
"bytes"
"text/template"
"github.com/duckbrain/beluga/internal/compose"
"github.com/pkg/errors"
)
type composeTemplateData struct {
Src *compose.File
Service *compose.Service
ServiceName string
Volume *compose.VolumeDefinition
VolumeName string
Env Environment
}
const tmplKey = "BELUGA"
func composeTemplate(src, tmpl string, env Environment) (string, error) {
if len(tmpl) == 0 {
return src, nil
}
t, err := template.New("").Parse(tmpl)
if err != nil {
return "", errors.Wrap(err, "parse template")
}
file, err := compose.Parse(src)
if err != nil {
return "", errors.Wrap(err, "yaml parse source")
}
parseTemplate := func(data composeTemplateData) (*compose.File, error) {
s := new(bytes.Buffer)
data.Env = env
data.Src = file
if data.Service == nil {
data.Service = &compose.Service{}
}
if data.Volume == nil {
data.Volume = &compose.VolumeDefinition{}
}
err := t.Execute(s, data)
if err != nil {
return nil, errors.Wrap(err, "execute template")
}
f, err := compose.Parse(s.String())
return f, errors.Wrap(err, "yaml parse template output")
}
tFile, err := parseTemplate(composeTemplateData{})
if err != nil {
return "", err
}
err = file.Merge(tFile)
if err != nil {
return "", errors.Wrap(err, "merging file base")
}
for name, service := range file.Services {
tFile, err := parseTemplate(composeTemplateData{Service: service, ServiceName: name})
if err != nil {
return "", errors.Wrapf(err, "service \"%v\"", name)
}
if tService, ok := tFile.Services[tmplKey]; ok {
err = service.Merge(tService)
if err != nil {
return "", errors.Wrapf(err, "merge service %v", name)
}
}
file.Services[name] = service
}
for name, tService := range tFile.Services {
if name == tmplKey {
continue
}
service, ok := file.Services[name]
if ok {
err = service.Merge(tService)
if err != nil {
return "", errors.Wrapf(err, "merge template service %v", name)
}
file.Services[name] = service
} else {
file.Services[name] = tService
}
}
for name, volume := range file.Volumes {
tFile, err := parseTemplate(composeTemplateData{Volume: &volume, VolumeName: name})
if err != nil {
return "", errors.Wrapf(err, "volume \"%v\"", name)
}
if tVolume, ok := tFile.Volumes[tmplKey]; ok {
err = volume.Merge(&tVolume)
if err != nil {
return "", errors.Wrapf(err, "merge volume %v", name)
}
}
file.Volumes[name] = volume
}
for name, tVolume := range tFile.Volumes {
if name == tmplKey {
continue
}
volume, ok := file.Volumes[name]
if ok {
err = volume.Merge(&tVolume)
if err != nil {
return "", errors.Wrapf(err, "merge template volume %v", name)
}
file.Volumes[name] = volume
} else {
file.Volumes[name] = tVolume
}
}
return file.TryString()
}