forked from knative/func
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.go
311 lines (265 loc) · 8.04 KB
/
templates.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
package function
// Updating Templates:
// See documentation in ./templates/README.md
// go get github.com/markbates/pkger
//go:generate pkger
import (
"errors"
"io"
"os"
"path/filepath"
"sort"
"strings"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/markbates/pkger"
)
type filesystem interface {
Stat(name string) (os.FileInfo, error)
Open(path string) (file, error)
ReadDir(path string) ([]os.FileInfo, error)
}
type file interface {
io.Reader
io.Closer
}
// Trigger encoding of ./templates as pkged.go
//
// When pkger is run, code analysis detects this pkger.Include statement,
// triggering the serialization of the templates directory and all its contents
// into pkged.go, which is then made available via a pkger filesystem. Path is
// relative to the go module root.
func init() {
_ = pkger.Include("/templates")
}
type templateWriter struct {
// Extensible Template Repositories
// templates on disk (extensible templates)
// Stored on disk at path:
// [customTemplatesPath]/[repository]/[runtime]/[template]
// For example
// ~/.config/func/boson/go/http"
// Specified when writing templates as simply:
// Write([runtime], [repository], [path])
// For example
// w := templateWriter{templates:"/home/username/.config/func/templates")
// w.Write("go", "boson/http")
// Ie. "Using the custom templates in the func configuration directory,
// write the Boson HTTP template for the Go runtime."
repositories string
// URL of a a specific network-available Git repository to use for
// templates. Takes precidence over both builtin and extensible
// if defined.
url string
// enable verbose logging
verbose bool
}
var (
ErrRepositoryNotFound = errors.New("repository not found")
ErrRepositoriesNotDefined = errors.New("custom template repositories location not specified")
ErrRuntimeNotFound = errors.New("runtime not found")
ErrTemplateNotFound = errors.New("template not found")
ErrTemplateMissingRepository = errors.New("template name missing repository prefix")
)
func (t templateWriter) Write(runtime, template, dest string) error {
if runtime == "" {
runtime = DefaultRuntime
}
if template == "" {
template = DefaultTemplate
}
// remote URLs, when provided, take precidence
if t.url != "" {
return writeRemote(t.url, runtime, template, dest)
}
// templates with repo prefix are on-disk "custom" (not embedded)
if len(strings.Split(template, "/")) > 1 {
return writeCustom(t.repositories, runtime, template, dest)
}
// default case is to write from the embedded set of core templates.
return writeEmbedded(runtime, template, dest)
}
func writeRemote(url, runtime, template, dest string) error {
// Clone a minimal copy of the remote repository in-memory.
r, err := git.Clone(
memory.NewStorage(),
memfs.New(),
&git.CloneOptions{
URL: url,
Depth: 1,
Tags: git.NoTags,
RecurseSubmodules: git.NoRecurseSubmodules,
})
if err != nil {
return err
}
wt, err := r.Worktree()
if err != nil {
return err
}
fs := wt.Filesystem
if _, err := fs.Stat(runtime); err != nil {
return ErrRuntimeNotFound
}
templatePath := filepath.Join(runtime, template)
if _, err := fs.Stat(templatePath); err != nil {
return ErrTemplateNotFound
}
accessor := billyFilesystem{fs: fs}
return copy(templatePath, dest, accessor)
}
// write from a custom repository. The temlate full name is prefixed
func writeCustom(repositoriesPath, runtime, templateFullName, dest string) error {
// assert path to template repos provided
if repositoriesPath == "" {
return ErrRepositoriesNotDefined
}
// assert template in form "repoName/templateName"
cc := strings.Split(templateFullName, "/")
if len(cc) != 2 {
return ErrTemplateMissingRepository
}
var (
repo = cc[0]
template = cc[1]
repoPath = filepath.Join(repositoriesPath, repo)
runtimePath = filepath.Join(repositoriesPath, repo, runtime)
templatePath = filepath.Join(repositoriesPath, repo, runtime, template)
accessor = osFilesystem{} // in instanced provider of Stat and Open
)
if _, err := accessor.Stat(repoPath); err != nil {
return ErrRepositoryNotFound
}
if _, err := accessor.Stat(runtimePath); err != nil {
return ErrRuntimeNotFound
}
if _, err := accessor.Stat(templatePath); err != nil {
return ErrTemplateNotFound
}
return copy(templatePath, dest, accessor)
}
func writeEmbedded(runtime, template, dest string) error {
var (
repoPath = "/templates"
runtimePath = filepath.Join(repoPath, runtime)
templatePath = filepath.Join(repoPath, runtime, template)
accessor = pkgerFilesystem{} // instanced provder of Stat and Open
)
if _, err := accessor.Stat(runtimePath); err != nil {
return ErrRuntimeNotFound
}
if _, err := accessor.Stat(templatePath); err != nil {
return ErrTemplateNotFound
}
return copy(templatePath, dest, accessor)
}
func copy(src, dest string, accessor filesystem) (err error) {
node, err := accessor.Stat(src)
if err != nil {
return
}
if node.IsDir() {
return copyNode(src, dest, accessor)
} else {
return copyLeaf(src, dest, accessor)
}
}
func copyNode(src, dest string, accessor filesystem) (err error) {
// Ideally we should use the file mode of the src node
// but it seems the git module is reporting directories
// as 0644 instead of 0755. For now, just do it this way.
err = os.MkdirAll(dest, 0755)
if err != nil {
return
}
children, err := readDir(src, accessor)
if err != nil {
return
}
for _, child := range children {
if err = copy(filepath.Join(src, child.Name()), filepath.Join(dest, child.Name()), accessor); err != nil {
return
}
}
return
}
func readDir(src string, accessor filesystem) ([]os.FileInfo, error) {
list, err := accessor.ReadDir(src)
if err != nil {
return nil, err
}
sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
return list, nil
}
func copyLeaf(src, dest string, accessor filesystem) (err error) {
srcFile, err := accessor.Open(src)
if err != nil {
return
}
defer srcFile.Close()
srcFileInfo, err := accessor.Stat(src)
if err != nil {
return
}
destFile, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, srcFileInfo.Mode())
if err != nil {
return
}
defer destFile.Close()
_, err = io.Copy(destFile, srcFile)
return
}
// Filesystems
// Wrap the implementations of FS with their subtle differences into the
// common interface for accessing template files defined herein.
// os: standard for on-disk extensible template repositories.
// pker: embedded filesystem backed by the generated pkged.go.
// billy: go-git library's filesystem used for remote git template repos.
// osFilesystem is a template file accessor backed by an os
// filesystem.
type osFilesystem struct{}
func (a osFilesystem) Stat(path string) (os.FileInfo, error) {
return os.Stat(path)
}
func (a osFilesystem) Open(path string) (file, error) {
return os.Open(path)
}
func (a osFilesystem) ReadDir(path string) ([]os.FileInfo, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return f.Readdir(-1)
}
// pkgerFilesystem is template file accessor backed by the pkger-provided
// embedded filesystem.
type pkgerFilesystem struct{}
func (a pkgerFilesystem) Stat(path string) (os.FileInfo, error) {
return pkger.Stat(path)
}
func (a pkgerFilesystem) Open(path string) (file, error) {
return pkger.Open(path)
}
func (a pkgerFilesystem) ReadDir(path string) ([]os.FileInfo, error) {
f, err := pkger.Open(path)
if err != nil {
return nil, err
}
return f.Readdir(-1) // Really? Pkger's ReadDir is Readdir.
}
// billyFilesystem is a template file accessor backed by a billy FS
type billyFilesystem struct {
fs billy.Filesystem
}
func (a billyFilesystem) Stat(path string) (os.FileInfo, error) {
return a.fs.Stat(path)
}
func (a billyFilesystem) Open(path string) (file, error) {
return a.fs.Open(path)
}
func (a billyFilesystem) ReadDir(path string) ([]os.FileInfo, error) {
return a.fs.ReadDir(path)
}