From 97445ca2cdb30ada4fb2ba04f2f3aadde0c492c3 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 24 Sep 2025 13:00:00 +0100 Subject: [PATCH] Add `find` command Signed-off-by: Oliver Davies --- cmd/find.go | 35 +++++++++++++++++++++++++++++++++++ cmd/root.go | 1 + internal/lib/zet.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 cmd/find.go diff --git a/cmd/find.go b/cmd/find.go new file mode 100644 index 0000000..0564bc7 --- /dev/null +++ b/cmd/find.go @@ -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) + }, +} diff --git a/cmd/root.go b/cmd/root.go index 54a606e..cd7c326 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -34,6 +34,7 @@ func Execute() { } func init() { + rootCmd.AddCommand(findCmd) rootCmd.AddCommand(titlesCmd) rootCmd.AddCommand(viewCmd) diff --git a/internal/lib/zet.go b/internal/lib/zet.go index 2273ccb..2de0c21 100644 --- a/internal/lib/zet.go +++ b/internal/lib/zet.go @@ -43,6 +43,39 @@ func GetAllZets(dir string) []int { 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 { var lines []string