Skip to content

Commit

Permalink
Load array env
Browse files Browse the repository at this point in the history
  • Loading branch information
luanxuechao committed May 21, 2024
1 parent f9f2fe7 commit 19b55a0
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions cmd/relayproxy/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -91,10 +92,17 @@ func New(flagSet *pflag.FlagSet, log *zap.Logger, version string) (*Config, erro
log.Error("error loading file", zap.Error(errBindFile))
}
}

configMap := k.Raw()
// Map environment variables
_ = k.Load(env.Provider("", ".", func(s string) string {
return strings.ReplaceAll(strings.ToLower(s), "_", ".")
_ = k.Load(env.ProviderWithValue("", ".", func(s string, v string) (string, interface{}) {
if strings.HasPrefix(s, "RETRIEVERS") || strings.HasPrefix(s, "NOTIFIERS") {
err := loadArrayEnv(s, v, configMap)
if err != nil {
fmt.Errorf("init array env fail %s", s)

Check failure on line 101 in cmd/relayproxy/config/config.go

View workflow job for this annotation

GitHub Actions / Lint

unusedresult: result of fmt.Errorf call not used (govet)

Check warning on line 101 in cmd/relayproxy/config/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/relayproxy/config/config.go#L99-L101

Added lines #L99 - L101 were not covered by tests
}
return s, v

Check warning on line 103 in cmd/relayproxy/config/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/relayproxy/config/config.go#L103

Added line #L103 was not covered by tests
}
return strings.ReplaceAll(strings.ToLower(s), "_", "."), v
}), nil)

_ = k.Set("version", version)
Expand Down Expand Up @@ -322,3 +330,45 @@ func locateConfigFile(inputFilePath string) (string, error) {
return "", fmt.Errorf(
"impossible to find config file in the default locations [%s]", strings.Join(defaultLocations, ","))
}

// Load the ENV Like:RETRIEVERS_0_HEADERS_AUTHORIZATION
func loadArrayEnv(s string, v string, configMap map[string]interface{}) error {
paths := strings.Split(s, "_")
for i, str := range paths {
paths[i] = strings.ToLower(str)

Check warning on line 338 in cmd/relayproxy/config/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/relayproxy/config/config.go#L335-L338

Added lines #L335 - L338 were not covered by tests
}
prefixKey := paths[0]
if configArray, ok := configMap[prefixKey].([]interface{}); ok {
index, err := strconv.Atoi(paths[1])
if err != nil {
return err

Check warning on line 344 in cmd/relayproxy/config/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/relayproxy/config/config.go#L340-L344

Added lines #L340 - L344 were not covered by tests
}
configItem := configArray[index].(map[string]interface{})
keys := paths[2:]
currentMap := configItem
for i, key := range keys {
hasKey := false
lowerKey := key
for y := range currentMap {
if y != lowerKey {
continue

Check warning on line 354 in cmd/relayproxy/config/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/relayproxy/config/config.go#L346-L354

Added lines #L346 - L354 were not covered by tests
}
if nextMap, ok := currentMap[y].(map[string]interface{}); ok {
currentMap = nextMap
hasKey = true
break

Check warning on line 359 in cmd/relayproxy/config/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/relayproxy/config/config.go#L356-L359

Added lines #L356 - L359 were not covered by tests
}
}
if !hasKey && i != len(keys)-1 {
newMap := make(map[string]interface{})
currentMap[lowerKey] = newMap
currentMap = newMap

Check warning on line 365 in cmd/relayproxy/config/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/relayproxy/config/config.go#L362-L365

Added lines #L362 - L365 were not covered by tests
}
}
lastKey := keys[len(keys)-1]
currentMap[lastKey] = v
configArray[index] = configItem
k.Set(prefixKey, configArray)

Check failure on line 371 in cmd/relayproxy/config/config.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `k.Set` is not checked (errcheck)

Check warning on line 371 in cmd/relayproxy/config/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/relayproxy/config/config.go#L368-L371

Added lines #L368 - L371 were not covered by tests
}
return nil

Check warning on line 373 in cmd/relayproxy/config/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/relayproxy/config/config.go#L373

Added line #L373 was not covered by tests
}

0 comments on commit 19b55a0

Please sign in to comment.