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) {
|
func initializeServices(ctx context.Context) (*service.Services, func(), error) {
|
||||||
config := service.Config{
|
return service.NewServices(ctx, tasks.BaseConfig())
|
||||||
IssuePrefix: "pm",
|
|
||||||
BeadsDBPath: "./.pm/db.db",
|
|
||||||
StatisticsStoragePath: "./.pm/stats.json",
|
|
||||||
WebAddress: ":8080",
|
|
||||||
}
|
|
||||||
return service.NewServices(ctx, config)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func initInterfaces() map[string]task.Interface {
|
func initInterfaces() map[string]task.Interface {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package tasks
|
package tasks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/cli/repl"
|
"github.com/LazyBachelor/LazyPM/pkg/cli/repl"
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||||
taskui "github.com/LazyBachelor/LazyPM/pkg/task/ui"
|
taskui "github.com/LazyBachelor/LazyPM/pkg/task/ui"
|
||||||
@@ -40,12 +41,16 @@ func BaseDetails() taskui.TaskDetails {
|
|||||||
func BaseConfig() task.TaskConfig {
|
func BaseConfig() task.TaskConfig {
|
||||||
return task.TaskConfig{
|
return task.TaskConfig{
|
||||||
IssuePrefix: "pm",
|
IssuePrefix: "pm",
|
||||||
|
WebAddress: ":8080",
|
||||||
BeadsDBPath: "./.pm/db.db",
|
BeadsDBPath: "./.pm/db.db",
|
||||||
StatisticsStoragePath: "./.pm/stats.json",
|
StatisticsStoragePath: "./.pm/stats.json",
|
||||||
WebAddress: ":8080",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ClearIssues(svc *service.Services) error {
|
||||||
|
return svc.Beads.DeleteIssues()
|
||||||
|
}
|
||||||
|
|
||||||
func BaseQuestions(interfaceType task.InterfaceType) taskui.Questions {
|
func BaseQuestions(interfaceType task.InterfaceType) taskui.Questions {
|
||||||
var taskRating int
|
var taskRating int
|
||||||
return taskui.Questions{
|
return taskui.Questions{
|
||||||
@@ -66,36 +71,23 @@ func BaseQuestions(interfaceType task.InterfaceType) taskui.Questions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func AppendGroup(questions *taskui.Questions, group *huh.Group) taskui.Questions {
|
func ReplQuestion(interfaceType task.InterfaceType, fields ...huh.Field) *huh.Group {
|
||||||
*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 {
|
|
||||||
if interfaceType != InterfaceREPL {
|
if interfaceType != InterfaceREPL {
|
||||||
return *questions
|
return nil
|
||||||
}
|
}
|
||||||
*questions = append(*questions, huh.NewGroup(field...))
|
return huh.NewGroup(fields...)
|
||||||
return *questions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
if interfaceType != InterfaceWeb {
|
||||||
return *questions
|
return nil
|
||||||
}
|
}
|
||||||
*questions = append(*questions, huh.NewGroup(field...))
|
return huh.NewGroup(fields...)
|
||||||
return *questions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
if interfaceType != InterfaceTUI {
|
||||||
return *questions
|
return nil
|
||||||
}
|
}
|
||||||
*questions = append(*questions, huh.NewGroup(field...))
|
return huh.NewGroup(fields...)
|
||||||
return *questions
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||||
taskui "github.com/LazyBachelor/LazyPM/pkg/task/ui"
|
taskui "github.com/LazyBachelor/LazyPM/pkg/task/ui"
|
||||||
|
"github.com/charmbracelet/huh"
|
||||||
)
|
)
|
||||||
|
|
||||||
const codingDescription = `You are tasked with writing a simple function.
|
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 {
|
func (t *CodingTask) Config() task.TaskConfig {
|
||||||
config := BaseConfig()
|
return BaseConfig().WithStatisticsStoragePath("./.pm/coding-task-stats.json")
|
||||||
config.StatisticsStoragePath = "./.pm/coding-task-stats.json"
|
|
||||||
return config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CodingTask) Details() taskui.TaskDetails {
|
func (t *CodingTask) Details() taskui.TaskDetails {
|
||||||
details := BaseDetails()
|
return BaseDetails().WithTitle("Coding Task").WithDescription(codingDescription)
|
||||||
details.Title = "Coding Task"
|
|
||||||
details.Description = codingDescription
|
|
||||||
return details
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CodingTask) Questions(interfaceType task.InterfaceType) (questions taskui.Questions) {
|
func (t *CodingTask) Questions(interfaceType task.InterfaceType) (questions taskui.Questions) {
|
||||||
questions = append(questions, BaseQuestions(interfaceType)...)
|
return BaseQuestions(interfaceType).
|
||||||
|
With(ReplQuestion(interfaceType,
|
||||||
return questions
|
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 {
|
func (t *CodingTask) Setup(ctx context.Context) error {
|
||||||
if err := t.svc.DeleteIssues(); err != nil {
|
if err := ClearIssues(t.svc); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
content := textFileContent
|
content := textFileContent
|
||||||
|
|
||||||
if err := os.WriteFile("./code.txt", []byte(content), 0644); err != nil {
|
if err := os.WriteFile("./code.txt", []byte(content), 0644); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,36 +25,25 @@ func NewCreateIssueTask(svc *service.Services) *CreateIssueTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *CreateIssueTask) Config() task.TaskConfig {
|
func (t *CreateIssueTask) Config() task.TaskConfig {
|
||||||
config := BaseConfig()
|
return BaseConfig().WithStatisticsStoragePath("./.pm/create-issue-stats.json")
|
||||||
config.StatisticsStoragePath = "./.pm/create-issue-stats.json"
|
|
||||||
return config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CreateIssueTask) Details() taskui.TaskDetails {
|
func (t *CreateIssueTask) Details() taskui.TaskDetails {
|
||||||
details := BaseDetails()
|
return BaseDetails().WithTitle("Create Issue Task").WithDescription(description)
|
||||||
details.Title = "Create Issue Task"
|
|
||||||
details.Description = description
|
|
||||||
return details
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CreateIssueTask) Questions(interfaceType task.InterfaceType) taskui.Questions {
|
func (t *CreateIssueTask) Questions(interfaceType task.InterfaceType) taskui.Questions {
|
||||||
questions := BaseQuestions(interfaceType)
|
return BaseQuestions(interfaceType)
|
||||||
return questions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CreateIssueTask) Setup(ctx context.Context) error {
|
func (t *CreateIssueTask) Setup(ctx context.Context) error {
|
||||||
// Clear existing issues to ensure a clean state for the task
|
// 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
issue := models.Issue{
|
issue := models.NewBaseIssue().
|
||||||
ID: "pm-abc",
|
WithTitle("Create a New Issue").WithDescription(description).Build()
|
||||||
Title: "Create A New Issue",
|
|
||||||
Description: description,
|
|
||||||
IssueType: models.TypeTask,
|
|
||||||
Status: models.StatusOpen,
|
|
||||||
}
|
|
||||||
|
|
||||||
return t.svc.Beads.CreateIssue(ctx, &issue, "")
|
return t.svc.Beads.CreateIssue(ctx, &issue, "")
|
||||||
}
|
}
|
||||||
|
|||||||
64
internal/models/issue.go
Normal file
64
internal/models/issue.go
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type IssueBuilder struct {
|
||||||
|
ID string
|
||||||
|
Title string
|
||||||
|
Description string
|
||||||
|
Status Status
|
||||||
|
IssueType IssueType
|
||||||
|
Priority int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIssueBuilder() *IssueBuilder {
|
||||||
|
return &IssueBuilder{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBaseIssue() *IssueBuilder {
|
||||||
|
return NewIssueBuilder().
|
||||||
|
WithID("pm-abc").
|
||||||
|
WithTitle("Basic Issue").
|
||||||
|
WithDescription("Basic Description").
|
||||||
|
WithIssueType(TypeTask).
|
||||||
|
WithStatus(StatusOpen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *IssueBuilder) WithID(id string) *IssueBuilder {
|
||||||
|
b.ID = id
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *IssueBuilder) WithTitle(title string) *IssueBuilder {
|
||||||
|
b.Title = title
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *IssueBuilder) WithDescription(description string) *IssueBuilder {
|
||||||
|
b.Description = description
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *IssueBuilder) WithStatus(status Status) *IssueBuilder {
|
||||||
|
b.Status = status
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *IssueBuilder) WithIssueType(issueType IssueType) *IssueBuilder {
|
||||||
|
b.IssueType = issueType
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *IssueBuilder) WithPriority(priority int) *IssueBuilder {
|
||||||
|
b.Priority = priority
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b IssueBuilder) Build() Issue {
|
||||||
|
return Issue{
|
||||||
|
ID: b.ID,
|
||||||
|
Title: b.Title,
|
||||||
|
Description: b.Description,
|
||||||
|
Status: b.Status,
|
||||||
|
IssueType: b.IssueType,
|
||||||
|
Priority: b.Priority,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,3 +41,13 @@ func (s *BeadsService) AllIssues(ctx context.Context) ([]models.Issue, error) {
|
|||||||
|
|
||||||
return issues, nil
|
return issues, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *BeadsService) DeleteIssues() error {
|
||||||
|
|
||||||
|
var deleteIssues = "DELETE FROM issues;"
|
||||||
|
|
||||||
|
if _, err := s.UnderlyingDB().Exec(deleteIssues); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
34
internal/service/config.go
Normal file
34
internal/service/config.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
RootCmd string
|
||||||
|
WebAddress string
|
||||||
|
BeadsDBPath string
|
||||||
|
IssuePrefix string
|
||||||
|
StatisticsStoragePath string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) WithRootCmd(rootCmd string) Config {
|
||||||
|
c.RootCmd = rootCmd
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) WithWebAddress(webAddress string) Config {
|
||||||
|
c.WebAddress = webAddress
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) WithBeadsDBPath(beadsDBPath string) Config {
|
||||||
|
c.BeadsDBPath = beadsDBPath
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) WithIssuePrefix(issuePrefix string) Config {
|
||||||
|
c.IssuePrefix = issuePrefix
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) WithStatisticsStoragePath(path string) Config {
|
||||||
|
c.StatisticsStoragePath = path
|
||||||
|
return c
|
||||||
|
}
|
||||||
@@ -7,8 +7,6 @@ import (
|
|||||||
"github.com/charmbracelet/huh"
|
"github.com/charmbracelet/huh"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Questions []*huh.Group
|
|
||||||
|
|
||||||
type QuestionnaireModel struct {
|
type QuestionnaireModel struct {
|
||||||
Questions
|
Questions
|
||||||
form *huh.Form
|
form *huh.Form
|
||||||
|
|||||||
@@ -10,13 +10,6 @@ import (
|
|||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TaskDetails struct {
|
|
||||||
Title string
|
|
||||||
Description string
|
|
||||||
TimeToComplete string
|
|
||||||
Difficulty string
|
|
||||||
}
|
|
||||||
|
|
||||||
type TaskModel struct {
|
type TaskModel struct {
|
||||||
TaskDetails
|
TaskDetails
|
||||||
keys TaskHelpKeys
|
keys TaskHelpKeys
|
||||||
|
|||||||
39
pkg/task/ui/types.go
Normal file
39
pkg/task/ui/types.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package taskui
|
||||||
|
|
||||||
|
import "github.com/charmbracelet/huh"
|
||||||
|
|
||||||
|
type TaskDetails struct {
|
||||||
|
Title string
|
||||||
|
Description string
|
||||||
|
TimeToComplete string
|
||||||
|
Difficulty string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (td TaskDetails) WithTitle(title string) TaskDetails {
|
||||||
|
td.Title = title
|
||||||
|
return td
|
||||||
|
}
|
||||||
|
|
||||||
|
func (td TaskDetails) WithDescription(description string) TaskDetails {
|
||||||
|
td.Description = description
|
||||||
|
return td
|
||||||
|
}
|
||||||
|
|
||||||
|
func (td TaskDetails) WithTimeToComplete(time string) TaskDetails {
|
||||||
|
td.TimeToComplete = time
|
||||||
|
return td
|
||||||
|
}
|
||||||
|
|
||||||
|
func (td TaskDetails) WithDifficulty(difficulty string) TaskDetails {
|
||||||
|
td.Difficulty = difficulty
|
||||||
|
return td
|
||||||
|
}
|
||||||
|
|
||||||
|
type Questions []*huh.Group
|
||||||
|
|
||||||
|
func (q Questions) With(group *huh.Group) Questions {
|
||||||
|
if group == nil {
|
||||||
|
return q
|
||||||
|
}
|
||||||
|
return append(q, group)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user