forked from xplshn/dbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsearch.go
70 lines (57 loc) · 1.85 KB
/
fsearch.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
package main
import (
"fmt"
"path/filepath"
"strings"
)
func fSearch(config *Config, searchTerms []string, uRepoIndex []binaryEntry) error {
var results []binaryEntry
for _, bin := range uRepoIndex {
name, pkgId, version, description, rank := bin.Name, bin.PkgId, bin.Version, bin.Description, bin.Rank
if name == "" || description == "" {
continue
}
match := true
for _, term := range searchTerms {
if !strings.Contains(strings.ToLower(name), strings.ToLower(term)) &&
!strings.Contains(strings.ToLower(description), strings.ToLower(term)) &&
!strings.Contains(strings.ToLower(pkgId), strings.ToLower(term)) {
match = false
break
}
}
if match {
results = append(results, binaryEntry{
Name: name,
PkgId: pkgId,
Version: version,
Description: description,
Rank: rank,
})
}
}
filteredResults := make([]binaryEntry, 0)
for _, result := range results {
ext := strings.ToLower(filepath.Ext(result.Name))
base := filepath.Base(result.Name)
if !contains(excludedFileTypes, ext) && !contains(excludedFileNames, base) {
filteredResults = append(filteredResults, result)
}
}
if len(filteredResults) == 0 {
return fmt.Errorf("no matching binaries found for '%s'", strings.Join(searchTerms, " "))
} else if uint(len(filteredResults)) > config.Limit {
return fmt.Errorf("too many matching binaries (+%d. [Use --limit before your query]) found for '%s'",
config.Limit, strings.Join(searchTerms, " "))
}
disableTruncation := config.DisableTruncation
for _, result := range filteredResults {
prefix := "[-]"
if bEntryOfinstalledBinary(filepath.Join(config.InstallDir, filepath.Base(result.Name))).PkgId == result.PkgId {
prefix = "[i]"
}
truncatePrintf(disableTruncation, "%s %s - %s\n",
prefix, parseBinaryEntry(result, true), result.Description)
}
return nil
}