Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/master: add envOrDefault helper #1461

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/templating-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ provides the following functions:
- [containsNone](#containsnone)
- [containsNotAll](#containsnotall)
- [env](#env)
- [envOrDefault](#envOrDefault)
- [executeTemplate](#executetemplate)
- [explode](#explode)
- [explodeMap](#explodemap)
Expand Down Expand Up @@ -1046,6 +1047,20 @@ Reads the given environment variable and if it does not exist or is blank use a
{{ or (env "CLUSTER_ID") "12345" }}
```

### `envOrDefault`

Reads the given environment variable accessible to the current process. If the environment variable is found, the value of that variable will be used. This includes empty values. Otherwise, the default will be used instead.

```liquid
{{ envOrDefault "CLUSTER_NAME" "Default_Cluster" }}
```

This function can be chained to manipulate the output:

```liquid
{{ envOrDefault "CLUSTER_NAME" "Default_Cluster" | toLower }}
```

### `executeTemplate`

Executes and returns a defined template.
Expand Down
22 changes: 22 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ func envFunc(env []string) func(string) (string, error) {
}
}

// envWithDefaultFunc returns a function which checks the value of an environment variable.
// Invokers can specify their own environment, which takes precedences over any
// real environment variables.
// If an environment variable is found, the value of that variable will be used. This includes empty values.
// Otherwise, the default will be used instead.
func envWithDefaultFunc(env []string) func(string, string) (string, error) {
return func(s string, def string) (string, error) {
for _, e := range env {
split := strings.SplitN(e, "=", 2)
k, v := split[0], split[1]
if k == s {
return v, nil
}
}
val, isPresent := os.LookupEnv(s)
if isPresent {
return val, nil
}
return def, nil
}
}

// executeTemplateFunc executes the given template in the context of the
// parent. If an argument is specified, it will be used as the context instead.
// This can be used for nested template definitions.
Expand Down
1 change: 1 addition & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func funcMap(i *funcMapInput) template.FuncMap {
"containsNone": containsSomeFunc(true, false),
"containsNotAll": containsSomeFunc(false, true),
"env": envFunc(i.env),
"envOrDefault": envWithDefaultFunc(i.env),
"executeTemplate": executeTemplateFunc(i.t),
"explode": explode,
"explodeMap": explodeMap,
Expand Down
17 changes: 17 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,23 @@ func TestTemplate_Execute(t *testing.T) {
"[dc1 dc2]",
false,
},
{
"func_envOrDefault",
&NewTemplateInput{
Contents: `{{ envOrDefault "SET_VAR" "100" }} {{ envOrDefault "EMPTY_VAR" "200" }} {{ envOrDefault "UNSET_VAR" "300" }}`,
},
&ExecuteInput{
Env: func() []string {
return []string{"SET_VAR=400", "EMPTY_VAR="}
}(),
Brain: func() *Brain {
b := NewBrain()
return b
}(),
},
"400 300",
false,
},
{
"func_file",
&NewTemplateInput{
Expand Down