summaryrefslogtreecommitdiff
path: root/match/file.go
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2017-05-07 19:53:55 +0300
committerEyal Posener <[email protected]>2017-05-07 19:57:41 +0300
commit6fb4875efa1f536813da29972a0c3250bde8b5eb (patch)
treec7ee97d75ed8d1d6c734256e3ff789402571bcca /match/file.go
parente8f6dfad584cb1e3082bc7ea82f8c0551dd944b3 (diff)
Move match to a separate package
Diffstat (limited to 'match/file.go')
-rw-r--r--match/file.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/match/file.go b/match/file.go
new file mode 100644
index 0000000..c972ce0
--- /dev/null
+++ b/match/file.go
@@ -0,0 +1,30 @@
+package match
+
+import (
+ "path/filepath"
+ "strings"
+)
+
+// File is a file name Matcher, if the last word can prefix the
+// File path, there is a possible match
+type File string
+
+func (a File) String() string {
+ return string(a)
+}
+
+// Match returns true if prefix's abs path prefixes a's abs path
+func (a File) Match(prefix string) bool {
+ full, err := filepath.Abs(string(a))
+ if err != nil {
+ return false
+ }
+ prefixFull, err := filepath.Abs(prefix)
+ if err != nil {
+ return false
+ }
+
+ // if the file has the prefix as prefix,
+ // but we don't want to show too many files, so, if it is in a deeper directory - omit it.
+ return strings.HasPrefix(full, prefixFull) && (full == prefixFull || !strings.Contains(full[len(prefixFull)+1:], "/"))
+}