add interactive mode
This commit is contained in:
@@ -1,15 +1,19 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/LazyBachelor/LazyPM/internal/models"
|
||||||
"github.com/charmbracelet/huh"
|
"github.com/charmbracelet/huh"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Variables for delete command flag.
|
// Variables for delete command flag.
|
||||||
var confirmDelete bool
|
var confirmDelete bool
|
||||||
|
var deleteIDs []string
|
||||||
|
var deleteInteractive bool
|
||||||
|
|
||||||
// deleteCmd represents the delete command.
|
// deleteCmd represents the delete command.
|
||||||
var deleteCmd = &cobra.Command{
|
var deleteCmd = &cobra.Command{
|
||||||
@@ -18,8 +22,9 @@ var deleteCmd = &cobra.Command{
|
|||||||
Long: `Delete an existing issue by its ID.`,
|
Long: `Delete an existing issue by its ID.`,
|
||||||
Example: `pm delete pm-abc`,
|
Example: `pm delete pm-abc`,
|
||||||
|
|
||||||
|
ValidArgsFunction: completeIssues,
|
||||||
|
|
||||||
Aliases: []string{"del", "remove", "rm"},
|
Aliases: []string{"del", "remove", "rm"},
|
||||||
Args: cobra.ExactArgs(1),
|
|
||||||
RunE: runDeleteCmd,
|
RunE: runDeleteCmd,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,6 +33,17 @@ var deleteCmd = &cobra.Command{
|
|||||||
func runDeleteCmd(cmd *cobra.Command, args []string) error {
|
func runDeleteCmd(cmd *cobra.Command, args []string) error {
|
||||||
deleteID := strings.Join(args, " ")
|
deleteID := strings.Join(args, " ")
|
||||||
|
|
||||||
|
if deleteInteractive {
|
||||||
|
if err := runDeleteInteractive(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if deleteID == "" {
|
||||||
|
return fmt.Errorf("issue ID cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch the issue to ensure it exists before deletion.
|
// Fetch the issue to ensure it exists before deletion.
|
||||||
issue, err := svc.Beads.GetIssue(cmd.Context(), deleteID)
|
issue, err := svc.Beads.GetIssue(cmd.Context(), deleteID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -62,8 +78,47 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func runDeleteInteractive() error {
|
||||||
|
options := []huh.Option[string]{}
|
||||||
|
|
||||||
|
issues, err := svc.Beads.SearchIssues(context.Background(), "", models.IssueFilter{})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error fetching issues: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, issue := range issues {
|
||||||
|
desc := fmt.Sprintf("%s: %s", issue.ID, issue.Title)
|
||||||
|
options = append(options, huh.NewOption(desc, issue.ID))
|
||||||
|
}
|
||||||
|
|
||||||
|
form := huh.NewForm(
|
||||||
|
huh.NewGroup(
|
||||||
|
huh.NewMultiSelect[string]().Value(&deleteIDs).
|
||||||
|
Options(options...).Value(&deleteIDs).
|
||||||
|
Title("Select issues to delete"))).WithTheme(huh.ThemeBase())
|
||||||
|
|
||||||
|
if err := form.Run(); err != nil {
|
||||||
|
return fmt.Errorf("error running interactive form: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(deleteIDs) == 0 {
|
||||||
|
return fmt.Errorf("no issues selected for deletion")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, id := range deleteIDs {
|
||||||
|
err := svc.Beads.DeleteIssue(context.Background(), id)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error deleting issue with ID %s: %w", id, err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Deleted issue with ID: %s\n", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// init function to set up the delete command and its flags.
|
// init function to set up the delete command and its flags.
|
||||||
func init() {
|
func init() {
|
||||||
|
deleteCmd.Flags().BoolVarP(&deleteInteractive, "interactive", "i", false, "Delete issues interactively")
|
||||||
deleteCmd.Flags().BoolVarP(&confirmDelete, "yes", "y", true, "Confirm deletion without prompt")
|
deleteCmd.Flags().BoolVarP(&confirmDelete, "yes", "y", true, "Confirm deletion without prompt")
|
||||||
|
|
||||||
rootCmd.AddCommand(deleteCmd)
|
rootCmd.AddCommand(deleteCmd)
|
||||||
|
|||||||
Reference in New Issue
Block a user