cmd-zet/internal/zet/search.go

45 lines
689 B
Go
Raw Normal View History

package zet
import (
"fmt"
"os"
"regexp"
"sort"
"strconv"
"code.oliverdavies.uk/opdavies/cmd-zet/internal/git"
)
func SearchZets(query string) []int {
zets, err := git.ExecGitCommand("grep", "-i", "--name-only", "--word-regex", query)
if err != nil {
fmt.Printf("No matches found for %s.\n", query)
os.Exit(1)
}
re := regexp.MustCompile(`[0-9]+`)
matches := re.FindAllString(zets, -1)
sort.Strings(matches)
ids := make(map[int]struct{})
for _, id := range matches {
num, err := strconv.Atoi(id)
if err == nil {
ids[num] = struct{}{}
}
}
var sorted []int
for num := range ids {
sorted = append(sorted, num)
}
sort.Ints(sorted)
return sorted
}