forked from couchbaselabs/cbfs
/
find.go
194 lines (168 loc) · 4.06 KB
/
find.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/couchbaselabs/cbfs/client"
"github.com/couchbaselabs/cbfs/tools"
"github.com/dustin/httputil"
)
type findType int
const (
findTypeAny = findType(iota)
findTypeFile
findTypeDir
)
var findFlags = flag.NewFlagSet("find", flag.ExitOnError)
var findTemplate = findFlags.String("t", "", "Display template")
var findTemplateFile = findFlags.String("T", "", "Display template filename")
var findDashName = findFlags.String("name", "", "Glob name to match")
var findDashIName = findFlags.String("iname", "",
"Case insensitive glob name to match")
var findDashMTime = findFlags.Duration("mtime", 0, "Find by mod time")
var findDashDepth = findFlags.Int("depth", 4096, "Maximum search depth")
var findDashType findType
const defaultFindTemplate = `{{.Name}}
`
func (t findType) String() string {
switch t {
case findTypeAny:
return ""
case findTypeFile:
return "f"
case findTypeDir:
return "d"
}
panic("unreachable")
}
func (t *findType) Set(s string) error {
switch s {
case "":
*t = findTypeAny
case "f":
*t = findTypeFile
case "d":
*t = findTypeDir
default:
return fmt.Errorf("must be 'f' or 'd'")
}
return nil
}
func init() {
findFlags.Var(&findDashType, "type", "Type to match (f or d)")
}
type dirAndFileMatcher struct {
m map[string]struct{}
p string
i bool // if true, case insensitive
}
func newDirAndFileMatcher() dirAndFileMatcher {
rv := dirAndFileMatcher{map[string]struct{}{}, *findDashName, false}
if *findDashIName != "" {
rv.i = true
rv.p = strings.ToLower(*findDashIName)
}
return rv
}
type findMatch struct {
path string
isDir bool
}
func (d dirAndFileMatcher) match(name string, isdir bool) bool {
switch findDashType {
case findTypeAny:
case findTypeFile:
if isdir {
return false
}
case findTypeDir:
if !isdir {
return false
}
}
m := name
if d.i {
m = strings.ToLower(m)
}
matched, err := filepath.Match(d.p, m)
if err != nil {
log.Fatalf("Error globbing: %v", err)
}
return matched
}
func (d dirAndFileMatcher) matches(name string) []findMatch {
if d.p == "" {
return []findMatch{{name, false}}
}
var matches []findMatch
dir := filepath.Dir(name)
for dir != "." {
if _, seen := d.m[dir]; !seen {
d.m[dir] = struct{}{}
if d.match(filepath.Base(dir), true) {
matches = append(matches, findMatch{dir, true})
}
}
dir = filepath.Dir(dir)
}
// Reverse these so the traversal order makes sense
for i := 0; i < len(matches)/2; i++ {
j := len(matches) - i - 1
matches[i], matches[j] = matches[j], matches[i]
}
if d.match(filepath.Base(name), false) {
matches = append(matches, findMatch{name, false})
}
return matches
}
func findMetaMatch(ref func(time.Time) bool, c cbfsclient.FileMeta) bool {
return true
}
func findGetRefTimeMatch(now time.Time) func(time.Time) bool {
switch {
case *findDashMTime > 0:
return now.Add(-*findDashMTime).Before
case *findDashMTime < 0:
return now.Add(*findDashMTime).After
}
return func(time.Time) bool { return true }
}
func findCommand(u string, args []string) {
if *findDashName != "" && *findDashIName != "" {
log.Fatalf("Can't specify both -name and -iname")
}
src := findFlags.Arg(0)
for src[len(src)-1] == '/' {
src = src[:len(src)-1]
}
tmpl := cbfstool.GetTemplate(*findTemplate, *findTemplateFile,
defaultFindTemplate)
httputil.InitHTTPTracker(false)
client, err := cbfsclient.New(u)
cbfstool.MaybeFatal(err, "Can't build a client: %v", err)
things, err := client.ListDepth(src, *findDashDepth)
cbfstool.MaybeFatal(err, "Can't list things: %v", err)
metaMatcher := findGetRefTimeMatch(time.Now())
matcher := newDirAndFileMatcher()
for fn, inf := range things.Files {
if !metaMatcher(inf.Modified) {
continue
}
if len(fn) > len(src)+1 {
fn = fn[len(src)+1:]
}
for _, match := range matcher.matches(fn) {
if err := tmpl.Execute(os.Stdout, struct {
Name string
IsDir bool
Meta cbfsclient.FileMeta
}{match.path, match.isDir, inf}); err != nil {
log.Fatalf("Error executing template: %v", err)
}
}
}
}