Skip to content

Commit

Permalink
feat: add toBool function for template
Browse files Browse the repository at this point in the history
  • Loading branch information
zekth authored and GGabriele committed Mar 17, 2023
1 parent 37634ae commit ff70966
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
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 }}

0 comments on commit ff70966

Please sign in to comment.