-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender.go
78 lines (65 loc) · 1.52 KB
/
render.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
package bindata
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)
func (b *bindata) renderFile(input *templateInput, outputFile string, typ Type) error {
tmplPaths, ok := typeToTemplate[typ]
if !ok {
return fmt.Errorf("unknown bindata type: %d", typ)
}
tmpl, err := b.readTemplateFiles(tmplPaths)
if err != nil {
return err
}
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, input)
if err != nil {
return err
}
bites, err := format.Source(buf.Bytes())
if err != nil {
return err
}
err = b.mkdir(outputFile)
if err != nil {
return err
}
err = ioutil.WriteFile(outputFile, bites, 0600)
if err != nil {
return err
}
return nil
}
func (b *bindata) mkdir(outputFile string) error {
dir := filepath.Dir(outputFile)
stat, err := os.Stat(dir)
if os.IsNotExist(err) {
return os.MkdirAll(dir, 0700)
} else if !stat.IsDir() {
tokens := strings.Split(dir, string(filepath.Separator))
p := strings.Join(tokens[:len(tokens)-1], string(filepath.Separator))
return fmt.Errorf("can't create directory '%s' - found file with the same name '%s' in folder '%s'", filepath.Base(dir), filepath.Base(dir), p)
}
return nil
}
func (b *bindata) readTemplateFiles(tmplPaths []string) (*template.Template, error) {
tmpl := template.New("root.tmpl")
for _, templatePath := range tmplPaths {
data, err := ReadFile(templatePath)
if err != nil {
return nil, err
}
_, err = tmpl.Parse(string(data))
if err != nil {
return nil, err
}
}
return tmpl, nil
}