Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from usedatabrew/feat/env-variables
Browse files Browse the repository at this point in the history
Feat/env variables
  • Loading branch information
le-vlad authored Nov 30, 2023
2 parents a81a202 + c856e36 commit 762cb56
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions public/stream/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ package stream

import (
"blink/config"
"os"
"regexp"
"strings"

"gopkg.in/yaml.v3"
)

var (
envRegex = regexp.MustCompile(`\${[0-9A-Za-z_.]+(:((\${[^}]+})|[^}])*)?}`)
escapedEnvRegex = regexp.MustCompile(`\${({[0-9A-Za-z_.]+(:((\${[^}]+})|[^}])*)?})}`)
)

func ReadInitConfigFromYaml(configBytes []byte) (config.Configuration, error) {
replacedBytes := replaceEnvVariables(configBytes, os.LookupEnv)

conf := config.Configuration{}
err := yaml.Unmarshal(configBytes, &conf)
err := yaml.Unmarshal(replacedBytes, &conf)

config.ValidateConfigSchema(conf)
return conf, err
}
Expand All @@ -17,7 +29,25 @@ func ReadDriverConfig[T any](driverConfig interface{}, targetConfig T) (T, error
if err != nil {
return targetConfig, err
}
err = yaml.Unmarshal(marshaled, &targetConfig)

replacedBytes := replaceEnvVariables(marshaled, os.LookupEnv)

err = yaml.Unmarshal(replacedBytes, &targetConfig)

return targetConfig, err
}

func replaceEnvVariables(config []byte, lookupFn func(string) (string, bool)) []byte {
replaced := envRegex.ReplaceAllFunc(config, func(content []byte) []byte {
var value string
if len(content) > 3 {
varName := string(content[2 : len(content)-1])
value, _ = lookupFn(varName)
value = strings.ReplaceAll(value, "\n", "\\n")
}
return []byte(value)
})
replaced = escapedEnvRegex.ReplaceAll(replaced, []byte("$$$1"))

return replaced
}

0 comments on commit 762cb56

Please sign in to comment.