-
Notifications
You must be signed in to change notification settings - Fork 3
/
fsearch.go
90 lines (77 loc) · 2.66 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// fsearch.go // this file implements the search option
package main
import (
"fmt"
"path/filepath"
"strings"
)
// fSearch searches for binaries based on the given search term
func fSearch(config *Config, searchTerm string, metadata map[string]interface{}) error {
type tBinary struct {
Architecture string `json:"architecture"`
RealName string `json:"pkg"`
Description string `json:"description"`
}
// Fetch metadata
var binaries []tBinary
// Iterate over all sections and gather binaries
for _, section := range metadata {
binList, ok := section.([]interface{})
if !ok {
continue
}
for _, bin := range binList {
binMap, ok := bin.(map[string]interface{})
if !ok {
continue
}
realName, _ := binMap["pkg"].(string)
description, _ := binMap["description"].(string)
binaries = append(binaries, tBinary{
RealName: realName,
Description: description,
})
}
}
// Filter binaries based on the search term and architecture
searchResults := make([]string, 0)
for _, binary := range binaries {
if strings.Contains(strings.ToLower(binary.RealName), strings.ToLower(searchTerm)) ||
strings.Contains(strings.ToLower(binary.Description), strings.ToLower(searchTerm)) {
entry := fmt.Sprintf("%s - %s", binary.RealName, binary.Description)
searchResults = append(searchResults, entry)
}
}
// Check if no matching binaries found
limit := config.Limit
if len(searchResults) == 0 {
return fmt.Errorf("no matching binaries found for '%s'", searchTerm)
} else if len(searchResults) > limit {
return fmt.Errorf("too many matching binaries (+%d. [Use --limit before your query]) found for '%s'", limit, searchTerm)
}
// Maps to track installed and cached binaries
installedBinaries := make(map[string]bool)
// Check if the binary exists in the INSTALL_DIR and print results with installation state indicators
installDir := config.InstallDir
disableTruncation := config.DisableTruncation
for _, line := range searchResults {
parts := strings.SplitN(line, " - ", 2)
if len(parts) < 2 {
return fmt.Errorf("invalid search result format: %s", line)
}
RealName := parts[0]
description := parts[1]
baseRealName := filepath.Base(RealName)
// Determine the prefix based on conditions
prefix := "[-]"
_, cachedBinaryRealName, _ := ReturnCachedFile(config, RealName)
if installPath := filepath.Join(installDir, baseRealName); fileExists(installPath) && !installedBinaries[baseRealName] {
prefix = "[i]"
installedBinaries[baseRealName] = true
} else if cachedBinaryRealName == RealName {
prefix = "[c]"
}
truncatePrintf(disableTruncation, true, "%s %s - %s ", prefix, RealName, description)
}
return nil
}