Add find command

Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
Oliver Davies 2025-09-24 13:00:00 +01:00
parent 0a6e2b075c
commit 97445ca2cd
3 changed files with 69 additions and 0 deletions

35
cmd/find.go Normal file
View file

@ -0,0 +1,35 @@
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"code.oliverdavies.uk/opdavies/cmd-zet/internal/lib"
)
var findCmd = &cobra.Command{
Use: "find",
Aliases: []string{"f", "s", "search"},
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
zetDir := "/home/opdavies/Documents/zet"
if len(args) < 1 {
fmt.Println("No query")
os.Exit(1)
}
zets := lib.SearchZets(zetDir, args[0])
lib.ParseZetList(zets)
},
}

View file

@ -34,6 +34,7 @@ func Execute() {
} }
func init() { func init() {
rootCmd.AddCommand(findCmd)
rootCmd.AddCommand(titlesCmd) rootCmd.AddCommand(titlesCmd)
rootCmd.AddCommand(viewCmd) rootCmd.AddCommand(viewCmd)

View file

@ -43,6 +43,39 @@ func GetAllZets(dir string) []int {
return sorted return sorted
} }
func SearchZets(dir string, query string) []int {
zets, err := execGitCommand(dir, "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
}
func ParseZetList(ids []int) []string { func ParseZetList(ids []int) []string {
var lines []string var lines []string