diff options
| -rw-r--r-- | docs/content/functions/env.md | 26 | ||||
| -rw-r--r-- | docs/content/functions/general.md | 18 | ||||
| -rw-r--r-- | docs/content/syntax.md | 2 | ||||
| -rw-r--r-- | env/env.go (renamed from env.go) | 8 | ||||
| -rw-r--r-- | env/env_test.go | 14 | ||||
| -rw-r--r-- | env_test.go | 15 | ||||
| -rw-r--r-- | funcs.go | 3 | ||||
| -rw-r--r-- | funcs/env.go | 34 | ||||
| -rw-r--r-- | funcs/env_test.go | 16 | ||||
| -rw-r--r-- | gomplate_test.go | 2 | ||||
| -rw-r--r-- | main.go | 2 | ||||
| -rw-r--r-- | test/integration/envvars.bats | 6 |
12 files changed, 102 insertions, 44 deletions
diff --git a/docs/content/functions/env.md b/docs/content/functions/env.md new file mode 100644 index 00000000..ebbf8427 --- /dev/null +++ b/docs/content/functions/env.md @@ -0,0 +1,26 @@ +--- +title: env functions +menu: + main: + parent: functions +--- + +## `env.Getenv` + +**Alias:** `getenv` + +Exposes the [os.Getenv](https://golang.org/pkg/os/#Getenv) function. + +This is a more forgiving alternative to using `.Env`, since missing keys will +return an empty string. + +An optional default value can be given as well. + +#### Example + +```console +$ gomplate -i 'Hello, {{env.Getenv "USER"}}' +Hello, hairyhenderson +$ gomplate -i 'Hey, {{getenv "FIRSTNAME" "you"}}!' +Hey, you! +```
\ No newline at end of file diff --git a/docs/content/functions/general.md b/docs/content/functions/general.md index 32aa6820..ab362573 100644 --- a/docs/content/functions/general.md +++ b/docs/content/functions/general.md @@ -5,24 +5,6 @@ menu: parent: functions --- -## `getenv` - -Exposes the [os.Getenv](https://golang.org/pkg/os/#Getenv) function. - -This is a more forgiving alternative to using `.Env`, since missing keys will -return an empty string. - -An optional default value can be given as well. - -#### Example - -```console -$ gomplate -i 'Hello, {{getenv "USER"}}' -Hello, hairyhenderson -$ gomplate -i 'Hey, {{getenv "FIRSTNAME" "you"}}!' -Hey, you! -``` - ## `bool` Converts a true-ish string to a boolean. Can be used to simplify conditional statements based on environment variables or other text input. diff --git a/docs/content/syntax.md b/docs/content/syntax.md index e68af3b5..cbd63f4c 100644 --- a/docs/content/syntax.md +++ b/docs/content/syntax.md @@ -13,7 +13,7 @@ will fail and `gomplate` will exit with an error condition. Sometimes, this behaviour is desired; if the output is unusable without certain strings, this is a sure way to know that variables are missing! -If you want different behaviour, try [`getenv`](../functions/#getenv). +If you want different behaviour, try [`getenv`](../functions/env/#env-getenv). ## Built-in functions @@ -1,15 +1,11 @@ -package main +package env import "os" -// Env - functions that deal with the environment -type Env struct { -} - // Getenv retrieves the value of the environment variable named by the key. // It returns the value, or the default (or an emptry string) if the variable is // not set. -func (e *Env) Getenv(key string, def ...string) string { +func Getenv(key string, def ...string) string { val := os.Getenv(key) if val == "" && len(def) > 0 { return def[0] diff --git a/env/env_test.go b/env/env_test.go new file mode 100644 index 00000000..ba971d82 --- /dev/null +++ b/env/env_test.go @@ -0,0 +1,14 @@ +package env + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetenv(t *testing.T) { + assert.Empty(t, Getenv("FOOBARBAZ")) + assert.Equal(t, os.Getenv("USER"), Getenv("USER")) + assert.Equal(t, "default value", Getenv("BLAHBLAHBLAH", "default value")) +} diff --git a/env_test.go b/env_test.go deleted file mode 100644 index a01fbcdb..00000000 --- a/env_test.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -import ( - "os" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestGetenv(t *testing.T) { - e := &Env{} - assert.Empty(t, e.Getenv("FOOBARBAZ")) - assert.Equal(t, os.Getenv("USER"), e.Getenv("USER")) - assert.Equal(t, "default value", e.Getenv("BLAHBLAHBLAH", "default value")) -} @@ -9,11 +9,9 @@ import ( // initFuncs - The function mappings are defined here! func initFuncs(data *Data) template.FuncMap { - env := &Env{} typeconv := &TypeConv{} f := template.FuncMap{ - "getenv": env.Getenv, "bool": typeconv.Bool, "has": typeconv.Has, "json": typeconv.JSON, @@ -42,5 +40,6 @@ func initFuncs(data *Data) template.FuncMap { funcs.AddNetFuncs(f) funcs.AddReFuncs(f) funcs.AddStringFuncs(f) + funcs.AddEnvFuncs(f) return f } diff --git a/funcs/env.go b/funcs/env.go new file mode 100644 index 00000000..40c10ff9 --- /dev/null +++ b/funcs/env.go @@ -0,0 +1,34 @@ +package funcs + +import ( + "sync" + + "github.com/hairyhenderson/gomplate/env" +) + +var ( + ef *EnvFuncs + efInit sync.Once +) + +// EnvNS - the Env namespace +func EnvNS() *EnvFuncs { + efInit.Do(func() { ef = &EnvFuncs{} }) + return ef +} + +// AddEnvFuncs - +func AddEnvFuncs(f map[string]interface{}) { + f["env"] = EnvNS + + // global aliases - for backwards compatibility + f["getenv"] = EnvNS().Getenv +} + +// EnvFuncs - +type EnvFuncs struct{} + +// Getenv - +func (f *EnvFuncs) Getenv(key string, def ...string) string { + return env.Getenv(key, def...) +} diff --git a/funcs/env_test.go b/funcs/env_test.go new file mode 100644 index 00000000..95db230b --- /dev/null +++ b/funcs/env_test.go @@ -0,0 +1,16 @@ +package funcs + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEnvGetenv(t *testing.T) { + ef := &EnvFuncs{} + expected := os.Getenv("USER") + assert.Equal(t, expected, ef.Getenv("USER")) + + assert.Equal(t, "foo", ef.Getenv("bogusenvvar", "foo")) +} diff --git a/gomplate_test.go b/gomplate_test.go index 1d55751b..cd940d30 100644 --- a/gomplate_test.go +++ b/gomplate_test.go @@ -9,6 +9,7 @@ import ( "text/template" "github.com/hairyhenderson/gomplate/aws" + "github.com/hairyhenderson/gomplate/env" "github.com/stretchr/testify/assert" ) @@ -19,7 +20,6 @@ func testTemplate(g *Gomplate, template string) string { } func TestGetenvTemplates(t *testing.T) { - env := &Env{} typeconv := &TypeConv{} g := &Gomplate{ funcMap: template.FuncMap{ @@ -5,6 +5,7 @@ import ( "fmt" "os" + "github.com/hairyhenderson/gomplate/env" "github.com/hairyhenderson/gomplate/version" "github.com/spf13/cobra" ) @@ -83,7 +84,6 @@ func initFlags(command *cobra.Command) { command.Flags().StringArrayVarP(&opts.dataSources, "datasource", "d", nil, "`datasource` in alias=URL form. Specify multiple times to add multiple sources.") command.Flags().StringArrayVarP(&opts.dataSourceHeaders, "datasource-header", "H", nil, "HTTP `header` field in 'alias=Name: value' form to be provided on HTTP-based data sources. Multiples can be set.") - env := &Env{} ldDefault := env.Getenv("GOMPLATE_LEFT_DELIM", "{{") rdDefault := env.Getenv("GOMPLATE_RIGHT_DELIM", "}}") command.Flags().StringVar(&opts.lDelim, "left-delim", ldDefault, "override the default left-`delimiter` [$GOMPLATE_LEFT_DELIM]") diff --git a/test/integration/envvars.bats b/test/integration/envvars.bats index c8a69ddd..316f2572 100644 --- a/test/integration/envvars.bats +++ b/test/integration/envvars.bats @@ -44,3 +44,9 @@ load helper [ "$status" -eq 0 ] [[ "${output}" == "${HOME}" ]] } + +@test "existant env var using env.Getenv" { + gomplate -i '{{env.Getenv "HOME"}}' + [ "$status" -eq 0 ] + [[ "${output}" == "${HOME}" ]] +} |
