@@ -73,7 +73,7 @@ type Compiled struct {
73
73
RemoteEnv map [string ]string
74
74
}
75
75
76
- func SubstituteVars (s string , workspaceFolder string ) string {
76
+ func SubstituteVars (s string , workspaceFolder string , lookupEnv func ( string ) ( string , bool ) ) string {
77
77
var buf string
78
78
for {
79
79
beforeOpen , afterOpen , ok := strings .Cut (s , "${" )
@@ -85,14 +85,14 @@ func SubstituteVars(s string, workspaceFolder string) string {
85
85
return buf + s
86
86
}
87
87
88
- buf += beforeOpen + substitute (varExpr , workspaceFolder )
88
+ buf += beforeOpen + substitute (varExpr , workspaceFolder , lookupEnv )
89
89
s = afterClose
90
90
}
91
91
}
92
92
93
93
// Spec for variable substitutions:
94
94
// 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 {
96
96
parts := strings .Split (varExpr , ":" )
97
97
if len (parts ) == 1 {
98
98
switch varExpr {
@@ -101,12 +101,16 @@ func substitute(varExpr string, workspaceFolder string) string {
101
101
case "localWorkspaceFolderBasename" , "containerWorkspaceFolderBasename" :
102
102
return filepath .Base (workspaceFolder )
103
103
default :
104
- return os .Getenv (varExpr )
104
+ val , ok := lookupEnv (varExpr )
105
+ if ok {
106
+ return val
107
+ }
108
+ return ""
105
109
}
106
110
}
107
111
switch parts [0 ] {
108
112
case "env" , "localEnv" , "containerEnv" :
109
- if val , ok := os . LookupEnv (parts [1 ]); ok {
113
+ if val , ok := lookupEnv (parts [1 ]); ok {
110
114
return val
111
115
}
112
116
if len (parts ) == 3 {
@@ -131,7 +135,7 @@ func (s Spec) HasDockerfile() bool {
131
135
// devcontainerDir is the path to the directory where the devcontainer.json file
132
136
// is located. scratchDir is the path to the directory where the Dockerfile will
133
137
// 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 ) {
135
139
params := & Compiled {
136
140
User : s .ContainerUser ,
137
141
ContainerEnv : s .ContainerEnv ,
@@ -178,7 +182,7 @@ func (s *Spec) Compile(fs billy.Filesystem, devcontainerDir, scratchDir string,
178
182
179
183
buildArgs := make ([]string , 0 )
180
184
for _ , key := range buildArgkeys {
181
- val := SubstituteVars (s .Build .Args [key ], workspaceFolder )
185
+ val := SubstituteVars (s .Build .Args [key ], workspaceFolder , lookupEnv )
182
186
buildArgs = append (buildArgs , key + "=" + val )
183
187
}
184
188
params .BuildArgs = buildArgs
0 commit comments