Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
a3202aaff3
commit
948872b933
7 changed files with 206 additions and 150 deletions
44
internal/zet/search.go
Normal file
44
internal/zet/search.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue