Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
49 changes: 49 additions & 0 deletions cmd/search.go
Original file line number Diff line number Diff line change
@@ -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])

Comment on lines +14 to +17

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Prevent silently ignoring extra search terms.

Line 14 accepts multiple args, but Line 16 only searches args[0], so ./tasks search foo bar drops bar without feedback.

💡 Proposed fix
 RunE: func(cmd *cobra.Command, args []string) error {
-		keyword := strings.ToLower(args[0])
+		if len(args) > 1 {
+			return fmt.Errorf("search expects a single keyword; wrap multi-word phrases in quotes")
+		}
+		keyword := strings.TrimSpace(args[0])
+		if keyword == "" {
+			return fmt.Errorf("keyword cannot be empty")
+		}
+		keyword = strings.ToLower(keyword)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
keyword := strings.ToLower(args[0])
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return fmt.Errorf("search expects a single keyword; wrap multi-word phrases in quotes")
}
keyword := strings.TrimSpace(args[0])
if keyword == "" {
return fmt.Errorf("keyword cannot be empty")
}
keyword = strings.ToLower(keyword)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmd/search.go` around lines 14 - 17, The command currently allows multiple
args (Args: cobra.MinimumNArgs(1)) but only uses args[0] in the RunE closure
(variable keyword), causing extra terms to be ignored; update RunE in
cmd/search.go to combine all args into a single search string (e.g., keyword :=
strings.ToLower(strings.Join(args, " "))) so multi-word searches like "./tasks
search foo bar" use both terms, or alternatively change the Args validator to
cobra.ExactArgs(1) if you want to enforce a single-term search—apply the change
to the RunE closure (where keyword is defined) and keep logging/validation
consistent.

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)
}
Loading