@@ -195,27 +195,43 @@ func ComposeLogs(req *DockerComposeLogs, ws *websocket.Conn) error {
195
195
return nil
196
196
}
197
197
198
- func createTempComposeFile (projectName string , definition string ) (string , string , error ) {
198
+ func createTempComposeFile (projectName string , definition string , variables map [ string ]store. VariableValue ) (string , string , string , error ) {
199
199
dir , err := os .MkdirTemp ("" , projectName )
200
200
if err != nil {
201
201
log .Error ().Err (err ).Msg ("Error while creating temp directory for compose" )
202
- return "" , "" , err
202
+ return "" , "" , "" , err
203
203
}
204
204
205
- filename := filepath .Join (dir , "compose.yaml" )
206
- composeFile , err := os .Create (filename )
205
+ composeFilename := filepath .Join (dir , "compose.yaml" )
206
+ composeFile , err := os .Create (composeFilename )
207
207
if err != nil {
208
208
log .Error ().Err (err ).Msg ("Error while creating temp compose file" )
209
- return "" , "" , err
209
+ return "" , "" , "" , err
210
210
}
211
211
212
212
_ , err = composeFile .WriteString (definition )
213
213
if err != nil {
214
214
log .Error ().Err (err ).Msg ("Error while writing to temp compose file" )
215
- return "" , "" , err
215
+ return "" , "" , "" , err
216
216
}
217
217
218
- return dir , filename , nil
218
+ envFilename := filepath .Join (dir , ".env" )
219
+ envFile , err := os .Create (envFilename )
220
+ if err != nil {
221
+ log .Error ().Err (err ).Msg ("Error while creating temp compose file" )
222
+ return "" , "" , "" , err
223
+ }
224
+
225
+ envVars := toEnvFormat (variables )
226
+ for _ , v := range envVars {
227
+ _ , err = envFile .WriteString (v + "\r \n " )
228
+ if err != nil {
229
+ log .Error ().Err (err ).Msg ("Error while writing to temp .env file" )
230
+ return "" , "" , "" , err
231
+ }
232
+ }
233
+
234
+ return dir , composeFilename , envFilename , nil
219
235
}
220
236
221
237
func toEnvFormat (variables map [string ]store.VariableValue ) ([]string ) {
@@ -258,24 +274,24 @@ func processVars(cmd *exec.Cmd, variables map[string]store.VariableValue, ws *we
258
274
}
259
275
260
276
func performComposeAction (action string , projectName string , definition string , variables map [string ]store.VariableValue , ws * websocket.Conn , printVars bool ) error {
261
- dir , file , err := createTempComposeFile (projectName , definition )
262
- log .Debug ().Str ("fileName " , file ). Msg ("Created temporary compose file" )
277
+ dir , composefile , envfile , err := createTempComposeFile (projectName , definition , variables )
278
+ log .Debug ().Str ("composeFileName " , composefile ). Str ( "envFileName" , envfile ). Msg ("Created temporary compose file and .env file" )
263
279
if err != nil {
264
280
return err
265
281
}
266
282
defer func () {
267
- log .Debug ().Str ("fileName" , file ).Msg ("Deleting temporary compose file" )
283
+ log .Debug ().Str ("fileName" , composefile ).Msg ("Deleting temporary compose file and .env file" )
268
284
os .RemoveAll (dir )
269
285
}()
270
286
271
287
var cmd * exec.Cmd
272
288
switch action {
273
289
case "up" :
274
- cmd = exec .Command ("docker-compose" , "-p" , projectName , "-f" , file , action , "-d" )
290
+ cmd = exec .Command ("docker-compose" , "-p" , projectName , "-f" , composefile , action , "-d" )
275
291
case "down" :
276
292
cmd = exec .Command ("docker-compose" , "-p" , projectName , action )
277
293
case "pull" :
278
- cmd = exec .Command ("docker-compose" , "-p" , projectName , "-f" , file , action )
294
+ cmd = exec .Command ("docker-compose" , "-p" , projectName , "-f" , composefile , action )
279
295
default :
280
296
panic (fmt .Errorf ("unknown compose action %s" , action ))
281
297
}
0 commit comments