Skip to content

Commit

Permalink
Fix order of RPC and wallet keys fetched from env vars (#1150)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lukaszcl authored Sep 23, 2024
1 parent 715d26e commit 2a5f3f4
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 2a5f3f4

Please sign in to comment.