chore: refactor search function
This commit is contained in:
parent
b6263eb607
commit
21b5a76fa7
|
@ -2,6 +2,8 @@ package search
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
|
@ -19,34 +21,28 @@ type searchOptions struct {
|
||||||
func Search(fs afero.Fs, scope, query string, checker rules.Checker, found func(path string, f os.FileInfo) error) error {
|
func Search(fs afero.Fs, scope, query string, checker rules.Checker, found func(path string, f os.FileInfo) error) error {
|
||||||
search := parseSearch(query)
|
search := parseSearch(query)
|
||||||
|
|
||||||
scope = strings.Replace(scope, "\\", "/", -1)
|
scope = filepath.ToSlash(filepath.Clean(scope))
|
||||||
scope = strings.TrimPrefix(scope, "/")
|
scope = path.Join("/", scope)
|
||||||
scope = strings.TrimSuffix(scope, "/")
|
|
||||||
scope = "/" + scope + "/"
|
|
||||||
|
|
||||||
return afero.Walk(fs, scope, func(originalPath string, f os.FileInfo, err error) error {
|
return afero.Walk(fs, scope, func(fPath string, f os.FileInfo, err error) error {
|
||||||
originalPath = strings.Replace(originalPath, "\\", "/", -1)
|
fPath = filepath.ToSlash(filepath.Clean(fPath))
|
||||||
originalPath = strings.TrimPrefix(originalPath, "/")
|
fPath = path.Join("/", fPath)
|
||||||
originalPath = "/" + originalPath
|
relativePath := strings.TrimPrefix(fPath, scope)
|
||||||
path := originalPath
|
relativePath = strings.TrimPrefix(relativePath, "/")
|
||||||
|
|
||||||
if path == scope {
|
if fPath == scope {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !checker.Check(path) {
|
if !checker.Check(fPath) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !search.CaseSensitive {
|
|
||||||
path = strings.ToLower(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(search.Conditions) > 0 {
|
if len(search.Conditions) > 0 {
|
||||||
match := false
|
match := false
|
||||||
|
|
||||||
for _, t := range search.Conditions {
|
for _, t := range search.Conditions {
|
||||||
if t(path) {
|
if t(fPath) {
|
||||||
match = true
|
match = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -59,18 +55,18 @@ func Search(fs afero.Fs, scope, query string, checker rules.Checker, found func(
|
||||||
|
|
||||||
if len(search.Terms) > 0 {
|
if len(search.Terms) > 0 {
|
||||||
for _, term := range search.Terms {
|
for _, term := range search.Terms {
|
||||||
if strings.Contains(path, term) {
|
_, fileName := path.Split(fPath)
|
||||||
originalPath = strings.TrimPrefix(originalPath, scope)
|
if !search.CaseSensitive {
|
||||||
originalPath = strings.TrimPrefix(originalPath, "/")
|
fileName = strings.ToLower(fileName)
|
||||||
return found(originalPath, f)
|
term = strings.ToLower(term)
|
||||||
|
}
|
||||||
|
if strings.Contains(fileName, term) {
|
||||||
|
return found(relativePath, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
return nil
|
||||||
originalPath = strings.TrimPrefix(originalPath, scope)
|
|
||||||
originalPath = strings.TrimPrefix(originalPath, "/")
|
|
||||||
return found(originalPath, f)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return found(relativePath, f)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue