trueblocks-core
trueblocks-core copied to clipboard
loadFromEnv panics
The following code fails with invalid input:
Invalid input here causes panic. Set an invalid environment variable (TB_CHAINS_MAINNETRPC for example -- it should be TB_CHAINS_MAINNET_RPCPROVIDER) and run the tests to see the panic.
// loadFromEnv loads configuration from environment variables
func loadFromEnv(prefix string, destination *ConfigFile) (err error) {
// First we get all env variables then filter by prefix and finally parse the values
envs := os.Environ()
for i := 0; i < len(envs); i++ {
// Turn VAR=value into []string{"VAR", "value"}
parsed := strings.Split(envs[i], "=")
if !strings.HasPrefix(parsed[0], prefix) {
continue
}
// Turn PARENT_CHILD into []string{"PARENT", "CHILD"}
path := strings.Split(parsed[0][len(prefix):], "_")
if err := setByPath(destination, path, parsed[1]); err != nil {
return err
}
}
return nil
}