Skip to content

feat: support nested structs, structured arrays, and better secret value handling in config #4727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Oct 25, 2022
Prev Previous commit
Next Next commit
support slices
  • Loading branch information
f0ssel committed Oct 24, 2022
commit e6522cb12e37fed478fb27b3a85bcee98210bb9a
15 changes: 8 additions & 7 deletions cli/deployment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ func setConfig(prefix string, vip *viper.Viper, target interface{}) {
val = val.Elem()
typ = val.Type()
}

// Manually bind to env to support CODER_$INDEX_$FIELD format for structured slices.
_ = vip.BindEnv(prefix, formatEnv(prefix))

if strings.HasPrefix(typ.Name(), "DeploymentConfigField[") {
value := val.FieldByName("Value").Interface()
switch value.(type) {
Expand Down Expand Up @@ -421,8 +425,7 @@ func setConfig(prefix string, vip *viper.Viper, target interface{}) {
case reflect.Slice:
for j := 0; j < fv.Len(); j++ {
key := fmt.Sprintf("%s.%d", key, j)
v := fv.Index(j)
setConfig(key, vip, v)
setConfig(key, vip, fv.Index(j).Interface())
}
default:
panic(fmt.Sprintf("unsupported type %T", ft))
Expand Down Expand Up @@ -466,10 +469,8 @@ func setViperDefaults(prefix string, vip *viper.Viper, target interface{}) {
case reflect.Ptr:
setViperDefaults(key, vip, fv.Interface())
case reflect.Slice:
for j := 0; j < fv.Len(); j++ {
key := fmt.Sprintf("%s.%d", key, j)
setViperDefaults(key, vip, fv.Index(j))
}
// we currently don't support default values on structured slices
continue
default:
panic(fmt.Sprintf("unsupported type %T", ft))
}
Expand Down Expand Up @@ -539,7 +540,7 @@ func setFlags(prefix string, flagset *pflag.FlagSet, vip *viper.Viper, target in
case reflect.Slice:
for j := 0; j < fv.Len(); j++ {
key := fmt.Sprintf("%s.%d", key, j)
setFlags(key, flagset, vip, fv.Index(j), enterprise)
setFlags(key, flagset, vip, fv.Index(j).Interface(), enterprise)
}
default:
panic(fmt.Sprintf("unsupported type %T", ft))
Expand Down