Skip to content

Commit

Permalink
Merge pull request #110 from ricoberger/parse-environment-variables-i…
Browse files Browse the repository at this point in the history
…n-configuration

Parse Environment Variables in Configuration File
  • Loading branch information
ricoberger authored Jan 10, 2024
2 parents ab327fe + 95fd360 commit 6320401
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (c *Config) LoadConfig(file string) error {
return err
}

data = []byte(expandEnv(string(data)))

err = yaml.Unmarshal(data, &c)
if err != nil {
return err
Expand All @@ -83,6 +85,13 @@ func (c *Config) LoadConfig(file string) error {
return nil
}

// expandEnv replaces all environment variables in the provided string. The environment variables can be in the form
// `${var}` or `$var`. If the string should contain a `$` it can be escaped via `$$`.
func expandEnv(s string) string {
os.Setenv("CRANE_DOLLAR", "$")
return os.ExpandEnv(strings.Replace(s, "$$", "${CRANE_DOLLAR}", -1))
}

// ValidateConfig validates no contradictory config options are set.
func ValidateConfig(c Config) []error {
var errs []error
Expand Down

0 comments on commit 6320401

Please sign in to comment.