refactor for better reusability and maintainability
This commit is contained in:
@@ -23,7 +23,7 @@ func initializeServices(ctx context.Context) (*service.Services, func(), error)
|
||||
|
||||
func initTasks(svc *service.Services) []*task.Task {
|
||||
return []*task.Task{
|
||||
tasks.NewCreateIssueTask(svc),
|
||||
tasks.NewCreateIssueTask(svc).Init(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/cmd/survey/tasks"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||
)
|
||||
|
||||
@@ -15,6 +16,7 @@ func taskLoop(ctx context.Context, surveyTasks []*task.Task, interfaces []task.I
|
||||
for _, t := range surveyTasks {
|
||||
|
||||
t.SetInterface(interfaces[interfaceIndex])
|
||||
t.SetInterfaceType(tasks.InterfaceToType(interfaces[interfaceIndex]))
|
||||
|
||||
doneChan := make(chan bool, 1)
|
||||
quitChan := make(chan bool, 1)
|
||||
|
||||
@@ -17,18 +17,26 @@ This task will test your ability to use the issue creation workflow effectively.
|
||||
Assign this task to yourself and start creating the issue.
|
||||
Make sure to fill out all the necessary details, including the title, description, and assignee.`
|
||||
|
||||
func NewCreateIssueTask(svc *service.Services) *task.Task {
|
||||
aboutScreen := ui.NewTaskModel(createIssueDetails())
|
||||
questionnaire := ui.NewQuestionnaireModel(createIssueQuestionnaire())
|
||||
type CreateIssueTask struct {
|
||||
*task.Task
|
||||
svc *service.Services
|
||||
}
|
||||
|
||||
task := task.NewTask(svc, aboutScreen, questionnaire)
|
||||
task.SetConfigFunc(createIssueConfig)
|
||||
task.SetDbStateFunc(createIssueDbState)
|
||||
task.SetValidateFunc(createIssueValidate)
|
||||
func NewCreateIssueTask(svc *service.Services) *CreateIssueTask {
|
||||
return &CreateIssueTask{
|
||||
svc: svc,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *CreateIssueTask) Init() *task.Task {
|
||||
task := task.NewTask(t.svc, t.Details(), t.QuestionsFunc())
|
||||
task.SetConfigFunc(t.Config)
|
||||
task.SetDbStateFunc(t.DbStateFunc)
|
||||
task.SetValidateFunc(t.ValidateFunc)
|
||||
return task
|
||||
}
|
||||
|
||||
func createIssueConfig() task.TaskConfig {
|
||||
func (t *CreateIssueTask) Config() task.TaskConfig {
|
||||
return task.TaskConfig{
|
||||
IssuePrefix: "pm",
|
||||
BeadsDBPath: "./.pm/db.db",
|
||||
@@ -37,7 +45,7 @@ func createIssueConfig() task.TaskConfig {
|
||||
}
|
||||
}
|
||||
|
||||
func createIssueDetails() ui.TaskDetails {
|
||||
func (t *CreateIssueTask) Details() ui.TaskDetails {
|
||||
return ui.TaskDetails{
|
||||
Title: "Create Issue Task",
|
||||
Description: "Create a new issue in the project management system to test the issue creation workflow.",
|
||||
@@ -46,23 +54,38 @@ func createIssueDetails() ui.TaskDetails {
|
||||
}
|
||||
}
|
||||
|
||||
func createIssueQuestionnaire() ui.Questions {
|
||||
return ui.Questions{
|
||||
huh.NewGroup(
|
||||
huh.NewConfirm().Title("Was this good"),
|
||||
),
|
||||
huh.NewGroup(
|
||||
huh.NewSelect[int]().Options(
|
||||
huh.NewOption("Very good", 1),
|
||||
huh.NewOption("Very Bad", 2),
|
||||
).Title("How good was it?"),
|
||||
),
|
||||
func (t *CreateIssueTask) QuestionsFunc() task.QuestionsFunc {
|
||||
return func(interfaceType task.InterfaceType) ui.Questions {
|
||||
questions := ui.Questions{}
|
||||
|
||||
// Add an extra question for TUI
|
||||
if interfaceType == task.InterfaceTUI {
|
||||
questions = append(questions,
|
||||
huh.NewGroup(
|
||||
huh.NewConfirm().Title("Did you complete?"),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
questions = append(questions,
|
||||
huh.NewGroup(
|
||||
huh.NewConfirm().Title("Was this good"),
|
||||
),
|
||||
huh.NewGroup(
|
||||
huh.NewSelect[int]().Options(
|
||||
huh.NewOption("Very good", 1),
|
||||
huh.NewOption("Very Bad", 2),
|
||||
).Title("How good was it?"),
|
||||
),
|
||||
)
|
||||
|
||||
return questions
|
||||
}
|
||||
}
|
||||
|
||||
func createIssueDbState(ctx context.Context, svc *service.Services) error {
|
||||
func (t *CreateIssueTask) DbStateFunc(ctx context.Context) error {
|
||||
// Clear existing issues to ensure a clean state for the task
|
||||
if err := svc.DeleteIssues(); err != nil {
|
||||
if err := t.svc.DeleteIssues(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -72,16 +95,16 @@ func createIssueDbState(ctx context.Context, svc *service.Services) error {
|
||||
IssueType: models.TypeTask, Status: models.StatusOpen,
|
||||
}
|
||||
|
||||
if err := svc.Beads.CreateIssue(ctx, &issue, ""); err != nil {
|
||||
if err := t.svc.Beads.CreateIssue(ctx, &issue, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func createIssueValidate(ctx context.Context, svc *service.Services) (ok bool, errorMsg error) {
|
||||
func (t *CreateIssueTask) ValidateFunc(ctx context.Context) (ok bool, errorMsg error) {
|
||||
// Fetches issues, indexed with latest first
|
||||
issues, err := svc.Beads.SearchIssues(ctx, "", models.IssueFilter{})
|
||||
issues, err := t.svc.Beads.SearchIssues(ctx, "", models.IssueFilter{})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
27
cmd/survey/tasks/types.go
Normal file
27
cmd/survey/tasks/types.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"github.com/LazyBachelor/LazyPM/pkg/cli/repl"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/tui"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/web"
|
||||
)
|
||||
|
||||
const (
|
||||
InterfaceTUI task.InterfaceType = "tui"
|
||||
InterfaceCLI task.InterfaceType = "repl"
|
||||
InterfaceWeb task.InterfaceType = "web"
|
||||
)
|
||||
|
||||
func InterfaceToType(it task.Interface) task.InterfaceType {
|
||||
switch it.(type) {
|
||||
case *repl.REPL:
|
||||
return InterfaceCLI
|
||||
case *tui.Tui:
|
||||
return InterfaceTUI
|
||||
case *web.Web:
|
||||
return InterfaceWeb
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user