forked from gernest/hot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hot.go
169 lines (150 loc) · 3.26 KB
/
hot.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package hot
import (
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
)
type Config struct {
Watch bool
BaseName string
Dir string
Funcs template.FuncMap
LeftDelim string
RightDelim string
FilesExtension []string
Log io.Writer
}
type Template struct {
tpl *template.Template
cfg *Config
watcher *fsnotify.Watcher
closeChannel chan bool
Out io.Writer
}
func New(cfg *Config) (*Template, error) {
leftDelim, rightDelim := getDelims(cfg)
tmpl := template.New(cfg.BaseName).Delims(leftDelim, rightDelim)
if cfg.Funcs != nil {
tmpl = template.New(cfg.BaseName).Funcs(cfg.Funcs).Delims(leftDelim, rightDelim)
}
tpl := &Template{
tpl: tmpl,
cfg: cfg,
Out: os.Stdout,
}
if cfg.Log != nil {
tpl.Out = cfg.Log
}
tpl.Init()
err := tpl.Load(cfg.Dir)
if err != nil {
return nil, err
}
return tpl, nil
}
func (t *Template) Init() {
if t.cfg.Watch {
watcher, err := fsnotify.NewWatcher()
t.watcher = watcher
if err != nil {
fmt.Fprintln(t.Out, err)
return
}
t.closeChannel = make(chan bool, 1)
fmt.Fprintln(t.Out, "start watching ", t.cfg.Dir)
err = watcher.Add(t.cfg.Dir)
if err != nil {
watcher.Close()
fmt.Fprintln(t.Out, err)
return
}
go func() {
for {
select {
case <-t.closeChannel:
return
case evt := <-watcher.Events:
fmt.Fprintf(t.Out, "%s: reloading... \n", evt.String())
t.Reload()
}
}
}()
}
}
func (t *Template) Close() {
t.closeChannel <- true
t.watcher.Close()
}
func (t *Template) Reload() {
tpl := *t.tpl
leftDelim, rightDelim := getDelims(t.cfg)
t.tpl = template.New(t.cfg.BaseName).Delims(leftDelim, rightDelim)
if t.cfg.Funcs != nil {
t.tpl = template.New(t.cfg.BaseName).Funcs(t.cfg.Funcs).Delims(leftDelim, rightDelim)
}
err := t.Load(t.cfg.Dir)
if err != nil {
fmt.Fprintln(t.Out, err.Error())
t.tpl = &tpl
}
}
func (t *Template) Load(dir string) error {
fmt.Fprintln(t.Out, "loading...", dir)
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
extension := filepath.Ext(path)
found := false
for _, ext := range t.cfg.FilesExtension {
if ext == extension {
found = true
break
}
}
if !found {
return nil
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
// We remove the directory name from the path
// this means if we have directory foo, with file bar.tpl
// full path for bar file foo/bar.tpl
// we trim the foo part and remain with /bar.tpl
name := path[len(dir):]
name = filepath.ToSlash(name)
name = strings.TrimPrefix(name, "/") // case we missed the opening slash
tpl := t.tpl.New(name)
_, err = tpl.Parse(string(data))
if err != nil {
return err
}
return nil
})
}
func (t *Template) Execute(w io.Writer, name string, ctx interface{}) error {
return t.tpl.ExecuteTemplate(w, name, ctx)
}
func getDelims(cfg *Config) (leftDelim string, rightDelim string) {
if cfg.LeftDelim != "" {
leftDelim = cfg.LeftDelim
} else {
leftDelim = "{{"
}
if cfg.RightDelim != "" {
rightDelim = cfg.RightDelim
} else {
rightDelim = "}}"
}
return
}