add builders and chaining methods for various types and variables for better reusability and readability
This commit is contained in:
@@ -37,13 +37,7 @@ func initTasks(svc *service.Services) []task.Tasker {
|
||||
}
|
||||
|
||||
func initializeServices(ctx context.Context) (*service.Services, func(), error) {
|
||||
config := service.Config{
|
||||
IssuePrefix: "pm",
|
||||
BeadsDBPath: "./.pm/db.db",
|
||||
StatisticsStoragePath: "./.pm/stats.json",
|
||||
WebAddress: ":8080",
|
||||
}
|
||||
return service.NewServices(ctx, config)
|
||||
return service.NewServices(ctx, tasks.BaseConfig())
|
||||
}
|
||||
|
||||
func initInterfaces() map[string]task.Interface {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/cli/repl"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||
taskui "github.com/LazyBachelor/LazyPM/pkg/task/ui"
|
||||
@@ -40,12 +41,16 @@ func BaseDetails() taskui.TaskDetails {
|
||||
func BaseConfig() task.TaskConfig {
|
||||
return task.TaskConfig{
|
||||
IssuePrefix: "pm",
|
||||
WebAddress: ":8080",
|
||||
BeadsDBPath: "./.pm/db.db",
|
||||
StatisticsStoragePath: "./.pm/stats.json",
|
||||
WebAddress: ":8080",
|
||||
}
|
||||
}
|
||||
|
||||
func ClearIssues(svc *service.Services) error {
|
||||
return svc.Beads.DeleteIssues()
|
||||
}
|
||||
|
||||
func BaseQuestions(interfaceType task.InterfaceType) taskui.Questions {
|
||||
var taskRating int
|
||||
return taskui.Questions{
|
||||
@@ -66,36 +71,23 @@ func BaseQuestions(interfaceType task.InterfaceType) taskui.Questions {
|
||||
}
|
||||
}
|
||||
|
||||
func AppendGroup(questions *taskui.Questions, group *huh.Group) taskui.Questions {
|
||||
*questions = append(*questions, group)
|
||||
return *questions
|
||||
}
|
||||
|
||||
func AppendQuestion(questions *taskui.Questions, interfaceType task.InterfaceType, field ...huh.Field) taskui.Questions {
|
||||
*questions = append(*questions, huh.NewGroup(field...))
|
||||
return *questions
|
||||
}
|
||||
|
||||
func AppendReplQuestion(questions *taskui.Questions, interfaceType task.InterfaceType, field ...huh.Field) taskui.Questions {
|
||||
func ReplQuestion(interfaceType task.InterfaceType, fields ...huh.Field) *huh.Group {
|
||||
if interfaceType != InterfaceREPL {
|
||||
return *questions
|
||||
return nil
|
||||
}
|
||||
*questions = append(*questions, huh.NewGroup(field...))
|
||||
return *questions
|
||||
return huh.NewGroup(fields...)
|
||||
}
|
||||
|
||||
func AppendWebQuestion(questions *taskui.Questions, interfaceType task.InterfaceType, field ...huh.Field) taskui.Questions {
|
||||
func WebQuestion(questions *taskui.Questions, interfaceType task.InterfaceType, fields ...huh.Field) *huh.Group {
|
||||
if interfaceType != InterfaceWeb {
|
||||
return *questions
|
||||
return nil
|
||||
}
|
||||
*questions = append(*questions, huh.NewGroup(field...))
|
||||
return *questions
|
||||
return huh.NewGroup(fields...)
|
||||
}
|
||||
|
||||
func AppendTUIQuestion(questions *taskui.Questions, interfaceType task.InterfaceType, field ...huh.Field) taskui.Questions {
|
||||
func TUIQuestion(questions *taskui.Questions, interfaceType task.InterfaceType, fields ...huh.Field) *huh.Group {
|
||||
if interfaceType != InterfaceTUI {
|
||||
return *questions
|
||||
return nil
|
||||
}
|
||||
*questions = append(*questions, huh.NewGroup(field...))
|
||||
return *questions
|
||||
return huh.NewGroup(fields...)
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||
taskui "github.com/LazyBachelor/LazyPM/pkg/task/ui"
|
||||
"github.com/charmbracelet/huh"
|
||||
)
|
||||
|
||||
const codingDescription = `You are tasked with writing a simple function.
|
||||
@@ -30,31 +31,28 @@ func NewCodingTask(svc *service.Services) *CodingTask {
|
||||
}
|
||||
|
||||
func (t *CodingTask) Config() task.TaskConfig {
|
||||
config := BaseConfig()
|
||||
config.StatisticsStoragePath = "./.pm/coding-task-stats.json"
|
||||
return config
|
||||
return BaseConfig().WithStatisticsStoragePath("./.pm/coding-task-stats.json")
|
||||
}
|
||||
|
||||
func (t *CodingTask) Details() taskui.TaskDetails {
|
||||
details := BaseDetails()
|
||||
details.Title = "Coding Task"
|
||||
details.Description = codingDescription
|
||||
return details
|
||||
return BaseDetails().WithTitle("Coding Task").WithDescription(codingDescription)
|
||||
}
|
||||
|
||||
func (t *CodingTask) Questions(interfaceType task.InterfaceType) (questions taskui.Questions) {
|
||||
questions = append(questions, BaseQuestions(interfaceType)...)
|
||||
|
||||
return questions
|
||||
return BaseQuestions(interfaceType).
|
||||
With(ReplQuestion(interfaceType,
|
||||
huh.NewInput().Title("Please write your code in the 'code.txt' file and save it.").
|
||||
Placeholder("Write your code here...")),
|
||||
)
|
||||
}
|
||||
|
||||
func (t *CodingTask) Setup(ctx context.Context) error {
|
||||
if err := t.svc.DeleteIssues(); err != nil {
|
||||
if err := ClearIssues(t.svc); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
content := textFileContent
|
||||
|
||||
|
||||
if err := os.WriteFile("./code.txt", []byte(content), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -25,36 +25,25 @@ func NewCreateIssueTask(svc *service.Services) *CreateIssueTask {
|
||||
}
|
||||
|
||||
func (t *CreateIssueTask) Config() task.TaskConfig {
|
||||
config := BaseConfig()
|
||||
config.StatisticsStoragePath = "./.pm/create-issue-stats.json"
|
||||
return config
|
||||
return BaseConfig().WithStatisticsStoragePath("./.pm/create-issue-stats.json")
|
||||
}
|
||||
|
||||
func (t *CreateIssueTask) Details() taskui.TaskDetails {
|
||||
details := BaseDetails()
|
||||
details.Title = "Create Issue Task"
|
||||
details.Description = description
|
||||
return details
|
||||
return BaseDetails().WithTitle("Create Issue Task").WithDescription(description)
|
||||
}
|
||||
|
||||
func (t *CreateIssueTask) Questions(interfaceType task.InterfaceType) taskui.Questions {
|
||||
questions := BaseQuestions(interfaceType)
|
||||
return questions
|
||||
return BaseQuestions(interfaceType)
|
||||
}
|
||||
|
||||
func (t *CreateIssueTask) Setup(ctx context.Context) error {
|
||||
// Clear existing issues to ensure a clean state for the task
|
||||
if err := t.svc.DeleteIssues(); err != nil {
|
||||
if err := ClearIssues(t.svc); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
issue := models.Issue{
|
||||
ID: "pm-abc",
|
||||
Title: "Create A New Issue",
|
||||
Description: description,
|
||||
IssueType: models.TypeTask,
|
||||
Status: models.StatusOpen,
|
||||
}
|
||||
issue := models.NewBaseIssue().
|
||||
WithTitle("Create a New Issue").WithDescription(description).Build()
|
||||
|
||||
return t.svc.Beads.CreateIssue(ctx, &issue, "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user