Skip to content

Commit

Permalink
Merge pull request #3 from Syuparn/add-file-options
Browse files Browse the repository at this point in the history
Add file options
  • Loading branch information
Syuparn authored Feb 18, 2021
2 parents 02ff42e + 259fd4f commit cdf7723
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ executable go-template command (like awk and jq!)
$ go get -u github.com/syuparn/tmplscript
$ echo "hello" | tmplscript '{{print . ", " "world!"}}'
hello, world!
# read from file instead
$ seq 15 | tmplscript -f example/fizzbuzz.tmpl
1
2
fizz
...
```

REPL mode
Expand Down
5 changes: 5 additions & 0 deletions example/fizzbuzz.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{- with $i := int . -}}
{{- $fizz := mod $i 3 | eq 0 | ternary "fizz" "" -}}
{{- $buzz := mod $i 5 | eq 0 | ternary "buzz" "" -}}
{{- coalesce (print $fizz $buzz) $i -}}{{- "\n" -}}
{{- end -}}
9 changes: 9 additions & 0 deletions example/template_as_func.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{- define "templateAdd" -}}
{{- /* HACK: assign dummy var to be evaluated as "" */ -}}
{{- $_ := set . "ret" (add (index .args 0) (index .args 1)) -}}
{{- end -}}

{{- with $d := dict "args" (list 2 3) -}}
{{- template "templateAdd" $d -}}
{{- print "result: " $d.ret -}}
{{- end -}}
32 changes: 32 additions & 0 deletions example/template_factorial.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{- define "_fact" -}}
{{- $i := index . 0 -}}
{{- $acc := index . 1 -}}
{{- if le $i 1 -}}
{{- println $acc -}}
{{- else -}}
{{- template "_fact" list (sub $i 1) (mul $acc $i) -}}
{{- end -}}
{{- end -}}

{{- define "fact" -}}
{{- template "_fact" list . 1 -}}
{{- end -}}


{{- range $i := until 11}}
{{- printf "%2d! = " $i}}{{template "fact" $i}}
{{- end}}

{{- /*
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
*/ -}}
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
Expand All @@ -14,6 +15,7 @@ import (
)

var (
fileName = flag.String("f", "", "use template file")
runsREPL = flag.Bool("i", false, "run interactive REPL instead")
)

Expand All @@ -25,6 +27,22 @@ func main() {
return
}

if *fileName != "" {
fp, err := os.Open(*fileName)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open %s: %v\n", *fileName, err)
return
}

b, err := ioutil.ReadAll(fp)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read %s: %v\n", *fileName, err)
}

runPipeMode(string(b))
return
}

if flag.Arg(0) == "" {
fmt.Fprintf(os.Stderr, "template must be passed to argument\n")
return
Expand Down

0 comments on commit cdf7723

Please sign in to comment.