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

feat: add toBool function for template #867

Merged
merged 1 commit into from
Mar 17, 2023
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
8 changes: 7 additions & 1 deletion file/readfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"text/template"

Expand Down Expand Up @@ -145,9 +146,14 @@ func getPrefixedEnvVar(key string) (string, error) {
return value, nil
}

func toBool(key string) (bool, error) {
return strconv.ParseBool(key)
}

func renderTemplate(content string) (string, error) {
t := template.New("state").Funcs(template.FuncMap{
"env": getPrefixedEnvVar,
"env": getPrefixedEnvVar,
"toBool": toBool,
}).Delims("${{", "}}")
t, err := t.Parse(content)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions file/readfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,33 @@ func Test_getContent(t *testing.T) {
},
wantErr: false,
},
{
name: "file with env var and parse bool",
args: args{[]string{"testdata/parsebool/file.yaml"}},
envVars: map[string]string{
"DECK_MOCKBIN_ENABLED": "true",
},
want: &Content{
Services: []FService{
{
Service: kong.Service{
Name: kong.String("svc1"),
Host: kong.String("mockbin.org"),
Enabled: kong.Bool(true),
},
},
},
},
wantErr: false,
},
{
name: "file with env var and parse bool - err on bad value",
args: args{[]string{"testdata/parsebool/file.yaml"}},
envVars: map[string]string{
"DECK_MOCKBIN_ENABLED": "RIP",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions file/testdata/parsebool/file.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
- name: svc1
host: mockbin.org
enabled: ${{ env "DECK_MOCKBIN_ENABLED" | toBool }}