Skip to content

Commit

Permalink
Merge pull request #4 from lksv/add_sprint
Browse files Browse the repository at this point in the history
feat: Added Sprig - useful template functions for Go templates
  • Loading branch information
dohnto authored Feb 13, 2017
2 parents abf34cf + 585ed5e commit 58ee4be
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 49 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ goenvtemplator -env-file myenvfile -exec sh -c 'echo $D'
```

## Using Templates
Templates use Golang [text/template](http://golang.org/pkg/text/template/).
Templates use Golang [html/template](http://golang.org/pkg/html/template/)
and [Sprig](https://github.com/Masterminds/sprig) library.

### Built-in functions
There are a few built in functions as well:
* `env "ENV_NAME"` - Accesses environment variables. If it does not exist return empty string. `{{ env "TIMEOUT_MS }}`
* `require (env "ENV_NAME")` - Renders an error if environments variable does not exists. If it is equal to empty string, returns empty string. `{{ require (env "TIMEOUT_MS) }}`
* `default $default_1 $default_2 $default_3` - Returns a first argument that exists. If none is valid it generates error `{{ default (env "SPECIFIC_TIMEOUT_MS") (env "GENERAL_TIMEOUT_MS") "1000" }}`
17 changes: 17 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
goenvtemplator (2.0.0~rc1) Seznam; urgency=medium

* Added Sprig (useful template functions for Go templates)
* Breaking chages
- **`{{ .NonExistsent }}` resolves to empty string**
Before
`{{ .NonExistsent }}` => `<no-value>`
After
`{{ .NonExistsent }}` => ``
- **function `default` is used from `Sprig` and has another params**
Before:
`default (env "VAR") "10"`
After
`env "VAR" | default "10"`

-- Lukas Svoboda <[email protected]> Fri, 10 Feb 2017 16:02:47 +0100

goenvtemplator (1.1.0) Seznam; urgency=medium

* Table driven tests.
Expand Down
4 changes: 3 additions & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ Maintainer: https://github.com/seznam/goenvtemplator
Section: admin
Priority: optional
Build-Depends: debhelper (>= 9), golang (>= 2:1.5), git
Replaces: goenvtemplator
Breaks: goenvtemplator

Package: goenvtemplator
Package: goenvtemplator2
Architecture: amd64
Description: Tool to template configuration files by environment variables.
goenvtemplator is a simple app, that can template your config files by
Expand Down
4 changes: 2 additions & 2 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
+dh $@ --parallel

override_dh_auto_install:
mkdir -p ./debian/goenvtemplator/usr/bin
install -m 0755 goenvtemplator ./debian/goenvtemplator/usr/bin
mkdir -p ./debian/goenvtemplator2/usr/bin
install -m 0755 goenvtemplator ./debian/goenvtemplator2/usr/bin
10 changes: 8 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ package: github.com/seznam/goenvtemplator
import:
- package: github.com/joho/godotenv
version: v1
- package: github.com/Masterminds/sprig
version: 2.8.0
41 changes: 4 additions & 37 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"text/template"
// "io/ioutil"
"bytes"
"github.com/Masterminds/sprig"
"html/template"
"io/ioutil"
"log"
)
Expand All @@ -23,38 +23,6 @@ func (s OptionalString) String() string {
return *s.ptr
}

func Env(key string) OptionalString {
value, ok := os.LookupEnv(key)
if !ok {
return OptionalString{nil}
}
return OptionalString{&value}
}

func Default(args ...interface{}) (string, error) {
for _, arg := range args {
if arg == nil {
continue
}
switch v := arg.(type) {
case string:
return v, nil
case *string:
if v != nil {
return *v, nil
}
case OptionalString:
if v.ptr != nil {
return *v.ptr, nil
}
default:
return "", fmt.Errorf("Default: unsupported type '%T'!", v)
}
}

return "", errors.New("Default: all arguments are nil!")
}

func Require(arg interface{}) (string, error) {
if arg == nil {
return "", errors.New("Required argument is missing!")
Expand All @@ -77,15 +45,14 @@ func Require(arg interface{}) (string, error) {
}

var funcMap = template.FuncMap{
"env": Env,
"default": Default,
"require": Require,
}

func generateTemplate(source, name string) (string, error) {
var t *template.Template
var err error
if t, err = template.New(name).Funcs(funcMap).Parse(source); err != nil {
t, err = template.New(name).Funcs(funcMap).Funcs(sprig.FuncMap()).Parse(source)
if err != nil {
return "", err
}
var buffer bytes.Buffer
Expand Down
18 changes: 14 additions & 4 deletions template_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/joho/godotenv"
"os"
"testing"
)
Expand All @@ -12,17 +13,26 @@ func TestGenerateTemplate(t *testing.T) {
err error
}{
{`K={{ env "GOENVTEMPLATOR_DEFINED_VAR" }}`, `K=foo`, nil},
{`K={{ env "GOENVTEMPLATOR_DEFINED_FILE_VAR" }}`, `K=bar`, nil},
{`K={{ env "NONEXISTING" }}`, `K=`, nil},
{`K={{ .NONEXISTING }}`, `K=<no value>`, nil},
{`K={{ default .NonExisting "default value" }}`, `K=default value`, nil},
{`K={{ default (env "GOENVTEMPLATOR_DEFINED_VAR") }}`, `K=foo`, nil},
{`K={{ default (env "NONEXISTING") "default value" }}`, `K=default value`, nil},
{`K={{ .NONEXISTING }}`, `K=`, nil},
{`K={{ .NonExisting | default "default value" }}`, `K=default value`, nil},
{`K={{ env "GOENVTEMPLATOR_DEFINED_VAR" | default "xxx" }}`, `K=foo`, nil},
{`K={{ env "GOENVTEMPLATOR_DEFINED_FILE_VAR" | default "xxx" }}`, `K=bar`, nil},
{`K={{ env "NONEXISTING"| default "default value" }}`, `K=default value`, nil},
{`{{ "hi!" | upper | repeat 3 }}`, `HI!HI!HI!`, nil},
{`{{$v := "foo/bar/baz" | split "/"}}{{$v._1}}`, `bar`, nil},
}

templateName := "test"

os.Setenv("GOENVTEMPLATOR_DEFINED_VAR", "foo")

err := godotenv.Load("./tests/fixtures.env")
if err != nil {
t.Errorf("Cannot load env file: %q", err)
}

for _, tt := range tests {
got, gotErr := generateTemplate(tt.in, templateName)

Expand Down
1 change: 1 addition & 0 deletions tests/fixtures.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOENVTEMPLATOR_DEFINED_FILE_VAR=bar

0 comments on commit 58ee4be

Please sign in to comment.