-
Notifications
You must be signed in to change notification settings - Fork 0
/
templ.go
359 lines (293 loc) · 6.63 KB
/
templ.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright © 2018 The Vault Authors.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package vault
const sharedTypesTempl = `
import (
"net/http"
)
// AssetLoader implements a function to load an asset from the vault
type AssetLoader interface {
// Open loads a file from the vault.
Open(name string) (http.File, error)
}
`
const releaseImportTempl = `
import (
"compress/zlib"
"errors"
"io"
"os"
"fmt"
"sort"
"strings"
"time"
"net/http"
)
`
const releaseFileTempl = `
type assetReader interface {
io.ReadCloser
zlib.Resetter
}
type memFile struct {
r assetReader
rOffset int64
offset int64
name string
modTime time.Time
path string
length int64
size int64
}
// Readdir see os.File Readdir function
func (m memFile) Readdir(count int) ([]os.FileInfo, error) {
return []os.FileInfo{}, io.EOF
}
func (m memFile) Close() error {
if m.r == nil {
return nil
}
return m.r.Close()
}
func (m *memFile) resetReader() (err error) {
if m.r == nil {
var r io.ReadCloser
r, err = zlib.NewReader(strings.NewReader(vaultAssetBin{{.Suffix}}[m.offset : m.offset+m.length]))
m.r = r.(assetReader)
} else {
err = m.r.Reset(strings.NewReader(vaultAssetBin{{.Suffix}}[m.offset:m.offset+m.length]), nil)
}
if err != nil {
return err
}
m.rOffset = 0
return nil
}
func (m *memFile) Read(p []byte) (n int, err error) {
if m.r == nil {
if err := m.resetReader(); err != nil {
return 0, err
}
}
n, err = m.r.Read(p)
m.rOffset += int64(n)
return n, err
}
func (m *memFile) Seek(offset int64, whence int) (int64, error) {
switch whence {
case io.SeekCurrent:
offset += m.rOffset
case io.SeekStart:
case io.SeekEnd:
offset += m.size
default:
return 0, errors.New("Seek: invalid whence")
}
if offset < 0 {
return m.rOffset, errors.New("Seek: invalid offset")
}
if offset < m.rOffset {
if err := m.resetReader(); err != nil {
return m.rOffset, err
}
}
buf := make([]byte, offset - m.rOffset)
_, err := m.Read(buf)
return m.rOffset, err
}
func (m memFile) Stat() (os.FileInfo, error) {
return m, nil
}
func (m memFile) Name() string {
return m.name
}
func (m memFile) Size() int64 {
return m.size
}
func (m memFile) Mode() os.FileMode {
return 0444
}
func (m memFile) ModTime() time.Time {
return m.modTime
}
func (m memFile) IsDir() bool {
return false
}
func (m memFile) Sys() interface{} {
return nil
}
type memDir struct {
dir string
files []os.FileInfo
size int64
}
func (m memDir) Close() error {
return nil
}
func (m memDir) Read(p []byte) (n int, err error) {
return 0, errors.New("Read: invalid operation on directory")
}
func (m memDir) Seek(offset int64, whence int) (int64, error) {
return 0, errors.New("Seek: invalid operation on directory")
}
func (m *memDir) Readdir(count int) ([]os.FileInfo, error) {
var ret []os.FileInfo
if count <= 0 || count >= len(m.files) {
ret = m.files[:]
m.files = m.files[0:0]
} else {
ret = m.files[:count]
m.files = m.files[count:]
}
if count > 0 && len(m.files) == 0 {
return ret, io.EOF
}
return ret, nil
}
func (m memDir) Stat() (os.FileInfo, error) {
return m, nil
}
func (m memDir) Name() string {
if m.dir == "/" {
return "/"
}
return m.dir[strings.LastIndex(m.dir, "/")+1:]
}
func (m memDir) Size() int64 {
return m.size
}
func (m memDir) Mode() os.FileMode {
return os.FileMode(0555)
}
func (m memDir) ModTime() time.Time {
// Until now no directory information is stored
// in the asset data, so for now we return the current
// time.
return time.Now()
}
func (m memDir) IsDir() bool {
return true
}
func (m memDir) Sys() interface{} {
return nil
}
type loader struct {
fm assetMap
}
func (l loader) Open(name string) (http.File, error) {
if !strings.HasPrefix(name, "/") {
if name == "." {
name = ""
}
name = "/" + name
}
if len(name) > 1 && name[len(name)-1] == '/' {
name = strings.TrimRight(name, "/")
}
if v, ok := l.fm[name]; ok {
return &v, nil
}
for _, v := range l.fm {
if strings.HasPrefix(v.path, name) {
return createDirFile(name, l.fm), nil
}
}
return nil, os.ErrNotExist
}
// New{{.Suffix}}Loader returns a new AssetLoader for the {{.Suffix}} resources.
func New{{.Suffix}}Loader() AssetLoader {
loader := &loader{
fm: assetMap{
{{- range $el := .Files }}
"{{join $el.Path $el.Name}}": memFile{offset: {{$el.Offset}},
name: "{{$el.Name}}",
modTime: time.Unix({{$el.ModTime.Unix}}, 0),
path: "{{$el.Path}}",
size: {{$el.Size}},
length: {{$el.Length}},
},
{{- end}}
},
}
return loader
}
// assetMap holds all information about the embedded files
type assetMap map[string]memFile
// createDirFile creates a http.File for the given path.
func createDirFile(path string, assets assetMap) http.File {
var fis []os.FileInfo
processed := map[string]struct{}{}
for _, val := range assets {
if val.path == path {
fis = append(fis, val)
continue
}
if strings.HasPrefix(val.path, path) {
dir := strings.TrimPrefix(val.path, path)
dir = strings.TrimPrefix(dir, "/")
if n := strings.Index(dir, "/"); n >= 0 {
dir = dir[:n]
}
if _, ok := processed[dir]; !ok {
var prefix string
if path == "/" {
prefix = "/" + dir
} else {
prefix = fmt.Sprintf("%v/%v", path, dir)
}
fis = append(fis, memDir{dir: dir, size: getSize(prefix, assets)})
processed[dir] = struct{}{}
}
}
}
sort.Slice(fis, func(i, j int) bool {
switch {
case fis[i].IsDir() && !fis[j].IsDir():
return true
case !fis[i].IsDir() && fis[j].IsDir():
return false
default:
return fis[i].Name() < fis[j].Name()
}
})
return &memDir{dir: path, size: getSize(path, assets), files: fis}
}
// getSize summarize all files under the given path.
func getSize(path string, assets assetMap) int64 {
var cnt int64
for _, item := range assets {
if strings.HasPrefix(item.path, path) {
cnt += item.size
}
}
return cnt
}
`
const debugFileTemp = `
import (
"fmt"
"os"
"path"
"net/http"
)
type debugLoader struct {
base string
}
func (d debugLoader) Open(name string) (http.File, error) {
return os.Open(getFullPath(d.base, name))
}
func getFullPath(b, p string) string {
return path.Clean(fmt.Sprintf("%v/%v", b, path.Clean("/"+p)))
}
// New{{.Suffix}}Loader returns a new AssetLoader for the {{.Suffix}} resources.
func New{{.Suffix}}Loader() AssetLoader {
return &debugLoader{base: "{{.Base}}"}
}
`
const fileHeaderTempl = `// This file is generated by the vault-cli command line utility.
// It offers a easy way to embed binary resources into a go executable.
// DO NOT EDIT this file, it will be overwritten on the next run of the vault-cli utility.
package {{.}}
`