diff --git a/README.md b/README.md index e22581b..e8f0cfa 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ go build -o tasks . ./tasks add "Buy groceries" # Add a task ./tasks list # List all tasks ./tasks list --limit 2 # List first N tasks +./tasks search "grocery" # Search tasks by keyword ./tasks complete 1 # Mark task #1 as done ./tasks delete 1 # Delete task #1 ``` diff --git a/cmd/search.go b/cmd/search.go new file mode 100644 index 0000000..b154670 --- /dev/null +++ b/cmd/search.go @@ -0,0 +1,49 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/akarol/coderabbit-demo/internal/store" + "github.com/spf13/cobra" +) + +var searchCmd = &cobra.Command{ + Use: "search [keyword]", + Short: "Search tasks by keyword", + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + keyword := strings.ToLower(args[0]) + + tasks, err := store.Load() + if err != nil { + return err + } + + var matches []store.Task + for _, t := range tasks { + if strings.Contains(strings.ToLower(t.Title), keyword) { + matches = append(matches, t) + } + } + + if len(matches) == 0 { + fmt.Printf("No tasks found matching '%s'\n", args[0]) + return nil + } + + fmt.Printf("Found %d task(s) matching '%s':\n", len(matches), args[0]) + for _, t := range matches { + status := "[ ]" + if t.Done { + status = "[x]" + } + fmt.Printf("%s #%d - %s\n", status, t.ID, t.Title) + } + return nil + }, +} + +func init() { + rootCmd.AddCommand(searchCmd) +}