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

add mustEnv function #1657

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion docs/templating-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ provides the following functions:
- [containsNone](#containsnone)
- [containsNotAll](#containsnotall)
- [env](#env)
- [mustEnv](#mustEnv)
- [envOrDefault](#envOrDefault)
- [executeTemplate](#executetemplate)
- [explode](#explode)
Expand All @@ -60,7 +61,7 @@ provides the following functions:
- [trimSpace](#trimspace)
- [trim](#trim)
- [trimPrefix](#trimprefix)
- [trimSuffix](#trimsuffix)
- [trimSuffix](#trimsuffix)
- [parseBool](#parsebool)
- [parseFloat](#parsefloat)
- [parseInt](#parseint)
Expand Down Expand Up @@ -1140,6 +1141,14 @@ Reads the given environment variable and if it does not exist or is blank use a
{{ or (env "CLUSTER_ID") "12345" }}
```

### `mustEnv`

Reads the given environment variable accessible to the current process. If the variable is not defined or is empty the substitution will fail with error.

```golang
{{ mustEnv "CLUSTER_ID" }}
```

### `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.
Expand Down
25 changes: 25 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ func envFunc(env []string) func(string) (string, error) {
}
}

// mustEnvFunc returns a function which checks the value of an environment variable
// and returns an error if environment variable value is empty.
// Invokers can specify their own environment, which takes precedences over any
// real environment variables.
func mustEnvFunc(env []string) func(string) (string, error) {
return func(s string) (string, error) {
val := ""
for _, e := range env {
split := strings.SplitN(e, "=", 2)
k, v := split[0], split[1]
if k == s {
val = v
break
}
}
if val == "" {
val = os.Getenv(s)
}
if val == "" {
return "", fmt.Errorf("required environment variable %s is empty", s)
}
return val, nil
}
}

// 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.
Expand Down
1 change: 1 addition & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ func funcMap(i *funcMapInput) template.FuncMap {
"containsNone": containsSomeFunc(true, false),
"containsNotAll": containsSomeFunc(false, true),
"env": envFunc(i.env),
"mustEnv": mustEnvFunc(i.env),
"envOrDefault": envWithDefaultFunc(i.env),
"executeTemplate": executeTemplateFunc(i.newTmpl),
"explode": explode,
Expand Down
28 changes: 28 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,34 @@ func TestTemplate_Execute(t *testing.T) {
"1",
false,
},
{
"helper_mustEnv",
&NewTemplateInput{
Contents: `{{ mustEnv "CT_TEST" }}`,
},
&ExecuteInput{
Brain: func() *Brain {
// Cheat and use the brain callback here to set the env.
if err := os.Setenv("CT_TEST", "1"); err != nil {
t.Fatal(err)
}
return NewBrain()
}(),
},
"1",
false,
},
{
"helper_mustEnv_negative",
&NewTemplateInput{
Contents: `{{ mustEnv "CT_TEST_NONEXISTENT" }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"",
true,
},
{
"helper_env__override",
&NewTemplateInput{
Expand Down