-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
tree.go
173 lines (156 loc) · 3.64 KB
/
tree.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
package genfs
import (
"fmt"
"path"
"sort"
"strings"
"github.com/xlab/treeprint"
)
// generate function
// type generate func(target string) (fs.File, error)
// func (fn generate) Generate(target string) (fs.File, error) {
// return fn(target)
// }
func newTree() *tree {
return &tree{
node: &node{
children: map[string]*node{},
Generator: nil,
Path: ".",
Name: ".",
Mode: modeDir,
parent: nil,
},
}
}
type tree struct {
node *node
}
type node struct {
Path string // path from root
Name string // basename
Mode mode // mode of the file
Generator generator
children map[string]*node
parent *node
}
func (n *node) Children() (children []*node) {
for _, child := range n.children {
children = append(children, child)
}
sort.Slice(children, func(i, j int) bool {
return children[i].Name < children[j].Name
})
return children
}
func (t *tree) Insert(fpath string, mode mode, generator generator) {
segments := strings.Split(fpath, "/")
last := len(segments) - 1
name := segments[last]
parent := t.mkdirAll(segments[:last])
// Add the base path with it's file generator to the tree.
child, found := parent.children[name]
if !found {
child = &node{
children: map[string]*node{},
Generator: generator,
Path: fpath,
Name: name,
Mode: mode,
parent: parent,
}
// Add child to parent
parent.children[name] = child
}
// Create or update the child's attributes
child.Mode = mode
child.Generator = generator
}
func (t *tree) mkdirAll(segments []string) *node {
parent := t.node
for _, segment := range segments {
child, ok := parent.children[segment]
if !ok {
child = &node{
children: map[string]*node{},
Generator: nil,
Path: path.Join(parent.Path, segment),
Name: segment,
Mode: modeDir,
parent: parent,
}
parent.children[segment] = child
}
parent = child
}
return parent
}
// Find an exact match the provided path
func (t *tree) Find(path string) (n *node, ok bool) {
// Special case to find the root node
if path == "." {
return t.node, true
}
// Traverse the children keyed by segments
node := t.node
segments := strings.Split(path, "/")
for _, name := range segments {
node, ok = node.children[name]
if !ok {
return nil, false
}
}
return node, true
}
// Get the closest match to the provided path
func (t *tree) FindPrefix(path string) (n *node, ok bool) {
// Special case to find the root node
if path == "." {
return t.node, true
}
// Traverse the children keyed by segments
node := t.node
segments := strings.Split(path, "/")
for _, name := range segments {
child, ok := node.children[name]
if !ok {
// nodes that aren't dirs must be an exact match
if !node.Mode.IsDir() {
return nil, false
}
return node, true
}
node = child
}
return node, true
}
func (t *tree) Delete(paths ...string) {
for _, path := range paths {
if node, ok := t.Find(path); ok {
// We're trying to delete the root, ignore for now
if node.parent == nil {
continue
}
// Remove node from parent, deleting all descendants
delete(node.parent.children, node.Name)
node.parent = nil
}
}
}
func formatNode(node *node) string {
if node.Generator == nil {
return fmt.Sprintf("%s mode=%s", node.Name, node.Mode)
}
return fmt.Sprintf("%s mode=%s generator=%v", node.Name, node.Mode, node.Generator)
}
func (t *tree) Print() string {
tp := treeprint.NewWithRoot(formatNode(t.node))
print(tp, t.node)
return tp.String()
}
func print(tp treeprint.Tree, node *node) {
for _, child := range node.Children() {
cp := tp.AddBranch(formatNode(child))
print(cp, child)
}
}