-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (120 loc) · 3.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
"golang.org/x/xerrors"
"github.com/peterh/liner"
)
var (
fileName = flag.String("f", "", "use template file")
runsREPL = flag.Bool("i", false, "run interactive REPL instead")
showsVersion = flag.Bool("v", false, "show version")
leftDelim = flag.String("ldelim", "{{", "specify left deliminater")
rightDelim = flag.String("rdelim", "}}", "specify right deliminater")
)
func main() {
err := run()
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
os.Exit(0)
}
func run() error {
flag.Parse()
if *showsVersion {
fmt.Println(versionStr())
return nil
}
tmpl := newTemplate(*leftDelim, *rightDelim)
if *runsREPL {
return runREPLMode(tmpl)
}
if *fileName != "" {
fp, err := os.Open(*fileName)
if err != nil {
return xerrors.Errorf("failed to open %s: %w", *fileName, err)
}
b, err := ioutil.ReadAll(fp)
if err != nil {
return xerrors.Errorf("failed to read %s: %w", *fileName, err)
}
return runPipeMode(tmpl, string(b))
}
if flag.Arg(0) == "" {
return xerrors.Errorf("template must be passed to argument")
}
return runPipeMode(tmpl, flag.Arg(0))
}
func runPipeMode(tmplGen *template.Template, tmplStr string) error {
tmpl, err := tmplGen.Parse(tmplStr)
if err != nil {
return xerrors.Errorf("template error: %w", err)
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
err = tmpl.Execute(os.Stdout, line)
if err != nil {
return xerrors.Errorf("runtime error: %w", err)
}
}
return nil
}
func runREPLMode(tmplGen *template.Template) error {
line := liner.NewLiner()
defer line.Close()
line.SetCtrlCAborts(true)
line.SetWordCompleter(newWordCompleter())
tmplStr := ""
lineNum := 1
for {
inputStr, err := line.Prompt(fmt.Sprintf("tmpl:%d> ", lineNum))
if err != nil {
if err == liner.ErrPromptAborted {
// end of repl
// HACK: prepend \n to break line even if repl is stopped by SIGINT
fmt.Println("\nBye.")
return nil
}
fmt.Fprintf(os.Stderr, "input error:\n%v\n", err)
continue
}
line.AppendHistory(inputStr)
newLine := inputStr + "\n"
// NOTE: whole history is necessary to refer previous variable statement
// HACK: insert "\034"(, which is not printable) to detect
// output generated by the latest input line
tmpl, err := tmplGen.Parse(tmplStr + "\034" + newLine)
if err != nil {
fmt.Fprintf(os.Stderr, "template error:\n%v\n", err)
continue
}
out := new(bytes.Buffer)
err = tmpl.Execute(out, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "runtime error:\n%v\n", err)
continue
}
// print only added output, which corresponds to the latest input
splitted := strings.Split(out.String(), "\034")
if len(splitted) != 2 {
fmt.Fprintf(os.Stderr,
"runtime error:\nthere must be 1 internal delimiter \\034 (found %d)\n", len(splitted)-1)
continue
}
diffStr := splitted[1]
fmt.Println(diffStr)
tmplStr = tmplStr + newLine
lineNum++
}
}
func newTemplate(leftDelim, rightDelim string) *template.Template {
return template.New("tmpl").Delims(leftDelim, rightDelim).Funcs(funcMap())
}