Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pongo2 support #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ goenvtemplator.tar.xz
goenvtemplator2
goenvtemplator2-amd64
goenvtemplator2.tar.xz

vendor
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ and [Sprig](https://github.com/Masterminds/sprig) library.

### Built-in functions
There are a few built in functions as well:
* `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) }}`
* `{{ 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) }}`
* `{{ range $key, $value := envall }}{{ $key }}={{ $value }};{{ end }}` - Loop over every environment variable.

### Nested Go templates
If you have nested Go templates there is problem with escaping. To resolve this problem you can define different
Expand Down
7 changes: 7 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
goenvtemplator (2.1.0) UNRELEASED; urgency=medium

* Support for pongo2 template language
* Simple API for fast template language implementation

-- Jakub Dusek <jakub.dusek@firma.seznam.cz> Fri, 20 Jul 2017 17:42:19 +0200

goenvtemplator (2.0.0~rc3) Seznam; urgency=medium

* Binary name changed to goenvtemplator2 to support dual installation of both major versions.
Expand Down
53 changes: 43 additions & 10 deletions doc/goenvtemplator2.1
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,49 @@ goenvtemplator2 [OPTIONS]
goenvtemplator2 is a simple app, that can template your config files by environment variables and optionally replace itself (by exec syscall) with the application binary. So at the end your application runs directly under the process that run this tool like docker as if it was originally the entrypoint itself.

.SH OPTIONS
Possible OPTIONS are:
-debug-templates Print processed templates to stdout.
-env-file FILE Additional file with environment variables. Can be
passed multiple times. (default [])
-exec COMMAND ARGS Activates exec by command. First non-flag arguments is
the command, the rest are it's arguments.
-template VALUE Template (/template:/dest). Can be passed multiple
times. (default [])
-v INT Verbosity level.
-version Prints version.
.TP
.BI "\-alsologtostderr"\fR
log to standard error as well as files
.TP
.BI "\-delim-left \fIstring"\fR
(text/template only) Override default left delimiter {{.
.TP
.BI "\-delim-right \fIstring"
(text/template only) Override default right delimiter }}.
.TP
.BI "\-engine \fIstring"
Override default text/template [supports: text/template, pongo] (default "text/template")
.TP
.BI "\-env-file \fIvalue"
Additional file with environment variables. Can be passed multiple times.
.TP
.BI "\-exec"\fR
Activates exec by command. First non-flag arguments is the command, the rest are it's arguments.
.TP
.BI "\-log_backtrace_at \fIvalue"
when logging hits line file:N, emit a stack trace
.TP
.BI "\-log_dir \fIstring"
If non-empty, write log files in this directory
.TP
.BI "\-logtostderr"
log to standard error instead of files
.TP
.BI "\-stderrthreshold \fIvalue"
logs at or above this threshold go to stderr
.TP
.BI "\-template \fIvalue"
Template (/template:/dest). Can be passed multiple times.
.TP
.BI "\-v \fIvalue"
log level for V logs
.TP
.BI "\-version"
Prints version.
.TP
.BI "\-vmodule \fIvalue"
comma-separated list of pattern=N settings for file-filtered logging

.SH SEE ALSO
envsubst(1), env(1)
.SH BUGS
Expand Down
40 changes: 40 additions & 0 deletions engine/pongo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package engine

import (
"os"
"strings"

"github.com/flosch/pongo2"
log "github.com/golang/glog"
)

type PongoTemplar struct {
Source string
}

func (templar *PongoTemplar) GenerateTemplate() (string, error) {
context := pongo2.Context{}

tmpl, err := pongo2.FromString(templar.Source)
if err != nil {
return "", err
}

for _, val := range os.Environ() {
parts := strings.SplitN(val, "=", 2)
key, value := parts[0], parts[1]

context[key] = value
}

if log.V(3) {
log.Info("Using context %v", context)
}

out, err := tmpl.Execute(context)
if err != nil {
return "", err
}

return out, nil
}
5 changes: 5 additions & 0 deletions engine/templar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package engine

type Templar interface {
GenerateTemplate() (string, error)
}
108 changes: 108 additions & 0 deletions engine/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package engine

import (
"bytes"
"errors"
"flag"
"fmt"
"os"
"strings"
"text/template"

"github.com/Masterminds/sprig"
)

type TextTemplar struct {
Source string
Name string
}

type OptionalString struct {
ptr *string
}

var (
delimLeft string
delimRight string
)

func init() {
flag.StringVar(
&delimLeft,
"delim-left",
"",
"(text/template only) Override default left delimiter {{.",
)
flag.StringVar(
&delimRight,
"delim-right",
"",
"(text/template only) Override default right delimiter }}.",
)
}

func (s OptionalString) String() string {
if s.ptr == nil {
return ""
}
return *s.ptr
}

func Require(arg interface{}) (string, error) {
if arg == nil {
return "", errors.New("Required argument is missing!")
}

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
}
}

return "", fmt.Errorf("Requires: unsupported type '%T'!", arg)
}

func EnvAll() (map[string]string, error) {
res := make(map[string]string)

for _, item := range os.Environ() {
split := strings.Split(item, "=")
res[split[0]] = strings.Join(split[1:], "=")
}

return res, nil
}

var funcMap = template.FuncMap{
"require": Require,
"envall": EnvAll,
}

func (templar *TextTemplar) GenerateTemplate() (string, error) {
t, err := template.New(templar.Name).
Delims(delimLeft, delimRight).
Option("missingkey=error").
Funcs(funcMap).
Funcs(sprig.TxtFuncMap()).
Parse(templar.Source)

if err != nil {
return "", err
}

var buffer bytes.Buffer
// hacking because go 1.7 fails to throw error, see https://github.com/golang/go/commit/277bcbbdcd26f2d64493e596238e34b47782f98e
emptyHash := map[string]interface{}{}
if err = t.Execute(&buffer, &emptyHash); err != nil {
return "", err
}

return buffer.String(), nil
}
10 changes: 7 additions & 3 deletions glide.lock

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

3 changes: 3 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ import:
version: v1
- package: github.com/Masterminds/sprig
version: 2.8.0
- package: github.com/flosch/pongo2
version: v3
- package: github.com/golang/glog
Loading