@@ -4,13 +4,16 @@ import (
4
4
"bytes"
5
5
"fmt"
6
6
"io/ioutil"
7
+ "os"
7
8
"path/filepath"
8
9
"strings"
9
10
"text/template"
10
11
11
12
"github.com/docker/cli/cli/compose/loader"
12
13
composetypes "github.com/docker/cli/cli/compose/types"
14
+ "github.com/docker/lunchbox/internal"
13
15
"github.com/docker/lunchbox/packager"
16
+ "github.com/docker/yatee/yatee"
14
17
"gopkg.in/yaml.v2"
15
18
)
16
19
@@ -69,6 +72,44 @@ func loadSettings(files []string) (map[string]interface{}, error) {
69
72
return res , nil
70
73
}
71
74
75
+ func applyRenderers (data []byte , renderers []string , settings map [string ]interface {}) ([]byte , error ) {
76
+ for _ , r := range renderers {
77
+ switch r {
78
+ case "gotemplate" :
79
+ tmpl , err := template .New ("compose" ).Parse (string (data ))
80
+ if err != nil {
81
+ return nil , err
82
+ }
83
+ yaml := bytes .NewBuffer (nil )
84
+ err = tmpl .Execute (yaml , settings )
85
+ if err != nil {
86
+ return nil , err
87
+ }
88
+ data = yaml .Bytes ()
89
+ case "yatee" :
90
+ yateed , err := yatee .Process (string (data ), settings )
91
+ if err != nil {
92
+ return nil , err
93
+ }
94
+ m , err := yaml .Marshal (yateed )
95
+ if err != nil {
96
+ return nil , err
97
+ }
98
+ data = []byte (strings .Replace (string (m ), "$" , "$$" , - 1 ))
99
+ }
100
+ }
101
+ return data , nil
102
+ }
103
+
104
+ func contains (list []string , needle string ) bool {
105
+ for _ , e := range list {
106
+ if e == needle {
107
+ return true
108
+ }
109
+ }
110
+ return false
111
+ }
112
+
72
113
// Render renders the Compose file for this app, merging in settings files, other compose files, end env
73
114
func Render (appname string , composeFiles []string , settingsFile []string , env map [string ]string ) (* composetypes.Config , error ) {
74
115
appname , cleanup , err := packager .Extract (appname )
@@ -121,23 +162,28 @@ func Render(appname string, composeFiles []string, settingsFile []string, env ma
121
162
// prepend our app compose file to the list
122
163
composes := []string {filepath .Join (appname , "docker-compose.yml" )}
123
164
composes = append (composes , composeFiles ... )
165
+ renderers := strings .Split (internal .Renderers , "," )
166
+ if r , ok := os .LookupEnv ("DOCKERAPP_RENDERERS" ); ok {
167
+ rl := strings .Split (r , "," )
168
+ for _ , r := range rl {
169
+ if ! contains (renderers , r ) {
170
+ return nil , fmt .Errorf ("renderer '%s' not found" , r )
171
+ }
172
+ }
173
+ renderers = rl
174
+ }
124
175
// go-template, then parse, then expand the compose files
125
176
configFiles := []composetypes.ConfigFile {}
126
177
for _ , c := range composes {
127
178
data , err := ioutil .ReadFile (c )
128
179
if err != nil {
129
180
return nil , err
130
181
}
131
- tmpl , err := template .New ("compose" ).Parse (string (data ))
132
- if err != nil {
133
- return nil , err
134
- }
135
- yaml := bytes .NewBuffer (nil )
136
- err = tmpl .Execute (yaml , settings )
182
+ data , err = applyRenderers (data , renderers , settings )
137
183
if err != nil {
138
184
return nil , err
139
185
}
140
- parsed , err := loader .ParseYAML (yaml . Bytes () )
186
+ parsed , err := loader .ParseYAML (data )
141
187
if err != nil {
142
188
return nil , err
143
189
}
0 commit comments