Browse Source

fix #2129 (#2132)

* fix #2129

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

* fix unit tests
pull/2136/head
Shivaram Lingamneni 2 months ago
parent
commit
681e8b1292
No account linked to committer's email address
2 changed files with 11 additions and 11 deletions
  1. 9
    9
      irc/config.go
  2. 2
    2
      irc/config_test.go

+ 9
- 9
irc/config.go View File

1043
 	return fmt.Sprintf("Couldn't apply config override `%s`: %s", ce.name, ce.desc)
1043
 	return fmt.Sprintf("Couldn't apply config override `%s`: %s", ce.name, ce.desc)
1044
 }
1044
 }
1045
 
1045
 
1046
-func mungeFromEnvironment(config *Config, envPair string) (applied bool, err *configPathError) {
1046
+func mungeFromEnvironment(config *Config, envPair string) (applied bool, name string, err *configPathError) {
1047
 	equalIdx := strings.IndexByte(envPair, '=')
1047
 	equalIdx := strings.IndexByte(envPair, '=')
1048
 	name, value := envPair[:equalIdx], envPair[equalIdx+1:]
1048
 	name, value := envPair[:equalIdx], envPair[equalIdx+1:]
1049
 	if strings.HasPrefix(name, "ERGO__") {
1049
 	if strings.HasPrefix(name, "ERGO__") {
1051
 	} else if strings.HasPrefix(name, "ORAGONO__") {
1051
 	} else if strings.HasPrefix(name, "ORAGONO__") {
1052
 		name = strings.TrimPrefix(name, "ORAGONO__")
1052
 		name = strings.TrimPrefix(name, "ORAGONO__")
1053
 	} else {
1053
 	} else {
1054
-		return false, nil
1054
+		return false, "", nil
1055
 	}
1055
 	}
1056
 	pathComponents := strings.Split(name, "__")
1056
 	pathComponents := strings.Split(name, "__")
1057
 	for i, pathComponent := range pathComponents {
1057
 	for i, pathComponent := range pathComponents {
1062
 	t := v.Type()
1062
 	t := v.Type()
1063
 	for _, component := range pathComponents {
1063
 	for _, component := range pathComponents {
1064
 		if component == "" {
1064
 		if component == "" {
1065
-			return false, &configPathError{name, "invalid", nil}
1065
+			return false, "", &configPathError{name, "invalid", nil}
1066
 		}
1066
 		}
1067
 		if v.Kind() != reflect.Struct {
1067
 		if v.Kind() != reflect.Struct {
1068
-			return false, &configPathError{name, "index into non-struct", nil}
1068
+			return false, "", &configPathError{name, "index into non-struct", nil}
1069
 		}
1069
 		}
1070
 		var nextField reflect.StructField
1070
 		var nextField reflect.StructField
1071
 		success := false
1071
 		success := false
1091
 			}
1091
 			}
1092
 		}
1092
 		}
1093
 		if !success {
1093
 		if !success {
1094
-			return false, &configPathError{name, fmt.Sprintf("couldn't resolve path component: `%s`", component), nil}
1094
+			return false, "", &configPathError{name, fmt.Sprintf("couldn't resolve path component: `%s`", component), nil}
1095
 		}
1095
 		}
1096
 		v = v.FieldByName(nextField.Name)
1096
 		v = v.FieldByName(nextField.Name)
1097
 		// dereference pointer field if necessary, initialize new value if necessary
1097
 		// dereference pointer field if necessary, initialize new value if necessary
1105
 	}
1105
 	}
1106
 	yamlErr := yaml.Unmarshal([]byte(value), v.Addr().Interface())
1106
 	yamlErr := yaml.Unmarshal([]byte(value), v.Addr().Interface())
1107
 	if yamlErr != nil {
1107
 	if yamlErr != nil {
1108
-		return false, &configPathError{name, "couldn't deserialize YAML", yamlErr}
1108
+		return false, "", &configPathError{name, "couldn't deserialize YAML", yamlErr}
1109
 	}
1109
 	}
1110
-	return true, nil
1110
+	return true, name, nil
1111
 }
1111
 }
1112
 
1112
 
1113
 // LoadConfig loads the given YAML configuration file.
1113
 // LoadConfig loads the given YAML configuration file.
1119
 
1119
 
1120
 	if config.AllowEnvironmentOverrides {
1120
 	if config.AllowEnvironmentOverrides {
1121
 		for _, envPair := range os.Environ() {
1121
 		for _, envPair := range os.Environ() {
1122
-			applied, envErr := mungeFromEnvironment(config, envPair)
1122
+			applied, name, envErr := mungeFromEnvironment(config, envPair)
1123
 			if envErr != nil {
1123
 			if envErr != nil {
1124
 				if envErr.fatalErr != nil {
1124
 				if envErr.fatalErr != nil {
1125
 					return nil, envErr
1125
 					return nil, envErr
1127
 					log.Println(envErr.Error())
1127
 					log.Println(envErr.Error())
1128
 				}
1128
 				}
1129
 			} else if applied {
1129
 			} else if applied {
1130
-				log.Printf("applied environment override: %s\n", envPair)
1130
+				log.Printf("applied environment override: %s\n", name)
1131
 			}
1131
 			}
1132
 		}
1132
 		}
1133
 	}
1133
 	}

+ 2
- 2
irc/config_test.go View File

28
 		`ORAGONO__SERVER__IP_CLOAKING={"enabled": true, "enabled-for-always-on": true, "netname": "irc", "cidr-len-ipv4": 32, "cidr-len-ipv6": 64, "num-bits": 64}`,
28
 		`ORAGONO__SERVER__IP_CLOAKING={"enabled": true, "enabled-for-always-on": true, "netname": "irc", "cidr-len-ipv4": 32, "cidr-len-ipv6": 64, "num-bits": 64}`,
29
 	}
29
 	}
30
 	for _, envPair := range env {
30
 	for _, envPair := range env {
31
-		_, err := mungeFromEnvironment(&config, envPair)
31
+		_, _, err := mungeFromEnvironment(&config, envPair)
32
 		if err != nil {
32
 		if err != nil {
33
 			t.Errorf("couldn't apply override `%s`: %v", envPair, err)
33
 			t.Errorf("couldn't apply override `%s`: %v", envPair, err)
34
 		}
34
 		}
93
 	}
93
 	}
94
 
94
 
95
 	for _, env := range invalidEnvs {
95
 	for _, env := range invalidEnvs {
96
-		success, err := mungeFromEnvironment(&config, env)
96
+		success, _, err := mungeFromEnvironment(&config, env)
97
 		if err == nil || success {
97
 		if err == nil || success {
98
 			t.Errorf("accepted invalid env override `%s`", env)
98
 			t.Errorf("accepted invalid env override `%s`", env)
99
 		}
99
 		}

Loading…
Cancel
Save