From 8ae5b9fe2fde3cbc32f4bd7cc17d57c2e1a4a6e5 Mon Sep 17 00:00:00 2001 From: lukaszcl <120112546+lukaszcl@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:04:48 +0200 Subject: [PATCH] Fix order of RPC and wallet keys fetched from env vars This commit refactors the code in `lib/config/env.go` to use sorted keys when handling environment variables. Previously, the code was iterating over the environment variables in an arbitrary order, which could lead to inconsistent behavior. Now, the `getSortedEnvs` function has been added to sort the environment variables by key before processing them. --- lib/config/env.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/config/env.go b/lib/config/env.go index 86d5fe006..71172765f 100644 --- a/lib/config/env.go +++ b/lib/config/env.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "regexp" + "sort" "strconv" "strings" @@ -79,7 +80,7 @@ func ReadEnvVarSlice_String(pattern string) []string { re := regexp.MustCompile(pattern) var values []string - for _, env := range os.Environ() { + for _, env := range getSortedEnvs() { pair := strings.SplitN(env, "=", 2) if len(pair) != 2 { continue @@ -146,7 +147,7 @@ func readEnvVarValue(envVarName string, valueType EnvValueType) (interface{}, er func readEnvVarGroupedMap(pattern string) map[string][]string { re := regexp.MustCompile(pattern) groupedVars := make(map[string][]string) - for _, env := range os.Environ() { + for _, env := range getSortedEnvs() { pair := strings.SplitN(env, "=", 2) if len(pair) != 2 { continue @@ -164,7 +165,7 @@ func readEnvVarGroupedMap(pattern string) map[string][]string { func readEnvVarSingleMap(pattern string) map[string]string { re := regexp.MustCompile(pattern) singleVars := make(map[string]string) - for _, env := range os.Environ() { + for _, env := range getSortedEnvs() { pair := strings.SplitN(env, "=", 2) if len(pair) != 2 { continue @@ -212,3 +213,13 @@ const ( Boolean Float ) + +// getSortedEnvs returns a sorted slice of environment variables +func getSortedEnvs() []string { + envs := os.Environ() + // Sort environment variables by key + sort.Slice(envs, func(i, j int) bool { + return strings.SplitN(envs[i], "=", 2)[0] < strings.SplitN(envs[j], "=", 2)[0] + }) + return envs +}