refactor to decouple commands form entrypoint for reusability and centralizing commands.
Increase reusability and composition
This commit is contained in:
117
internal/commands/issues/create.go
Normal file
117
internal/commands/issues/create.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package issuesCmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/internal/models"
|
||||
"github.com/charmbracelet/huh"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// createFlags holds the flag values for the create command
|
||||
var createFlags Flags
|
||||
|
||||
const (
|
||||
createCmdExample = `pm create New issue -d "Description" -s open -t task -p 3
|
||||
pm create Fix bug --desc "Bug description" --status in_progress --type bug --priority 5`
|
||||
)
|
||||
|
||||
// CreateCmd represents the create command, which allows users to create a new issue with specified details.
|
||||
var CreateCmd = &cobra.Command{
|
||||
Use: "create [title]",
|
||||
Short: "Create a new issue",
|
||||
Long: `Create a new issue with the specified details.`,
|
||||
Example: createCmdExample,
|
||||
|
||||
Args: cobra.MinimumNArgs(0),
|
||||
Aliases: []string{"add"},
|
||||
RunE: runCreateCmd,
|
||||
}
|
||||
|
||||
// runCreateCmd executes the create command logic,
|
||||
func runCreateCmd(cmd *cobra.Command, args []string) error {
|
||||
createFlags.title = strings.Join(args, " ")
|
||||
|
||||
// Run interactive if flag is set
|
||||
if createFlags.interactive {
|
||||
if err := runCreateInteractive(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if createFlags.title == "" {
|
||||
return fmt.Errorf("issue title cannot be empty")
|
||||
}
|
||||
|
||||
issue := &models.Issue{
|
||||
Title: createFlags.title,
|
||||
Description: createFlags.description,
|
||||
Status: models.Status(createFlags.status),
|
||||
IssueType: models.IssueType(createFlags.issueType),
|
||||
Priority: createFlags.priority,
|
||||
}
|
||||
|
||||
// Create the issue using the service layer.
|
||||
app := AppFromContext(cmd.Context())
|
||||
err := app.Issues.CreateIssue(cmd.Context(), issue, "test_actor")
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating issue: %w", err)
|
||||
}
|
||||
|
||||
// Display the created issue details to the user.
|
||||
cmd.Printf("Created issue:\n%s", models.IssueString(*issue))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// runCreateInteractive runs the interactive mode for creating issues,
|
||||
// allowing users to input issue details through a form.
|
||||
func runCreateInteractive() error {
|
||||
form := huh.NewForm(
|
||||
|
||||
huh.NewGroup(
|
||||
huh.NewInput().Value(&createFlags.title).Title("Title"),
|
||||
huh.NewText().Value(&createFlags.description).Title("Description")),
|
||||
|
||||
huh.NewGroup(
|
||||
huh.NewSelect[string]().Title("Status").
|
||||
Options(
|
||||
huh.NewOption("Open", "open"),
|
||||
huh.NewOption("Closed", "closed"),
|
||||
huh.NewOption("In Progress", "in_progress"),
|
||||
).Value(&createFlags.status),
|
||||
|
||||
huh.NewSelect[string]().Title("Type").
|
||||
Options(
|
||||
huh.NewOption("Bug", "bug"),
|
||||
huh.NewOption("Feature", "feature"),
|
||||
huh.NewOption("Task", "task"),
|
||||
).Value(&createFlags.issueType),
|
||||
|
||||
huh.NewSelect[int]().Title("Priority").
|
||||
Options(
|
||||
huh.NewOption("0", 0),
|
||||
huh.NewOption("1", 1),
|
||||
huh.NewOption("2", 2),
|
||||
huh.NewOption("3", 3),
|
||||
huh.NewOption("4", 4),
|
||||
).Value(&createFlags.priority),
|
||||
)).WithTheme(huh.ThemeBase16())
|
||||
|
||||
return form.Run()
|
||||
}
|
||||
|
||||
// init function to set up the create command and its flags.
|
||||
func init() {
|
||||
CreateCmd.Flags().BoolVarP(&createFlags.interactive, "interactive", "i", false, "Create issue interactively")
|
||||
CreateCmd.Flags().StringVarP(&createFlags.description, "desc", "d", "", "Issue description")
|
||||
CreateCmd.Flags().StringVarP(&createFlags.status, "status", "s", "open", "Issue status(open, closed, in_progress)")
|
||||
CreateCmd.Flags().StringVarP(&createFlags.issueType, "type", "t", "task", "Issue type(bug, feature, task)")
|
||||
CreateCmd.Flags().IntVarP(&createFlags.priority, "priority", "p", 0, "Issue priority(0-4)")
|
||||
|
||||
CreateCmd.RegisterFlagCompletionFunc("type", completionFunc(typeOptions))
|
||||
CreateCmd.RegisterFlagCompletionFunc("status", completionFunc(statusOptions))
|
||||
CreateCmd.RegisterFlagCompletionFunc("priority", completionFunc(priorityRange))
|
||||
}
|
||||
Reference in New Issue
Block a user