-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsearch.go
85 lines (74 loc) 路 2.62 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
// fsearch.go // this file implements the search option
package main
import (
"fmt"
"os/exec"
"path/filepath"
"strings"
)
// fSearch searches for binaries based on the given search term.
func fSearch(searchTerm string, limit int) {
type tBinary struct {
Architecture string `json:"architecture"`
Name string `json:"name"`
Description string `json:"description"`
}
// Fetch metadata
var binaries []tBinary
err := fetchJSON(RNMetadataURL, &binaries)
if err != nil {
fmt.Println("Failed to fetch and decode binary information:", err)
return
}
// Filter binaries based on the search term and architecture
searchResultsSet := make(map[string]struct{})
for _, binary := range binaries {
if strings.Contains(strings.ToLower(binary.Name), strings.ToLower(searchTerm)) || strings.Contains(strings.ToLower(binary.Description), strings.ToLower(searchTerm)) {
ext := strings.ToLower(filepath.Ext(binary.Name))
base := filepath.Base(binary.Name)
if _, excluded := excludedFileTypes[ext]; excluded {
continue // Skip this binary if its extension is excluded
}
if _, excludedName := excludedFileNames[base]; excludedName {
continue // Skip this binary if its name is excluded
}
if binary.Description != "" {
entry := fmt.Sprintf("%s - %s", binary.Name, binary.Description)
searchResultsSet[entry] = struct{}{}
}
}
}
// Check if no matching binaries found
if len(searchResultsSet) == 0 {
fmt.Printf("No matching binaries found for '%s'.\n", searchTerm)
return
} else if len(searchResultsSet) > limit {
fmt.Printf("Too many matching binaries (+%d. [Use --limit before your query]) found for '%s'.\n", limit, searchTerm)
return
}
// Convert set to slice for sorting
var searchResults []string
for entry := range searchResultsSet {
searchResults = append(searchResults, entry)
}
// Sort the search results
searchResults = sortBinaries(searchResults)
// Check if the binary exists in the INSTALL_DIR and print results with installation state indicators
for _, line := range searchResults {
parts := strings.SplitN(line, " - ", 2)
name := parts[0]
description := parts[1]
installPath := filepath.Join(InstallDir, name)
cachedLocation, _ := ReturnCachedFile(name)
prefix := "[-]"
if fileExists(installPath) {
prefix = "[i]"
} else if path, err := exec.LookPath(name); err == nil && path != "" {
prefix = "[\033[4mi\033[0m]" // Print [i],'i' is underlined
} else if cachedLocation != "" && isExecutable(cachedLocation) {
prefix = "[c]"
}
truncatePrintf("%s %s - %s ", prefix, name, description)
fmt.Printf("\n") // Escape sequences are truncated too...
}
}