@@ -73,7 +73,7 @@ type Compiled struct {
7373 RemoteEnv map [string ]string
7474}
7575
76- func SubstituteVars (s string , workspaceFolder string ) string {
76+ func SubstituteVars (s string , workspaceFolder string , lookupEnv func ( string ) ( string , bool ) ) string {
7777 var buf string
7878 for {
7979 beforeOpen , afterOpen , ok := strings .Cut (s , "${" )
@@ -85,14 +85,14 @@ func SubstituteVars(s string, workspaceFolder string) string {
8585 return buf + s
8686 }
8787
88- buf += beforeOpen + substitute (varExpr , workspaceFolder )
88+ buf += beforeOpen + substitute (varExpr , workspaceFolder , lookupEnv )
8989 s = afterClose
9090 }
9191}
9292
9393// Spec for variable substitutions:
9494// https://containers.dev/implementors/json_reference/#variables-in-devcontainerjson
95- func substitute (varExpr string , workspaceFolder string ) string {
95+ func substitute (varExpr string , workspaceFolder string , lookupEnv func ( string ) ( string , bool ) ) string {
9696 parts := strings .Split (varExpr , ":" )
9797 if len (parts ) == 1 {
9898 switch varExpr {
@@ -101,12 +101,16 @@ func substitute(varExpr string, workspaceFolder string) string {
101101 case "localWorkspaceFolderBasename" , "containerWorkspaceFolderBasename" :
102102 return filepath .Base (workspaceFolder )
103103 default :
104- return os .Getenv (varExpr )
104+ val , ok := lookupEnv (varExpr )
105+ if ok {
106+ return val
107+ }
108+ return ""
105109 }
106110 }
107111 switch parts [0 ] {
108112 case "env" , "localEnv" , "containerEnv" :
109- if val , ok := os . LookupEnv (parts [1 ]); ok {
113+ if val , ok := lookupEnv (parts [1 ]); ok {
110114 return val
111115 }
112116 if len (parts ) == 3 {
@@ -131,7 +135,7 @@ func (s Spec) HasDockerfile() bool {
131135// devcontainerDir is the path to the directory where the devcontainer.json file
132136// is located. scratchDir is the path to the directory where the Dockerfile will
133137// be written to if one doesn't exist.
134- func (s * Spec ) Compile (fs billy.Filesystem , devcontainerDir , scratchDir string , fallbackDockerfile , workspaceFolder string , useBuildContexts bool ) (* Compiled , error ) {
138+ func (s * Spec ) Compile (fs billy.Filesystem , devcontainerDir , scratchDir string , fallbackDockerfile , workspaceFolder string , useBuildContexts bool , lookupEnv func ( string ) ( string , bool ) ) (* Compiled , error ) {
135139 params := & Compiled {
136140 User : s .ContainerUser ,
137141 ContainerEnv : s .ContainerEnv ,
@@ -178,7 +182,7 @@ func (s *Spec) Compile(fs billy.Filesystem, devcontainerDir, scratchDir string,
178182
179183 buildArgs := make ([]string , 0 )
180184 for _ , key := range buildArgkeys {
181- val := SubstituteVars (s .Build .Args [key ], workspaceFolder )
185+ val := SubstituteVars (s .Build .Args [key ], workspaceFolder , lookupEnv )
182186 buildArgs = append (buildArgs , key + "=" + val )
183187 }
184188 params .BuildArgs = buildArgs
0 commit comments