Skip to content

Commit

Permalink
feat: text/template flags moved to module
Browse files Browse the repository at this point in the history
  • Loading branch information
tcpj committed Jun 15, 2018
1 parent 885819a commit 66e0dad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
29 changes: 24 additions & 5 deletions engine/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
"bytes"
"errors"
"flag"
"fmt"
"os"
"strings"
Expand All @@ -12,16 +13,34 @@ import (
)

type TextTemplar struct {
Source string
Name string
DelimLeft string
DelimRight string
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 ""
Expand Down Expand Up @@ -68,7 +87,7 @@ var funcMap = template.FuncMap{

func (templar *TextTemplar) GenerateTemplate() (string, error) {
t, err := template.New(templar.Name).
Delims(templar.DelimLeft, templar.DelimRight).
Delims(delimLeft, delimRight).
Option("missingkey=error").
Funcs(funcMap).
Funcs(sprig.TxtFuncMap()).
Expand Down
24 changes: 8 additions & 16 deletions goenvtemplator.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ func readFile(templatePath string) (string, error) {

}

func generateTemplates(
ts templatesPaths,
delimLeft string,
delimRight string,
engineName string) error {

func generateTemplates(ts templatesPaths, engineName string) error {
for _, t := range ts {
if log.V(1) {
log.Info("generating %s -> %s", t.source, t.destination)
Expand All @@ -117,10 +112,8 @@ func generateTemplates(
}
case "text/template":
templar = &engine.TextTemplar{
Source: source,
Name: templateName,
DelimLeft: delimLeft,
DelimRight: delimRight,
Source: source,
Name: templateName,
}
}

Expand Down Expand Up @@ -150,17 +143,16 @@ func main() {
var doExec bool
var printVersion bool
var envFileList envFiles
var delimLeft string
var delimRight string
var engine string

flag.Var(&tmpls, "template", "Template (/template:/dest). Can be passed multiple times.")
flag.BoolVar(&doExec, "exec", false, "Activates exec by command. First non-flag arguments is the command, the rest are it's arguments.")
flag.BoolVar(&printVersion, "version", false, "Prints version.")
flag.Var(&envFileList, "env-file", "Additional file with environment variables. Can be passed multiple times.")
flag.StringVar(&delimLeft, "delim-left", "", "Override default left delimiter {{.")
flag.StringVar(&delimRight, "delim-right", "", "Override default right delimiter }}.")
flag.StringVar(&engine, "engine", "text/template", "Override default text/template [support: pongo2]")
flag.StringVar(
&engine, "engine", "text/template",
"Override default text/template [supports: text/template, pongo]",
)

flag.Parse()
// if no env-file was passed, godotenv.Load loads .env file by default, we want to disable this
Expand All @@ -179,7 +171,7 @@ func main() {
log.Info("Generating templates")
}

if err := generateTemplates(tmpls, delimLeft, delimRight, engine); err != nil {
if err := generateTemplates(tmpls, engine); err != nil {
log.Fatal(err)
}

Expand Down

0 comments on commit 66e0dad

Please sign in to comment.