Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

πŸ€– Plandex β†’ Enhance delete-plan command with range/wildcard support, update messages, and improve test scripts for plan deletion functionality.

Dane Schneider525f105c8424
diff --git a/app/cli/cmd/delete_plan.go b/app/cli/cmd/delete_plan.go
index 06111bfe..398115ef 100644
--- a/app/cli/cmd/delete_plan.go
+++ b/app/cli/cmd/delete_plan.go
@@ -31,6 +31,41 @@ var rmCmd = &cobra.Command{
Run: del,
}
+
+func parseIndices(indexRange string, maxIndex int) map[int]bool {
+ indices := make(map[int]bool)
+ parts := strings.Split(indexRange, "-")
+
+ if len(parts) == 1 {
+ // Single index
+ idx, err := strconv.Atoi(parts[0])
+ if err == nil && idx > 0 && idx <= maxIndex {
+ indices[idx-1] = true
+ }
+ } else if len(parts) == 2 {
+ // Range of indices
+ start, err1 := strconv.Atoi(parts[0])
+ end, err2 := strconv.Atoi(parts[1])
+ if err1 == nil && err2 == nil && start > 0 && end <= maxIndex && start <= end {
+ for i := start - 1; i < end; i++ {
+ indices[i] = true
+ }
+ }
+ }
+
+ return indices
+}
+
+func matchPlansByPattern(pattern string, plans []*shared.Plan) []*shared.Plan {
+ var matched []*shared.Plan
+ for _, plan := range plans {
+ if matched, _ := path.Match(pattern, plan.Name); matched {
+ matched = append(matched, plan)
+ }
+ }
+ return matched
+}
+
func del(cmd *cobra.Command, args []string) {
auth.MustResolveAuthWithOrg()
lib.MustResolveProject()
@@ -40,16 +75,6 @@ func del(cmd *cobra.Command, args []string) {
return
}
- var nameOrIdx string
- if len(args) > 0 {
- nameOrIdx = strings.TrimSpace(args[0])
-
- if all {
- term.OutputErrorAndExit("Can't use both --all and a plan name or index")
- }
- }
- var plan *shared.Plan
-
term.StartSpinner("")
plans, apiErr := api.Client.ListPlans([]string{lib.CurrentProjectId})
term.StopSpinner()
@@ -65,66 +90,102 @@ func del(cmd *cobra.Command, args []string) {
return
}
- if nameOrIdx == "" {
+ var plansToDelete []*shared.Plan
+ if len(args) == 0 {
+ // Interactive selection
opts := make([]string, len(plans))
for i, plan := range plans {
opts[i] = plan.Name
}
selected, err := term.SelectFromList("Select a plan:", opts)
-
if err != nil {
term.OutputErrorAndExit("Error selecting plan: %v", err)
}
for _, p := range plans {
if p.Name == selected {
- plan = p
+ plansToDelete = append(plansToDelete, p)
break
}
}
} else {
+ nameOrPattern := strings.TrimSpace(args[0])
- // see if it's an index
- idx, err := strconv.Atoi(nameOrIdx)
-
- if err == nil {
- if idx > 0 && idx <= len(plans) {
- plan = plans[idx-1]
- } else {
- term.OutputErrorAndExit("Plan index out of range")
+ // Check if it's a range of indices
+ if strings.Contains(nameOrPattern, "-") {
+ indices := parseIndices(nameOrPattern, len(plans))
+ for idx := range indices {
+ plansToDelete = append(plansToDelete, plans[idx])
}
+ } else if strings.Contains(nameOrPattern, "*") {
+ // Wildcard pattern matching
+ plansToDelete = matchPlansByPattern(nameOrPattern, plans)
} else {
- for _, p := range plans {
- if p.Name == nameOrIdx {
- plan = p
- break
+ // Try as index first
+ idx, err := strconv.Atoi(nameOrPattern)
+ if err == nil {
+ if idx > 0 && idx <= len(plans) {
+ plansToDelete = append(plansToDelete, plans[idx-1])
+ } else {
+ term.OutputErrorAndExit("Plan index out of range")
+ }
+ } else {
+ // Try exact name match
+ for _, p := range plans {
+ if p.Name == nameOrPattern {
+ plansToDelete = append(plansToDelete, p)
+ break
+ }
}
}
}
}
- if plan == nil {
- term.OutputErrorAndExit("Plan not found")
+ if len(plansToDelete) == 0 {
+ term.OutputErrorAndExit("No matching plans found")
}
- term.StartSpinner("")
- apiErr = api.Client.DeletePlan(plan.Id)
- term.StopSpinner()
+ // Show confirmation with list of plans to be deleted
+ fmt.Printf("\nThe following %d plan(s) will be deleted:\n", len(plansToDelete))
+ for _, p := range plansToDelete {
+ fmt.Printf(" - %s\n", color.New(color.Bold, term.ColorHiCyan).Sprint(p.Name))
+ }
+ fmt.Println()
- if apiErr != nil {
- term.OutputErrorAndExit("Error deleting plan: %s", apiErr.Msg)
+ confirmed, err := term.ConfirmYesNo("Are you sure you want to delete these plans?")
+ if err != nil {
+ term.OutputErrorAndExit("Error getting confirmation: %v", err)
+ }
+ if !confirmed {
+ fmt.Println("Operation cancelled")
+ return
}
- if lib.CurrentPlanId == plan.Id {
- err := lib.ClearCurrentPlan()
- if err != nil {
- term.OutputErrorAndExit("Error clearing current plan: %v", err)
+ // Delete the plans
+ term.StartSpinner("")
+ for _, p := range plansToDelete {
+ apiErr = api.Client.DeletePlan(p.Id)
+ if apiErr != nil {
+ term.StopSpinner()
+ term.OutputErrorAndExit("Error deleting plan %s: %s", p.Name, apiErr.Msg)
+ }
+
+ if lib.CurrentPlanId == p.Id {
+ err := lib.ClearCurrentPlan()
+ if err != nil {
+ term.OutputErrorAndExit("Error clearing current plan: %v", err)
+ }
}
}
+ term.StopSpinner()
- fmt.Printf("βœ… Deleted plan %s\n", color.New(color.Bold, term.ColorHiCyan).Sprint(plan.Name))
+ if len(plansToDelete) == 1 {
+ fmt.Printf("βœ… Deleted plan '%s'\n", plansToDelete[0].Name)
+ } else {
+ fmt.Printf("βœ… Deleted %d plans\n", len(plansToDelete))
+ }
}
func delAll() {
diff --git a/test/plan_deletion_test.sh b/test/plan_deletion_test.sh
new file mode 100755
index 00000000..854a3fd2
--- /dev/null
+++ b/test/plan_deletion_test.sh
@@ -0,0 +1,78 @@
+
+#!/bin/bash
+set -e
+
+# Helper function to check if a plan exists
+check_plan_exists() {
+ local plan_name=$1
+ pdxd plans | grep -q "$plan_name"
+}
+
+# Helper function to count total plans
+count_plans() {
+ pdxd plans | grep -v "^[[:space:]]*$" | grep -v "^[[:space:]]*\\.*" | grep -v "^[[:space:]]*+" | grep -v "^[[:space:]]*\\.*" | grep -v "^[[:space:]]*$" | wc -l
+}
+
+# Create test plans
+echo "Creating test plans..."
+pdxd new -n "plan-config-1"
+pdxd new -n "plan-config-2"
+pdxd new -n "plan-config-3"
+pdxd new -n "other-plan-1"
+pdxd new -n "other-plan-2"
+
+# Verify initial plans were created
+echo "Verifying plans were created..."
+initial_count=$(count_plans)
+if [ "$initial_count" -ne 5 ]; then
+ echo "❌ Expected 5 plans, but found $initial_count"
+ exit 1
+fi
+
+for plan in "plan-config-1" "plan-config-2" "plan-config-3" "other-plan-1" "other-plan-2"; do
+ if ! check_plan_exists "$plan"; then
+ echo "❌ Plan '$plan' was not created successfully"
+ exit 1
+ fi
+done
+
+# Test wildcard deletion
+echo "Testing wildcard deletion..."
+pdxd dp "plan-config-*" <<< "y"
+
+# Verify wildcard deletion worked
+remaining_count=$(count_plans)
+if [ "$remaining_count" -ne 2 ]; then
+ echo "❌ Expected 2 plans after wildcard deletion, but found $remaining_count"
+ exit 1
+fi
+
+for plan in "plan-config-1" "plan-config-2" "plan-config-3"; do
+ if check_plan_exists "$plan"; then
+ echo "❌ Plan '$plan' should have been deleted"
+ exit 1
+ fi
+done
+
+# Create more plans for range deletion test
+pdxd new -n "range-test-1"
+pdxd new -n "range-test-2"
+pdxd new -n "range-test-3"
+
+# Test range deletion (should delete first 3 plans)
+echo "Testing range deletion..."
+pdxd dp "1-3" <<< "y"
+
+# Verify range deletion worked
+final_count=$(count_plans)
+if [ "$final_count" -ne 2 ]; then
+ echo "❌ Expected 2 plans after range deletion, but found $final_count"
+ exit 1
+fi
+
+# Clean up any remaining plans
+echo "Cleaning up remaining plans..."
+pdxd dp --all <<< "y"
+
+echo "βœ… All tests passed!"
+