forked from knative/func
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.go
110 lines (96 loc) · 2.9 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
package function
import (
"context"
"errors"
"strings"
)
var (
ErrRepositoryNotFound = errors.New("repository not found")
ErrRepositoriesNotDefined = errors.New("custom template repositories location not specified")
ErrTemplatesNotFound = errors.New("templates path (runtimes) not found")
ErrRuntimeNotFound = errors.New("language runtime not found")
ErrRuntimeRequired = errors.New("language runtime required")
ErrTemplateNotFound = errors.New("template not found")
ErrTemplateMissingRepository = errors.New("template name missing repository prefix")
)
// Templates Manager
type Templates struct {
client *Client
}
// newTemplates manager
// Includes a back-reference to client (logic tree root) such
// that the templates manager has full access to the API for
// use in its implementations.
func newTemplates(client *Client) *Templates {
return &Templates{client: client}
}
// List the full name of templates available for the runtime.
// Full name is the optional repository prefix plus the template's repository
// local name. Default templates grouped first sans prefix.
func (t *Templates) List(runtime string) ([]string, error) {
names := []string{}
extended := newSortedSet()
rr, err := t.client.Repositories().All()
if err != nil {
return []string{}, err
}
for _, r := range rr {
tt, err := r.Templates(runtime)
if err != nil {
return []string{}, err
}
for _, t := range tt {
if r.Name == DefaultRepositoryName {
names = append(names, t.Name())
} else {
extended.Add(t.Fullname())
}
}
}
return append(names, extended.Items()...), nil
}
// Template returns the named template in full form '[repo]/[name]' for the
// specified runtime.
// Templates from the default repository do not require the repo name prefix,
// though it can be provided.
func (t *Templates) Get(runtime, fullname string) (Template, error) {
var (
template Template
repoName string
tplName string
repo Repository
err error
)
// Split into repo and template names.
// Defaults when unprefixed to DefaultRepository
cc := strings.Split(fullname, "/")
if len(cc) == 1 {
repoName = DefaultRepositoryName
tplName = fullname
} else {
repoName = cc[0]
tplName = cc[1]
}
// Get specified repository
repo, err = t.client.Repositories().Get(repoName)
if err != nil {
return template, err
}
return repo.Template(runtime, tplName)
}
// Write a function's template to disk.
// Returns a function which may have been modified dependent on the content
// of the template (which can define default function fields, builders,
// buildpacks, etc)
func (t *Templates) Write(f *Function) error {
// Ensure the function itself is syntactically valid
if err := f.Validate(); err != nil {
return err
}
// The function's Template
template, err := t.Get(f.Runtime, f.Template)
if err != nil {
return err
}
return template.Write(context.TODO(), f)
}