trueblocks-core icon indicating copy to clipboard operation
trueblocks-core copied to clipboard

loadFromEnv panics

Open tjayrush opened this issue 1 year ago • 0 comments

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
}

tjayrush avatar Sep 23 '24 22:09 tjayrush