-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate_input.go
110 lines (94 loc) · 2.03 KB
/
template_input.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 bindata
import (
"bytes"
"fmt"
"path/filepath"
"strings"
)
type templateInput struct {
PackageName string
FileNames []string
Files []file
Tree map[string]*treeElement
}
type file struct {
GoName string
Content string
}
type treeElement struct {
GoName string
RemainingPath string
Children map[string]*treeElement
}
func (b *bindata) createTemplateInput(pais []*pathInfo, buf *bytes.Buffer) (*templateInput, error) {
tree, err := b.buildPathTree(pais)
if err != nil {
return nil, err
}
var fileNames []string
for _, pai := range pais {
fileNames = append(fileNames, pai.path)
}
input := &templateInput{
PackageName: b.getPackageName(),
FileNames: fileNames,
Files: []file{
{
GoName: "Archive",
Content: buf.String(),
},
},
Tree: tree,
}
return input, nil
}
func (b *bindata) getPackageName() string {
if b.opts.PackageName != "" {
return b.opts.PackageName
}
dir := filepath.Dir(b.opts.OutputFile)
parts := strings.Split(dir, pathSeparator)
return parts[len(parts)-1]
}
func (b *bindata) buildPathTree(pais []*pathInfo) (map[string]*treeElement, error) {
tree := make(map[string]*treeElement)
for _, pai := range pais {
parts := b.splitPath(pai.path)
err := b.traverseTree(parts, tree, pai.path, pai.path)
if err != nil {
return nil, err
}
}
return tree, nil
}
func (b *bindata) traverseTree(parts []string, m map[string]*treeElement, path string, remainingPath string) error {
item := parts[0]
if len(parts) == 1 {
_, ok := m[item]
if ok {
return fmt.Errorf("file [%s] already exists", item)
}
m[item] = &treeElement{
RemainingPath: remainingPath,
GoName: "Archive",
}
return nil
}
n, ok := m[item]
if ok {
err := b.traverseTree(parts[1:], n.Children, path, remainingPath)
if err != nil {
return err
}
} else {
n = &treeElement{
Children: map[string]*treeElement{},
}
m[item] = n
err := b.traverseTree(parts[1:], n.Children, path, remainingPath)
if err != nil {
return err
}
}
return nil
}