@@ -12,6 +12,16 @@ import (
12
12
"github.com/spf13/pflag"
13
13
)
14
14
15
+ const (
16
+ envbuilderOptionPrefix = "ENVBUILDER_"
17
+ )
18
+
19
+ // nonOverrideOptions are options that cannot be overridden by extra_env.
20
+ var nonOverrideOptions = map [string ]struct {}{
21
+ "ENVBUILDER_CACHE_REPO" : {},
22
+ "ENVBUILDER_GIT_URL" : {},
23
+ }
24
+
15
25
// optionsFromDataModel converts a CachedImageResourceModel into a corresponding set of
16
26
// Envbuilder options. It returns the options and any diagnostics encountered.
17
27
func optionsFromDataModel (data CachedImageResourceModel ) (eboptions.Options , diag.Diagnostics ) {
@@ -154,43 +164,39 @@ func overrideOptionsFromExtraEnv(opts *eboptions.Options, extraEnv map[string]st
154
164
optsMap [opt .Env ] = opt .Value
155
165
}
156
166
for key , val := range extraEnv {
157
- switch key {
158
-
159
- // These options may not be overridden.
160
- case "ENVBUILDER_CACHE_REPO" , "ENVBUILDER_GIT_URL" :
167
+ if _ , found := nonOverrideOptions [key ]; found {
161
168
diags .AddAttributeWarning (path .Root ("extra_env" ),
162
169
"Cannot override required environment variable" ,
163
170
fmt .Sprintf ("The key %q in extra_env cannot be overridden." , key ),
164
171
)
165
172
continue
173
+ }
174
+
175
+ // Check if the option was set on the provider data model and generate a warning if so.
176
+ if _ , overridden := overrides [key ]; overridden {
177
+ diags .AddAttributeWarning (path .Root ("extra_env" ),
178
+ "Overriding provider environment variable" ,
179
+ fmt .Sprintf ("The key %q in extra_env overrides an option set on the provider." , key ),
180
+ )
181
+ }
166
182
167
- default :
168
- // Check if the option was set on the provider data model and generate a warning if so.
169
- if _ , overridden := overrides [key ]; overridden {
170
- diags .AddAttributeWarning (path .Root ("extra_env" ),
171
- "Overriding provider environment variable" ,
172
- fmt .Sprintf ("The key %q in extra_env overrides an option set on the provider." , key ),
173
- )
174
- }
175
-
176
- // XXX: workaround for serpent behaviour where calling Set() on a
177
- // string slice will append instead of replace: set to empty first.
178
- if key == "ENVBUILDER_IGNORE_PATHS" {
179
- _ = optsMap [key ].Set ("" )
180
- }
181
-
182
- opt , found := optsMap [key ]
183
- if ! found {
184
- // ignore unknown keys
185
- continue
186
- }
187
-
188
- if err := opt .Set (val ); err != nil {
189
- diags .AddAttributeError (path .Root ("extra_env" ),
190
- "Invalid value for environment variable" ,
191
- fmt .Sprintf ("The key %q in extra_env has an invalid value: %s" , key , err ),
192
- )
193
- }
183
+ // XXX: workaround for serpent behaviour where calling Set() on a
184
+ // string slice will append instead of replace: set to empty first.
185
+ if key == "ENVBUILDER_IGNORE_PATHS" {
186
+ _ = optsMap [key ].Set ("" )
187
+ }
188
+
189
+ opt , found := optsMap [key ]
190
+ if ! found {
191
+ // ignore unknown keys
192
+ continue
193
+ }
194
+
195
+ if err := opt .Set (val ); err != nil {
196
+ diags .AddAttributeError (path .Root ("extra_env" ),
197
+ "Invalid value for environment variable" ,
198
+ fmt .Sprintf ("The key %q in extra_env has an invalid value: %s" , key , err ),
199
+ )
194
200
}
195
201
}
196
202
return diags
@@ -210,26 +216,17 @@ func computeEnvFromOptions(opts eboptions.Options, extraEnv map[string]string) m
210
216
allEnvKeys [opt .Env ] = struct {}{}
211
217
}
212
218
213
- // Only set the environment variables from opts that are not legacy options.
214
- // Legacy options are those that are not prefixed with ENVBUILDER_.
215
- // While we can detect when a legacy option is set, overriding it becomes
216
- // problematic. Erring on the side of caution, we will not override legacy options.
217
- isEnvbuilderOption := func (key string ) bool {
218
- switch key {
219
- case "CODER_AGENT_URL" , "CODER_AGENT_TOKEN" , "CODER_AGENT_SUBSYSTEM" :
220
- return true // kinda
221
- default :
222
- return strings .HasPrefix (key , "ENVBUILDER_" )
223
- }
224
- }
225
-
226
219
computed := make (map [string ]string )
227
220
for _ , opt := range opts .CLI () {
228
221
if opt .Env == "" {
229
222
continue
230
223
}
231
224
// TODO: remove this check once support for legacy options is removed.
232
- if ! isEnvbuilderOption (opt .Env ) {
225
+ // Only set the environment variables from opts that are not legacy options.
226
+ // Legacy options are those that are not prefixed with ENVBUILDER_.
227
+ // While we can detect when a legacy option is set, overriding it becomes
228
+ // problematic. Erring on the side of caution, we will not override legacy options.
229
+ if ! strings .HasPrefix (opt .Env , envbuilderOptionPrefix ) {
233
230
continue
234
231
}
235
232
var val string
@@ -250,7 +247,7 @@ func computeEnvFromOptions(opts eboptions.Options, extraEnv map[string]string) m
250
247
// Merge in extraEnv, which may override values from opts.
251
248
// Skip any keys that are envbuilder options.
252
249
for key , val := range extraEnv {
253
- if isEnvbuilderOption (key ) {
250
+ if strings . HasPrefix (key , envbuilderOptionPrefix ) {
254
251
continue
255
252
}
256
253
computed [key ] = val
0 commit comments