Skip to content

Commit

Permalink
extended template function 'getenv' to allow default values
Browse files Browse the repository at this point in the history
  • Loading branch information
odedlaz committed Mar 6, 2016
1 parent fb2d930 commit d30c12b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion resource/template/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func newFuncMap() map[string]interface{} {
m["jsonArray"] = UnmarshalJsonArray
m["dir"] = path.Dir
m["map"] = CreateMap
m["getenv"] = os.Getenv
m["getenv"] = Getenv
m["join"] = strings.Join
m["datetime"] = time.Now
m["toUpper"] = strings.ToUpper
Expand All @@ -38,6 +38,22 @@ func addFuncs(out, in map[string]interface{}) {
}
}

// Getenv retrieves the value of the environment variable named by the key.
// It returns the value, which will the default value if the variable is not present.
// If no default value was given - returns "".
func Getenv(key string, v ...string) string {
defaultValue := ""
if len(v) > 0 {
defaultValue = v[0]
}

value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}

// CreateMap creates a key-value map of string -> interface{}
// The i'th is the key and the i+1 is the value
func CreateMap(values ...interface{}) (map[string]interface{}, error) {
Expand Down

0 comments on commit d30c12b

Please sign in to comment.