-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move template renderer to its own file (#105)
- Loading branch information
Showing
2 changed files
with
102 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package file | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
func getPrefixedEnvVar(key string) (string, error) { | ||
const envVarPrefix = "DECK_" | ||
if !strings.HasPrefix(key, envVarPrefix) { | ||
return "", fmt.Errorf("environment variables in the state file must "+ | ||
"be prefixed with 'DECK_', found: '%s'", key) | ||
} | ||
value, exists := os.LookupEnv(key) | ||
if !exists { | ||
return "", fmt.Errorf("environment variable '%s' present in state file but not set", key) | ||
} | ||
return value, nil | ||
} | ||
|
||
// getPrefixedEnvVarMocked is used when we mock the env variables while rendering a template. | ||
// It will always return the name of the environment variable in this case. | ||
func getPrefixedEnvVarMocked(key string) (string, error) { | ||
const envVarPrefix = "DECK_" | ||
if !strings.HasPrefix(key, envVarPrefix) { | ||
return "", fmt.Errorf("environment variables in the state file must "+ | ||
"be prefixed with 'DECK_', found: '%s'", key) | ||
} | ||
return key, nil | ||
} | ||
|
||
func toBool(key string) (bool, error) { | ||
return strconv.ParseBool(key) | ||
} | ||
|
||
// toBoolMocked is used when we mock the env variables while rendering a template. | ||
// It will always return false in this case. | ||
func toBoolMocked(_ string) (bool, error) { | ||
return false, nil | ||
} | ||
|
||
func toInt(key string) (int, error) { | ||
return strconv.Atoi(key) | ||
} | ||
|
||
// toIntMocked is used when we mock the env variables while rendering a template. | ||
// It will always return 42 in this case. | ||
func toIntMocked(_ string) (int, error) { | ||
return 42, nil | ||
} | ||
|
||
func toFloat(key string) (float64, error) { | ||
return strconv.ParseFloat(key, 64) | ||
} | ||
|
||
// toFloatMocked is used when we mock the env variables while rendering a template. | ||
// It will always return 42 in this case. | ||
func toFloatMocked(_ string) (float64, error) { | ||
return 42, nil | ||
} | ||
|
||
func indent(spaces int, v string) string { | ||
pad := strings.Repeat(" ", spaces) | ||
return strings.Replace(v, "\n", "\n"+pad, -1) | ||
} | ||
|
||
func renderTemplate(content string, mockEnvVars bool) (string, error) { | ||
var templateFuncs template.FuncMap | ||
if mockEnvVars { | ||
templateFuncs = template.FuncMap{ | ||
"env": getPrefixedEnvVarMocked, | ||
"toBool": toBoolMocked, | ||
"toInt": toIntMocked, | ||
"toFloat": toFloatMocked, | ||
"indent": indent, | ||
} | ||
} else { | ||
templateFuncs = template.FuncMap{ | ||
"env": getPrefixedEnvVar, | ||
"toBool": toBool, | ||
"toInt": toInt, | ||
"toFloat": toFloat, | ||
"indent": indent, | ||
} | ||
} | ||
t := template.New("state").Funcs(templateFuncs).Delims("${{", "}}") | ||
|
||
t, err := t.Parse(content) | ||
if err != nil { | ||
return "", err | ||
} | ||
var buffer bytes.Buffer | ||
err = t.Execute(&buffer, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
return buffer.String(), nil | ||
} |