Skip to content

Commit 6320401

Browse files
authored
Merge pull request #110 from ricoberger/parse-environment-variables-in-configuration
Parse Environment Variables in Configuration File
2 parents ab327fe + 95fd360 commit 6320401

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

pkg/config/config.go

+9
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func (c *Config) LoadConfig(file string) error {
7575
return err
7676
}
7777

78+
data = []byte(expandEnv(string(data)))
79+
7880
err = yaml.Unmarshal(data, &c)
7981
if err != nil {
8082
return err
@@ -83,6 +85,13 @@ func (c *Config) LoadConfig(file string) error {
8385
return nil
8486
}
8587

88+
// expandEnv replaces all environment variables in the provided string. The environment variables can be in the form
89+
// `${var}` or `$var`. If the string should contain a `$` it can be escaped via `$$`.
90+
func expandEnv(s string) string {
91+
os.Setenv("CRANE_DOLLAR", "$")
92+
return os.ExpandEnv(strings.Replace(s, "$$", "${CRANE_DOLLAR}", -1))
93+
}
94+
8695
// ValidateConfig validates no contradictory config options are set.
8796
func ValidateConfig(c Config) []error {
8897
var errs []error

0 commit comments

Comments
 (0)