3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-15 00:19:29 +01:00
* fix #2129

Don't print the values of environment variable overrides, just the keys

* fix unit tests
This commit is contained in:
Shivaram Lingamneni 2024-02-25 10:05:36 -05:00 committed by GitHub
parent 432d4ea860
commit 681e8b1292
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 11 deletions

View File

@ -1043,7 +1043,7 @@ func (ce *configPathError) Error() string {
return fmt.Sprintf("Couldn't apply config override `%s`: %s", ce.name, ce.desc) return fmt.Sprintf("Couldn't apply config override `%s`: %s", ce.name, ce.desc)
} }
func mungeFromEnvironment(config *Config, envPair string) (applied bool, err *configPathError) { func mungeFromEnvironment(config *Config, envPair string) (applied bool, name string, err *configPathError) {
equalIdx := strings.IndexByte(envPair, '=') equalIdx := strings.IndexByte(envPair, '=')
name, value := envPair[:equalIdx], envPair[equalIdx+1:] name, value := envPair[:equalIdx], envPair[equalIdx+1:]
if strings.HasPrefix(name, "ERGO__") { if strings.HasPrefix(name, "ERGO__") {
@ -1051,7 +1051,7 @@ func mungeFromEnvironment(config *Config, envPair string) (applied bool, err *co
} else if strings.HasPrefix(name, "ORAGONO__") { } else if strings.HasPrefix(name, "ORAGONO__") {
name = strings.TrimPrefix(name, "ORAGONO__") name = strings.TrimPrefix(name, "ORAGONO__")
} else { } else {
return false, nil return false, "", nil
} }
pathComponents := strings.Split(name, "__") pathComponents := strings.Split(name, "__")
for i, pathComponent := range pathComponents { for i, pathComponent := range pathComponents {
@ -1062,10 +1062,10 @@ func mungeFromEnvironment(config *Config, envPair string) (applied bool, err *co
t := v.Type() t := v.Type()
for _, component := range pathComponents { for _, component := range pathComponents {
if component == "" { if component == "" {
return false, &configPathError{name, "invalid", nil} return false, "", &configPathError{name, "invalid", nil}
} }
if v.Kind() != reflect.Struct { if v.Kind() != reflect.Struct {
return false, &configPathError{name, "index into non-struct", nil} return false, "", &configPathError{name, "index into non-struct", nil}
} }
var nextField reflect.StructField var nextField reflect.StructField
success := false success := false
@ -1091,7 +1091,7 @@ func mungeFromEnvironment(config *Config, envPair string) (applied bool, err *co
} }
} }
if !success { if !success {
return false, &configPathError{name, fmt.Sprintf("couldn't resolve path component: `%s`", component), nil} return false, "", &configPathError{name, fmt.Sprintf("couldn't resolve path component: `%s`", component), nil}
} }
v = v.FieldByName(nextField.Name) v = v.FieldByName(nextField.Name)
// dereference pointer field if necessary, initialize new value if necessary // dereference pointer field if necessary, initialize new value if necessary
@ -1105,9 +1105,9 @@ func mungeFromEnvironment(config *Config, envPair string) (applied bool, err *co
} }
yamlErr := yaml.Unmarshal([]byte(value), v.Addr().Interface()) yamlErr := yaml.Unmarshal([]byte(value), v.Addr().Interface())
if yamlErr != nil { if yamlErr != nil {
return false, &configPathError{name, "couldn't deserialize YAML", yamlErr} return false, "", &configPathError{name, "couldn't deserialize YAML", yamlErr}
} }
return true, nil return true, name, nil
} }
// LoadConfig loads the given YAML configuration file. // LoadConfig loads the given YAML configuration file.
@ -1119,7 +1119,7 @@ func LoadConfig(filename string) (config *Config, err error) {
if config.AllowEnvironmentOverrides { if config.AllowEnvironmentOverrides {
for _, envPair := range os.Environ() { for _, envPair := range os.Environ() {
applied, envErr := mungeFromEnvironment(config, envPair) applied, name, envErr := mungeFromEnvironment(config, envPair)
if envErr != nil { if envErr != nil {
if envErr.fatalErr != nil { if envErr.fatalErr != nil {
return nil, envErr return nil, envErr
@ -1127,7 +1127,7 @@ func LoadConfig(filename string) (config *Config, err error) {
log.Println(envErr.Error()) log.Println(envErr.Error())
} }
} else if applied { } else if applied {
log.Printf("applied environment override: %s\n", envPair) log.Printf("applied environment override: %s\n", name)
} }
} }
} }

View File

@ -28,7 +28,7 @@ func TestEnvironmentOverrides(t *testing.T) {
`ORAGONO__SERVER__IP_CLOAKING={"enabled": true, "enabled-for-always-on": true, "netname": "irc", "cidr-len-ipv4": 32, "cidr-len-ipv6": 64, "num-bits": 64}`, `ORAGONO__SERVER__IP_CLOAKING={"enabled": true, "enabled-for-always-on": true, "netname": "irc", "cidr-len-ipv4": 32, "cidr-len-ipv6": 64, "num-bits": 64}`,
} }
for _, envPair := range env { for _, envPair := range env {
_, err := mungeFromEnvironment(&config, envPair) _, _, err := mungeFromEnvironment(&config, envPair)
if err != nil { if err != nil {
t.Errorf("couldn't apply override `%s`: %v", envPair, err) t.Errorf("couldn't apply override `%s`: %v", envPair, err)
} }
@ -93,7 +93,7 @@ func TestEnvironmentOverrideErrors(t *testing.T) {
} }
for _, env := range invalidEnvs { for _, env := range invalidEnvs {
success, err := mungeFromEnvironment(&config, env) success, _, err := mungeFromEnvironment(&config, env)
if err == nil || success { if err == nil || success {
t.Errorf("accepted invalid env override `%s`", env) t.Errorf("accepted invalid env override `%s`", env)
} }