Skip to content

Commit e6522cb

Browse files
committed
support slices
1 parent 4bcdd0a commit e6522cb

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

cli/deployment/config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ func setConfig(prefix string, vip *viper.Viper, target interface{}) {
374374
val = val.Elem()
375375
typ = val.Type()
376376
}
377+
378+
// Manually bind to env to support CODER_$INDEX_$FIELD format for structured slices.
379+
_ = vip.BindEnv(prefix, formatEnv(prefix))
380+
377381
if strings.HasPrefix(typ.Name(), "DeploymentConfigField[") {
378382
value := val.FieldByName("Value").Interface()
379383
switch value.(type) {
@@ -421,8 +425,7 @@ func setConfig(prefix string, vip *viper.Viper, target interface{}) {
421425
case reflect.Slice:
422426
for j := 0; j < fv.Len(); j++ {
423427
key := fmt.Sprintf("%s.%d", key, j)
424-
v := fv.Index(j)
425-
setConfig(key, vip, v)
428+
setConfig(key, vip, fv.Index(j).Interface())
426429
}
427430
default:
428431
panic(fmt.Sprintf("unsupported type %T", ft))
@@ -466,10 +469,8 @@ func setViperDefaults(prefix string, vip *viper.Viper, target interface{}) {
466469
case reflect.Ptr:
467470
setViperDefaults(key, vip, fv.Interface())
468471
case reflect.Slice:
469-
for j := 0; j < fv.Len(); j++ {
470-
key := fmt.Sprintf("%s.%d", key, j)
471-
setViperDefaults(key, vip, fv.Index(j))
472-
}
472+
// we currently don't support default values on structured slices
473+
continue
473474
default:
474475
panic(fmt.Sprintf("unsupported type %T", ft))
475476
}
@@ -539,7 +540,7 @@ func setFlags(prefix string, flagset *pflag.FlagSet, vip *viper.Viper, target in
539540
case reflect.Slice:
540541
for j := 0; j < fv.Len(); j++ {
541542
key := fmt.Sprintf("%s.%d", key, j)
542-
setFlags(key, flagset, vip, fv.Index(j), enterprise)
543+
setFlags(key, flagset, vip, fv.Index(j).Interface(), enterprise)
543544
}
544545
default:
545546
panic(fmt.Sprintf("unsupported type %T", ft))

0 commit comments

Comments
 (0)