Skip to content

Commit

Permalink
adding define/block-support across different sources
Browse files Browse the repository at this point in the history
  • Loading branch information
ninlil committed Apr 27, 2022
1 parent cc9dbbf commit 61018b1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin/
bin/
test/
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ m-j:

multi:
go run . -i test/m-input.yaml -i test/m-input.json test/m-tmpl.yaml -v -s

adv:
go run . -i test/advanced/input.yaml test/advanced/*.tmpl8 -v
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ Generic (and Kubernetes-friendly) Templating Engine using the go text/template a
Supported input formats: JSON and YAML

```
tmpl8 [--input INPUT] [--output FILE] [--verbose] [--raw] [--split] TEMPLATE [TEMPLATE ...]
tmpl8 [--input INPUT] [--output FILE] [--verbose] [--trace] [--raw] [--split] [--noinput] TEMPLATE [TEMPLATE ...]
```

### Options
| Option | Description |
| ----------------- | ----------- |
| `-i` / `--input` | Manual assign where to read input object(s) from, use '`-`' to use stdin, multiple inputs supported |
| `-o` / `--output` | Set output filename (instead of using `>`) |
| `-r` / `--raw` | Don't ensure each template ends with a newline (not recommended for YAML output)|
| `-s` / `--split` | Split JSON input objects arrays to multiple objects parsed each (similar to YAMLs with `---` separators) |
| Option | Description |
| ----------------- | ----------- |
| `-i` / `--input` | Manual assign where to read input object(s) from, use '`-`' to use stdin, multiple inputs supported |
| `-z` / `--noinput` | Add an empty object as input (for template-only processing) |
| `-o` / `--output` | Set output filename (instead of using `>`) |
| `-r` / `--raw` | Don't ensure each template ends with a newline (not recommended for YAML output)|
| `-s` / `--split` | Split JSON input objects arrays to multiple objects parsed each (similar to YAMLs with `---` separators) |

## define/block support
Any `define`/`block` helpers are useable in templates in all following template sources (and can be overridden)

## Examples

Expand Down
22 changes: 16 additions & 6 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

type arguments struct {
Inputs []string `arg:"-i,--input,separate" help:"filename of objects to import and process (use '-' for stdin)"`
Templates []string `arg:"required,positional" help:"filename.tmpl | @filenames.lst | k8s:namespace/configmap" placeholder:"TEMPLATE"`
Output string `arg:"-o,--output" help:"destination filename" placeholder:"FILE"`
Verbose bool `arg:"-v,--verbose" help:"verbose output"`
Raw bool `arg:"-r,--raw" help:"will NOT ensure newline and end of each block"`
Split bool `arg:"-s,--split" help:"split json-array into separate 'documents'"`
Inputs []string `arg:"-i,--input,separate" help:"filename of objects to import and process (use '-' for stdin)"`
Templates []string `arg:"required,positional" help:"filename.tmpl | @filenames.lst | k8s:namespace/configmap" placeholder:"TEMPLATE"`
Output string `arg:"-o,--output" help:"destination filename" placeholder:"FILE"`
Verbose bool `arg:"-v,--verbose" help:"verbose output"`
VeryVerbose bool `arg:"-t,--trace" help:"trace output"`
Raw bool `arg:"-r,--raw" help:"will NOT ensure newline and end of each block"`
Split bool `arg:"-s,--split" help:"split json-array into separate 'documents'"`
NoInput bool `arg:"-z" help:"no input (equals -i '?{}')"`
}

func (arguments) Description() string {
Expand All @@ -38,6 +40,14 @@ func init() {
os.Exit(1)
}

if args.NoInput {
args.Inputs = append(args.Inputs, "?{}")
}

if args.VeryVerbose {
args.Verbose = true
}

if len(args.Inputs) == 0 {
args.Inputs = append(args.Inputs, "-")
}
Expand Down
41 changes: 29 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ func main() {
log.Info().Msgf("generating %d objects using %d templates", len(olist), len(templates))
}

t, err := prepare(templates)
if err != nil {
log.Error().Msgf("prepare-template-error: %v", err)
os.Exit(1)
}

for _, o := range olist {
err = handle(templates, o, &output)
err = process(t, o, &output)
if err != nil {
log.Error().Msgf("template-error: %v", err)
os.Exit(1)
Expand All @@ -59,24 +65,35 @@ func main() {

}

func handle(list []entry, o interface{}, w io.Writer) error {
// list, err := load(fn)
// if err != nil {
// return err
// }
func prepare(list []entry) ([]*template.Template, error) {
if args.VeryVerbose {
log.Trace().Msgf("preparing %d template", len(list))
}
var tmpls []*template.Template

tmpl := template.New("base").Funcs(sprig.TxtFuncMap())
var err error

for _, e := range list {
if args.Verbose {
log.Trace().Msgf("applying template '%s'...", e.name)
if args.VeryVerbose {
log.Trace().Msgf("parsing template '%s'...", e.name)
}

tmpl, err := template.New("base").Funcs(sprig.TxtFuncMap()).Parse(e.data)
tmpl, err = tmpl.New(e.name).Parse(e.data)
if err != nil {
return err
return nil, err
}
tmpls = append(tmpls, tmpl)
}
return tmpls, nil
}

func process(tmpls []*template.Template, o interface{}, w io.Writer) error {
for _, tmpl := range tmpls {
if args.VeryVerbose {
log.Trace().Msgf("executing template '%s'...", tmpl.Name())
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, o)
err := tmpl.Execute(&buf, o)
if err != nil {
return err
}
Expand Down

0 comments on commit 61018b1

Please sign in to comment.