tpl: Update getenv to accept interface{} params
authorCameron Moore <moorereason@gmail.com>
Fri, 23 Dec 2016 04:05:59 +0000 (22:05 -0600)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 23 Dec 2016 08:51:03 +0000 (09:51 +0100)
Updates #2822

tpl/template_funcs.go

index 7516b46adc1204f69db0ca2bba6447ee89a093bb..db3ba37bc4fbadebd62f5b510cef5bc0907db808 100644 (file)
@@ -2075,6 +2075,17 @@ func relURL(a interface{}) (template.HTML, error) {
        return template.HTML(helpers.CurrentPathSpec().RelURL(s, false)), nil
 }
 
+// getenv retrieves the value of the environment variable named by the key.
+// It returns the value, which will be empty if the variable is not present.
+func getenv(key interface{}) (string, error) {
+       skey, err := cast.ToStringE(key)
+       if err != nil {
+               return "", nil
+       }
+
+       return os.Getenv(skey), nil
+}
+
 func initFuncMap() {
        funcMap = template.FuncMap{
                "absURL": absURL,
@@ -2106,7 +2117,7 @@ func initFuncMap() {
                "ge":            ge,
                "getCSV":        getCSV,
                "getJSON":       getJSON,
-               "getenv":        func(varName string) string { return os.Getenv(varName) },
+               "getenv":        getenv,
                "gt":            gt,
                "hasPrefix":     hasPrefix,
                "highlight":     highlight,