add some helpers to simplify task creation
This commit is contained in:
@@ -6,14 +6,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/cli/repl"
|
"github.com/LazyBachelor/LazyPM/cmd/survey/tasks"
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/tui"
|
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/web"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func runTask(ctx context.Context, t task.Tasker, i task.Interface) error {
|
func runTask(ctx context.Context, t task.Tasker, i task.Interface) error {
|
||||||
return task.RunTask(ctx, t, i, InterfaceToType(i))
|
return task.RunTask(ctx, t, i, tasks.InterfaceToType(i))
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskLoop(ctx context.Context, surveyTasks []task.Tasker, interfaces map[string]task.Interface) error {
|
func taskLoop(ctx context.Context, surveyTasks []task.Tasker, interfaces map[string]task.Interface) error {
|
||||||
@@ -37,19 +35,6 @@ func taskLoop(ctx context.Context, surveyTasks []task.Tasker, interfaces map[str
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func InterfaceToType(it task.Interface) task.InterfaceType {
|
|
||||||
switch it.(type) {
|
|
||||||
case *repl.REPL:
|
|
||||||
return task.InterfaceCLI
|
|
||||||
case *tui.Tui:
|
|
||||||
return task.InterfaceTUI
|
|
||||||
case *web.Web:
|
|
||||||
return task.InterfaceWeb
|
|
||||||
default:
|
|
||||||
return task.InterfaceType("unknown")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func returnIfUserQuit(err error, msg string) error {
|
func returnIfUserQuit(err error, msg string) error {
|
||||||
if errors.Is(err, task.ErrUserQuit) {
|
if errors.Is(err, task.ErrUserQuit) {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
101
cmd/survey/tasks/base.go
Normal file
101
cmd/survey/tasks/base.go
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
package tasks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/LazyBachelor/LazyPM/pkg/cli/repl"
|
||||||
|
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||||
|
taskui "github.com/LazyBachelor/LazyPM/pkg/task/ui"
|
||||||
|
"github.com/LazyBachelor/LazyPM/pkg/tui"
|
||||||
|
"github.com/LazyBachelor/LazyPM/pkg/web"
|
||||||
|
"github.com/charmbracelet/huh"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
InterfaceTUI task.InterfaceType = "tui"
|
||||||
|
InterfaceREPL task.InterfaceType = "repl"
|
||||||
|
InterfaceWeb task.InterfaceType = "web"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InterfaceToType(it task.Interface) task.InterfaceType {
|
||||||
|
switch it.(type) {
|
||||||
|
case *repl.REPL:
|
||||||
|
return InterfaceREPL
|
||||||
|
case *tui.Tui:
|
||||||
|
return InterfaceTUI
|
||||||
|
case *web.Web:
|
||||||
|
return InterfaceWeb
|
||||||
|
default:
|
||||||
|
return task.InterfaceType("unknown")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BaseDetails() taskui.TaskDetails {
|
||||||
|
return taskui.TaskDetails{
|
||||||
|
Title: "Base Task",
|
||||||
|
Description: "This is a base task.",
|
||||||
|
TimeToComplete: "10m",
|
||||||
|
Difficulty: "Easy",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BaseConfig() task.TaskConfig {
|
||||||
|
return task.TaskConfig{
|
||||||
|
IssuePrefix: "pm",
|
||||||
|
BeadsDBPath: "./.pm/db.db",
|
||||||
|
StatisticsStoragePath: "./.pm/stats.json",
|
||||||
|
WebAddress: ":8080",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BaseQuestions(interfaceType task.InterfaceType) taskui.Questions {
|
||||||
|
var taskRating int
|
||||||
|
return taskui.Questions{
|
||||||
|
huh.NewGroup(
|
||||||
|
huh.NewConfirm().
|
||||||
|
Title("Did you complete the task?"),
|
||||||
|
),
|
||||||
|
huh.NewGroup(
|
||||||
|
huh.NewSelect[int]().Value(&taskRating).
|
||||||
|
Options(
|
||||||
|
huh.NewOption("Very easy", 1),
|
||||||
|
huh.NewOption("Easy", 2),
|
||||||
|
huh.NewOption("Moderate", 3),
|
||||||
|
huh.NewOption("Hard", 4),
|
||||||
|
).
|
||||||
|
Title("How difficult was the task?"),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
if interfaceType != InterfaceREPL {
|
||||||
|
return *questions
|
||||||
|
}
|
||||||
|
*questions = append(*questions, huh.NewGroup(field...))
|
||||||
|
return *questions
|
||||||
|
}
|
||||||
|
|
||||||
|
func AppendWebQuestion(questions *taskui.Questions, interfaceType task.InterfaceType, field ...huh.Field) taskui.Questions {
|
||||||
|
if interfaceType != InterfaceWeb {
|
||||||
|
return *questions
|
||||||
|
}
|
||||||
|
*questions = append(*questions, huh.NewGroup(field...))
|
||||||
|
return *questions
|
||||||
|
}
|
||||||
|
|
||||||
|
func AppendTUIQuestion(questions *taskui.Questions, interfaceType task.InterfaceType, field ...huh.Field) taskui.Questions {
|
||||||
|
if interfaceType != InterfaceTUI {
|
||||||
|
return *questions
|
||||||
|
}
|
||||||
|
*questions = append(*questions, huh.NewGroup(field...))
|
||||||
|
return *questions
|
||||||
|
}
|
||||||
@@ -6,7 +6,6 @@ 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.
|
||||||
@@ -23,37 +22,22 @@ func NewCodingTask(svc *service.Services) *CodingTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *CodingTask) Config() task.TaskConfig {
|
func (t *CodingTask) Config() task.TaskConfig {
|
||||||
return task.TaskConfig{
|
config := BaseConfig()
|
||||||
IssuePrefix: "pm",
|
config.StatisticsStoragePath = "./.pm/coding-task-stats.json"
|
||||||
BeadsDBPath: "./.pm/db.db",
|
return config
|
||||||
StatisticsStoragePath: "./.pm/coding-stats.json",
|
|
||||||
WebAddress: ":8080",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CodingTask) Details() taskui.TaskDetails {
|
func (t *CodingTask) Details() taskui.TaskDetails {
|
||||||
return taskui.TaskDetails{
|
details := BaseDetails()
|
||||||
Title: "Coding Task",
|
details.Title = "Coding Task"
|
||||||
Description: codingDescription,
|
details.Description = codingDescription
|
||||||
TimeToComplete: "10m",
|
return details
|
||||||
Difficulty: "Easy",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CodingTask) Questions(interfaceType task.InterfaceType) taskui.Questions {
|
func (t *CodingTask) Questions(interfaceType task.InterfaceType) (questions taskui.Questions) {
|
||||||
return taskui.Questions{
|
questions = append(questions, BaseQuestions(interfaceType)...)
|
||||||
huh.NewGroup(huh.NewConfirm().Title("Did you complete the coding task?")),
|
|
||||||
huh.NewGroup(
|
return questions
|
||||||
huh.NewSelect[int]().
|
|
||||||
Options(
|
|
||||||
huh.NewOption("Very easy", 1),
|
|
||||||
huh.NewOption("Easy", 2),
|
|
||||||
huh.NewOption("Moderate", 3),
|
|
||||||
huh.NewOption("Hard", 4),
|
|
||||||
).
|
|
||||||
Title("How difficult was the task?"),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CodingTask) Setup(ctx context.Context) error {
|
func (t *CodingTask) Setup(ctx context.Context) error {
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ 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 description = `You are tasked with creating a new issue in the project management system.
|
const description = `You are tasked with creating a new issue in the project management system.
|
||||||
@@ -26,35 +25,21 @@ func NewCreateIssueTask(svc *service.Services) *CreateIssueTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *CreateIssueTask) Config() task.TaskConfig {
|
func (t *CreateIssueTask) Config() task.TaskConfig {
|
||||||
return task.TaskConfig{
|
config := BaseConfig()
|
||||||
IssuePrefix: "pm",
|
config.StatisticsStoragePath = "./.pm/create-issue-stats.json"
|
||||||
BeadsDBPath: "./.pm/db.db",
|
return config
|
||||||
StatisticsStoragePath: "./.pm/task-1-stats.json",
|
|
||||||
WebAddress: ":8080",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CreateIssueTask) Details() taskui.TaskDetails {
|
func (t *CreateIssueTask) Details() taskui.TaskDetails {
|
||||||
return taskui.TaskDetails{
|
details := BaseDetails()
|
||||||
Title: "Create Issue Task",
|
details.Title = "Create Issue Task"
|
||||||
Description: "Create a new issue in the project management system...",
|
details.Description = description
|
||||||
TimeToComplete: "15m",
|
return details
|
||||||
Difficulty: "Hard",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CreateIssueTask) Questions(interfaceType task.InterfaceType) taskui.Questions {
|
func (t *CreateIssueTask) Questions(interfaceType task.InterfaceType) taskui.Questions {
|
||||||
return taskui.Questions{
|
questions := BaseQuestions(interfaceType)
|
||||||
huh.NewGroup(huh.NewConfirm().Title("Was this good")),
|
return questions
|
||||||
huh.NewGroup(
|
|
||||||
huh.NewSelect[int]().
|
|
||||||
Options(
|
|
||||||
huh.NewOption("Very good", 1),
|
|
||||||
huh.NewOption("Very Bad", 2),
|
|
||||||
).
|
|
||||||
Title("How good was it?"),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CreateIssueTask) Setup(ctx context.Context) error {
|
func (t *CreateIssueTask) Setup(ctx context.Context) error {
|
||||||
|
|||||||
@@ -9,21 +9,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TaskConfig = service.Config
|
type TaskConfig = service.Config
|
||||||
|
type InterfaceType string
|
||||||
var ErrUserQuit = fmt.Errorf("user quit")
|
|
||||||
|
|
||||||
type Interface interface {
|
type Interface interface {
|
||||||
Run(context.Context, TaskConfig) error
|
Run(context.Context, TaskConfig) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type InterfaceType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
InterfaceTUI InterfaceType = "tui"
|
|
||||||
InterfaceCLI InterfaceType = "repl"
|
|
||||||
InterfaceWeb InterfaceType = "web"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Tasker interface {
|
type Tasker interface {
|
||||||
Config() TaskConfig
|
Config() TaskConfig
|
||||||
Details() taskui.TaskDetails
|
Details() taskui.TaskDetails
|
||||||
@@ -41,3 +32,5 @@ type ValidatedInterface interface {
|
|||||||
Interface
|
Interface
|
||||||
SetChannels(feedbackChan chan ValidationFeedback, quitChan chan bool)
|
SetChannels(feedbackChan chan ValidationFeedback, quitChan chan bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrUserQuit = fmt.Errorf("user quit")
|
||||||
|
|||||||
Reference in New Issue
Block a user