Skip to content
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

fix #2129 #2132

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions irc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,15 +1043,15 @@ func (ce *configPathError) Error() string {
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, '=')
name, value := envPair[:equalIdx], envPair[equalIdx+1:]
if strings.HasPrefix(name, "ERGO__") {
name = strings.TrimPrefix(name, "ERGO__")
} else if strings.HasPrefix(name, "ORAGONO__") {
name = strings.TrimPrefix(name, "ORAGONO__")
} else {
return false, nil
return false, "", nil
}
pathComponents := strings.Split(name, "__")
for i, pathComponent := range pathComponents {
Expand All @@ -1062,10 +1062,10 @@ func mungeFromEnvironment(config *Config, envPair string) (applied bool, err *co
t := v.Type()
for _, component := range pathComponents {
if component == "" {
return false, &configPathError{name, "invalid", nil}
return false, "", &configPathError{name, "invalid", nil}
}
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
success := false
Expand All @@ -1091,7 +1091,7 @@ func mungeFromEnvironment(config *Config, envPair string) (applied bool, err *co
}
}
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)
// dereference pointer field if necessary, initialize new value if necessary
Expand All @@ -1105,9 +1105,9 @@ func mungeFromEnvironment(config *Config, envPair string) (applied bool, err *co
}
yamlErr := yaml.Unmarshal([]byte(value), v.Addr().Interface())
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.
Expand All @@ -1119,15 +1119,15 @@ func LoadConfig(filename string) (config *Config, err error) {

if config.AllowEnvironmentOverrides {
for _, envPair := range os.Environ() {
applied, envErr := mungeFromEnvironment(config, envPair)
applied, name, envErr := mungeFromEnvironment(config, envPair)
if envErr != nil {
if envErr.fatalErr != nil {
return nil, envErr
} else {
log.Println(envErr.Error())
}
} else if applied {
log.Printf("applied environment override: %s\n", envPair)
log.Printf("applied environment override: %s\n", name)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions irc/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
}
for _, envPair := range env {
_, err := mungeFromEnvironment(&config, envPair)
_, _, err := mungeFromEnvironment(&config, envPair)
if err != nil {
t.Errorf("couldn't apply override `%s`: %v", envPair, err)
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestEnvironmentOverrideErrors(t *testing.T) {
}

for _, env := range invalidEnvs {
success, err := mungeFromEnvironment(&config, env)
success, _, err := mungeFromEnvironment(&config, env)
if err == nil || success {
t.Errorf("accepted invalid env override `%s`", env)
}
Expand Down
Loading