From faa2c436123a3b9daf60e131a0e0a991fb3e0d0d Mon Sep 17 00:00:00 2001 From: Eugene Dementiev Date: Tue, 28 Jan 2020 15:21:40 +1300 Subject: [PATCH] Add "env" template func to get env var value --- ssm/transformations/template_funcs.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ssm/transformations/template_funcs.go b/ssm/transformations/template_funcs.go index 3b4fec8..8abab5c 100644 --- a/ssm/transformations/template_funcs.go +++ b/ssm/transformations/template_funcs.go @@ -1,12 +1,15 @@ package transformations import ( + "fmt" "net/url" + "os" "strings" "text/template" ) var funcMap = template.FuncMap{ + "env": GetEnv, "url_host": URLHost, "url_password": URLPassword, "url_path": URLPath, @@ -16,6 +19,15 @@ var funcMap = template.FuncMap{ "replace": strings.Replace, } +// GetEnv gets the environment variable +func GetEnv(input string) (string, error) { + val, ok := os.LookupEnv(input) + if !ok { + return "", fmt.Errorf("can't find %s in the environment variables", input) + } + return val, nil +} + // URLUser extracts user from the URL or returns "" if it's not set func URLUser(input string) (string, error) { u, err := url.Parse(input)