From 777d49dac8f135c448c69158729a73cdb670f280 Mon Sep 17 00:00:00 2001 From: Syuparn Date: Thu, 18 Feb 2021 08:56:26 +0900 Subject: [PATCH 1/2] add option -f --- README.md | 6 ++++++ example/fizzbuzz.tmpl | 5 +++++ main.go | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 example/fizzbuzz.tmpl diff --git a/README.md b/README.md index 411bd95..03b379d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/example/fizzbuzz.tmpl b/example/fizzbuzz.tmpl new file mode 100644 index 0000000..be3aadc --- /dev/null +++ b/example/fizzbuzz.tmpl @@ -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 -}} diff --git a/main.go b/main.go index 602cf1a..9cb2842 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "bytes" "flag" "fmt" + "io/ioutil" "os" "reflect" "strings" @@ -14,6 +15,7 @@ import ( ) var ( + fileName = flag.String("f", "", "use template file") runsREPL = flag.Bool("i", false, "run interactive REPL instead") ) @@ -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 From 259fd4f38c7405ef78d41aa759c508d942d26b2c Mon Sep 17 00:00:00 2001 From: Syuparn Date: Thu, 18 Feb 2021 09:16:50 +0900 Subject: [PATCH 2/2] add example --- example/template_as_func.tmpl | 9 +++++++++ example/template_factorial.tmpl | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 example/template_as_func.tmpl create mode 100644 example/template_factorial.tmpl diff --git a/example/template_as_func.tmpl b/example/template_as_func.tmpl new file mode 100644 index 0000000..0600ce0 --- /dev/null +++ b/example/template_as_func.tmpl @@ -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 -}} diff --git a/example/template_factorial.tmpl b/example/template_factorial.tmpl new file mode 100644 index 0000000..58eb541 --- /dev/null +++ b/example/template_factorial.tmpl @@ -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 +*/ -}}