@@ -333,7 +333,7 @@ func (d *LocalRunner) getService(name string) *service {
333333
334334// applyTemplate resolves the templates from the manifest (Dir, Port, Connect) into
335335// the actual values for this specific docker execution.
336- func (d * LocalRunner ) applyTemplate (s * service ) ([]string , error ) {
336+ func (d * LocalRunner ) applyTemplate (s * service ) ([]string , map [ string ] string , error ) {
337337 var input map [string ]interface {}
338338
339339 // For {{.Dir}}:
@@ -350,7 +350,12 @@ func (d *LocalRunner) applyTemplate(s *service) ([]string, error) {
350350 }
351351
352352 funcs := template.FuncMap {
353- "Service" : func (name string , portLabel string ) string {
353+ "Service" : func (name string , portLabel , protocol string ) string {
354+ protocolPrefix := ""
355+ if protocol == "http" {
356+ protocolPrefix = "http://"
357+ }
358+
354359 // For {{Service "name" "portLabel"}}:
355360 // - Service runs on host:
356361 // A: target is inside docker: access with localhost:hostPort
@@ -365,14 +370,14 @@ func (d *LocalRunner) applyTemplate(s *service) ([]string, error) {
365370
366371 if d .isHostService (s .Name ) {
367372 // A and B
368- return fmt .Sprintf ("http://localhost: %d" , port .HostPort )
373+ return fmt .Sprintf ("%slocalhost: %d" , protocolPrefix , port .HostPort )
369374 } else {
370375 if d .isHostService (svc .Name ) {
371376 // D
372- return fmt .Sprintf ("http://host .docker.internal:%d" , port .HostPort )
377+ return fmt .Sprintf ("%shost .docker.internal:%d" , protocolPrefix , port .HostPort )
373378 }
374379 // C
375- return fmt .Sprintf ("http://%s :%d" , svc .Name , port .Port )
380+ return fmt .Sprintf ("%s%s :%d" , protocolPrefix , svc .Name , port .Port )
376381 }
377382 },
378383 "Port" : func (name string , defaultPort int ) int {
@@ -386,28 +391,48 @@ func (d *LocalRunner) applyTemplate(s *service) ([]string, error) {
386391 },
387392 }
388393
389- var argsResult []string
390- for _ , arg := range s .args {
394+ runTemplate := func (arg string ) (string , error ) {
391395 tpl , err := template .New ("" ).Funcs (funcs ).Parse (arg )
392396 if err != nil {
393- return nil , err
397+ return "" , err
394398 }
395399
396400 var out strings.Builder
397401 if err := tpl .Execute (& out , input ); err != nil {
398- return nil , err
402+ return "" , err
403+ }
404+
405+ return out .String (), nil
406+ }
407+
408+ // apply the templates to the arguments
409+ var argsResult []string
410+ for _ , arg := range s .args {
411+ newArg , err := runTemplate (arg )
412+ if err != nil {
413+ return nil , nil , err
414+ }
415+ argsResult = append (argsResult , newArg )
416+ }
417+
418+ // apply the templates to the environment variables
419+ envs := map [string ]string {}
420+ for k , v := range s .env {
421+ newV , err := runTemplate (v )
422+ if err != nil {
423+ return nil , nil , err
399424 }
400- argsResult = append ( argsResult , out . String ())
425+ envs [ k ] = newV
401426 }
402427
403- return argsResult , nil
428+ return argsResult , envs , nil
404429}
405430
406431func (d * LocalRunner ) toDockerComposeService (s * service ) (map [string ]interface {}, error ) {
407432 // apply the template again on the arguments to figure out the connections
408433 // at this point all of them are valid, we just have to resolve them again. We assume for now
409434 // everyone is going to be on docker at the same network.
410- args , err := d .applyTemplate (s )
435+ args , envs , err := d .applyTemplate (s )
411436 if err != nil {
412437 return nil , fmt .Errorf ("failed to apply template, err: %w" , err )
413438 }
@@ -433,8 +458,8 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}
433458 "labels" : map [string ]string {"playground" : "true" },
434459 }
435460
436- if len (s . env ) > 0 {
437- service ["environment" ] = s . env
461+ if len (envs ) > 0 {
462+ service ["environment" ] = envs
438463 }
439464
440465 if s .readyCheck != nil {
@@ -544,7 +569,8 @@ func (d *LocalRunner) generateDockerCompose() ([]byte, error) {
544569
545570// runOnHost runs the service on the host machine
546571func (d * LocalRunner ) runOnHost (ss * service ) error {
547- args , err := d .applyTemplate (ss )
572+ // TODO: Use env vars in host processes
573+ args , _ , err := d .applyTemplate (ss )
548574 if err != nil {
549575 return fmt .Errorf ("failed to apply template, err: %w" , err )
550576 }
0 commit comments