-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
133 lines (105 loc) · 2.57 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
// pago - a command-line password manager.
//
// License: MIT.
// See the file LICENSE.
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/xlab/treeprint"
)
func dirTree(root string, transform func(name string, info os.FileInfo) (bool, string)) (string, error) {
tree := treeprint.NewWithRoot(filepath.Base(root))
visited := make(map[string]treeprint.Tree)
err := filepath.Walk(root, func(name string, info os.FileInfo, err error) error {
if err != nil {
return err
}
name, err = filepath.Abs(name)
if err != nil {
return err
}
keep, displayName := transform(name, info)
if !keep {
return nil
}
if len(visited) == 0 {
visited[name] = tree
return nil
}
parent, ok := visited[filepath.Dir(name)]
if !ok {
return nil
}
var newTree treeprint.Tree
if info.IsDir() {
newTree = parent.AddBranch(displayName)
} else {
newTree = parent.AddNode(displayName)
}
visited[name] = newTree
return nil
})
if err != nil {
return "", err
}
return tree.String(), nil
}
func printStoreTree(store string) error {
tree, err := dirTree(store, func(name string, info os.FileInfo) (bool, string) {
if strings.HasPrefix(info.Name(), ".") {
return false, ""
}
displayName := strings.TrimSuffix(info.Name(), ageExt)
if info.IsDir() {
displayName += "/"
}
return true, displayName
})
if err != nil {
return fmt.Errorf("failed to build tree: %v", err)
}
fmt.Print(tree)
return nil
}
func listFiles(root string, transform func(name string, info os.FileInfo) (bool, string)) ([]string, error) {
list := []string{}
err := filepath.Walk(root, func(name string, info os.FileInfo, err error) error {
if err != nil {
return err
}
name, err = filepath.Abs(name)
if err != nil {
return err
}
keep, displayName := transform(name, info)
if !keep {
return nil
}
list = append(list, displayName)
return nil
})
if err != nil {
return []string{}, err
}
return list, nil
}
// Return a function that filters entries by a filename pattern.
func entryFilter(root string, pattern *regexp.Regexp) func(name string, info os.FileInfo) (bool, string) {
return func(name string, info os.FileInfo) (bool, string) {
if info.IsDir() || !strings.HasSuffix(name, ageExt) {
return false, ""
}
displayName := name
displayName = strings.TrimPrefix(displayName, root)
displayName = strings.TrimPrefix(displayName, "/")
displayName = strings.TrimSuffix(displayName, ageExt)
if pattern != nil && !pattern.MatchString(displayName) {
return false, ""
}
return true, displayName
}
}