-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
64 lines (55 loc) · 1.44 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
package lint
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/template"
"github.com/Masterminds/sprig"
)
// Templates lints a chart's templates.
func Templates(basepath string) (messages []Message) {
messages = []Message{}
path := filepath.Join(basepath, "templates")
if fi, err := os.Stat(path); err != nil {
messages = append(messages, Message{Severity: WarningSev, Text: "No templates"})
return
} else if !fi.IsDir() {
messages = append(messages, Message{Severity: ErrorSev, Text: "'templates' is not a directory"})
return
}
tpl := template.New("tpl").Funcs(sprig.TxtFuncMap())
err := filepath.Walk(basepath, func(name string, fi os.FileInfo, e error) error {
// If an error is returned, we fail. Non-fatal errors should just be
// added directly to messages.
if e != nil {
return e
}
if fi.IsDir() {
return nil
}
data, err := ioutil.ReadFile(name)
if err != nil {
messages = append(messages, Message{
Severity: ErrorSev,
Text: fmt.Sprintf("cannot read %s: %s", name, err),
})
return nil
}
// An error rendering a file should emit a warning.
newtpl, err := tpl.Parse(string(data))
if err != nil {
messages = append(messages, Message{
Severity: ErrorSev,
Text: fmt.Sprintf("error processing %s: %s", name, err),
})
return nil
}
tpl = newtpl
return nil
})
if err != nil {
messages = append(messages, Message{Severity: ErrorSev, Text: err.Error()})
}
return
}