package commands import ( "strings" "github.com/LazyBachelor/LazyPM/internal/models" "github.com/spf13/cobra" ) // Variables for get-issues command flags. var listFlags Flags const ( lsExamples = `pm list [id|title|description] pm list --status open --type bug pm list --title "New feature" --desc "feature description" pm list -p 1 -l 10` ) // getIssuesCmd represents the get issues command. var getIssuesCmd = &cobra.Command{ Use: "list [search query]", Short: "List all issues", Long: `List all issues in the project management system.`, Example: lsExamples, Aliases: []string{"ls", "search"}, Args: cobra.MinimumNArgs(0), RunE: runGetIssuesCmd, } // runGetIssuesCmd executes the get issues command logic, // which retrieves and displays a list of issues based on the provided search query and filters. func runGetIssuesCmd(cmd *cobra.Command, args []string) error { queryArg := strings.Join(args, " ") filter := models.IssueFilter{ TitleSearch: listFlags.title, DescriptionContains: listFlags.description, Limit: listFlags.limit, } // Only set filter fields if the corresponding flags // were explicitly provided by the user. if cmd.Flags().Changed("status") { s := models.Status(listFlags.status) filter.Status = &s } if cmd.Flags().Changed("type") { t := models.IssueType(listFlags.issueType) filter.IssueType = &t } if cmd.Flags().Changed("priority") { filter.Priority = &listFlags.priority } // Fetch issues based on the search query and filters. issuesPtr, err := svc.Beads.SearchIssues(cmd.Context(), queryArg, filter) if err != nil { return err } // Convert the returned issue pointers to issue values and print them. issues := models.IssuesPtrToIssues(issuesPtr) models.PrintIssues(issues) return nil } // init function to set up the get issues command and its flags. func init() { getIssuesCmd.Flags().StringVar(&listFlags.title, "title", "", "Filter issues by title") getIssuesCmd.Flags().StringVarP(&listFlags.description, "desc", "d", "", "Filter issues by description") getIssuesCmd.Flags().StringVarP(&listFlags.status, "status", "s", "", "Filter issues by status (open, closed, in_progress)") getIssuesCmd.Flags().StringVarP(&listFlags.issueType, "type", "t", "", "Filter issues by type (bug, feature, task)") getIssuesCmd.Flags().IntVarP(&listFlags.priority, "priority", "p", 0, "Filter issues by priority (0-4)") getIssuesCmd.Flags().IntVarP(&listFlags.limit, "limit", "l", 25, "Limit the number of issues returned") getIssuesCmd.RegisterFlagCompletionFunc("status", completionFunc(statusOptions)) getIssuesCmd.RegisterFlagCompletionFunc("type", completionFunc(typeOptions)) getIssuesCmd.RegisterFlagCompletionFunc("priority", completionFunc(priorityRange)) rootCmd.AddCommand(getIssuesCmd) }