-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
172 lines (147 loc) · 3.35 KB
/
template.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
170
171
172
package component
import (
"html/template"
"io"
"math/rand"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/ekara-platform/model"
gl "github.com/gobwas/glob"
"github.com/oklog/ulid"
)
// runTemplate runs the templates defined into a given component
func runTemplate(ctx *model.TemplateContext, componentPath string, patterns model.Patterns, resolver model.ComponentReferencer) (string, error) {
if len(patterns.Content) > 0 {
globs := make([]gl.Glob, 0, 0)
files := make([]string, 0, 0)
for _, p := range patterns.Content {
pa := filepath.Join(componentPath, p)
// workaround of issue : https://github.com/gobwas/glob/issues/35
if runtime.GOOS == "windows" {
pa = strings.Replace(pa, "\\", "/", -1)
}
globs = append(globs, gl.MustCompile(pa, '/'))
}
err := filepath.Walk(componentPath, func(path string, info os.FileInfo, err error) error {
for _, p := range patterns.Content {
pa := filepath.Join(componentPath, p)
if path == pa {
files = append(files, path)
return nil
}
}
for _, g := range globs {
pa := path
// workaround of issue : https://github.com/gobwas/glob/issues/35
if runtime.GOOS == "windows" {
pa = strings.Replace(pa, "\\", "/", -1)
}
if g.Match(pa) {
files = append(files, path)
return nil
}
}
return nil
})
if err != nil {
return "", err
}
var uuid string
var tmpPath string
// No matching files encountered then it won't be templated
if len(files) == 0 {
return "", nil
}
uuid = genUlid()
tmpPath = componentPath + "_" + uuid
err = copyFolder(componentPath, tmpPath)
if err != nil {
return "", err
}
for _, f := range files {
f = strings.Replace(f, componentPath, tmpPath, -1)
t, err := template.ParseFiles(f)
if err != nil {
os.RemoveAll(tmpPath)
return "", err
}
//t = template.Must(t.Option("missingkey=error"), err)
file, err := os.Create(f)
defer file.Close()
if err != nil {
os.RemoveAll(tmpPath)
return "", err
}
err = t.Execute(file, *ctx)
if err != nil {
os.RemoveAll(tmpPath)
return "", err
}
}
return tmpPath, nil
}
return "", nil
}
func copyFolder(source string, dest string) (err error) {
info, err := os.Stat(source)
if err != nil {
return
}
err = os.MkdirAll(dest, info.Mode())
if err != nil {
return
}
directory, err := os.Open(source)
if err != nil {
return
}
files, err := directory.Readdir(-1)
if err != nil {
return
}
for _, obj := range files {
sourcefilepointer := source + "/" + obj.Name()
destinationfilepointer := dest + "/" + obj.Name()
if obj.IsDir() {
err = copyFolder(sourcefilepointer, destinationfilepointer)
if err != nil {
return
}
} else {
err = copyFile(sourcefilepointer, destinationfilepointer)
if err != nil {
return
}
}
}
return
}
func copyFile(source string, dest string) (err error) {
s, err := os.Open(source)
if err != nil {
return
}
defer s.Close()
d, err := os.Create(dest)
if err != nil {
return
}
defer d.Close()
_, err = io.Copy(d, s)
if err == nil {
sourceinfo, err := os.Stat(source)
if err != nil {
err = os.Chmod(dest, sourceinfo.Mode())
}
}
return
}
func genUlid() string {
t := time.Now().UTC()
entropy := rand.New(rand.NewSource(t.UnixNano()))
id := ulid.MustNew(ulid.Timestamp(t), entropy)
return id.String()
}