refactor to use Tasker interface instead of Task

add tasker register for registering tasks for extiensibility
This commit is contained in:
Robin Olsen
2026-02-18 13:07:23 +01:00
parent 89f8596884
commit 23747fb799
9 changed files with 276 additions and 300 deletions

View File

@@ -10,56 +10,11 @@ import (
"github.com/LazyBachelor/LazyPM/pkg/task"
)
func runTask(ctx context.Context, t *task.Task, i task.Interface) error {
t.SetInterface(i)
t.SetInterfaceType(tasks.InterfaceToType(i))
doneChan := make(chan bool, 1)
quitChan := make(chan bool, 1)
feedbackChan := make(chan task.ValidationFeedback, 10)
if validated, ok := i.(task.ValidatedInterface); ok {
validated.SetChannels(feedbackChan, quitChan)
}
if err := t.Initialize(ctx); err != nil {
return fmt.Errorf("failed to initialize task: %w", err)
}
if err := t.IntroduceTask(); err != nil {
return returnIfUserQuit(err, "failed to display task introduction screen")
}
t.SetChannels(feedbackChan, doneChan, quitChan)
go t.StartValidationLoop(ctx)
interfaceDone := make(chan error, 1)
go func() {
interfaceDone <- t.StartInterface(ctx, t.Config)
}()
select {
case <-doneChan:
close(quitChan)
<-interfaceDone
fmt.Println("Task completed successfully!")
case err := <-interfaceDone:
close(quitChan)
if err != nil {
return returnIfUserQuit(err, "failed to start task interface")
}
fmt.Println("Task incomplete - you exited early")
}
if err := t.StartQuestionnaire(); err != nil {
return returnIfUserQuit(err, "failed to start questionnaire")
}
return nil
func runTask(ctx context.Context, t task.Tasker, i task.Interface) error {
return task.RunTask(ctx, t, i, tasks.InterfaceToType(i))
}
func taskLoop(ctx context.Context, surveyTasks []*task.Task, interfaces map[string]task.Interface) error {
func taskLoop(ctx context.Context, surveyTasks []task.Tasker, interfaces map[string]task.Interface) error {
var ifaceNames []string
for name := range interfaces {
ifaceNames = append(ifaceNames, name)
@@ -69,11 +24,11 @@ func taskLoop(ctx context.Context, surveyTasks []*task.Task, interfaces map[stri
ifaceNames[i], ifaceNames[j] = ifaceNames[j], ifaceNames[i]
})
for i, task := range surveyTasks {
for i, t := range surveyTasks {
idx := i % len(ifaceNames)
selected := interfaces[ifaceNames[idx]]
if err := runTask(ctx, task, selected); err != nil {
if err := runTask(ctx, t, selected); err != nil {
return err
}
}
@@ -81,7 +36,7 @@ func taskLoop(ctx context.Context, surveyTasks []*task.Task, interfaces map[stri
}
func returnIfUserQuit(err error, msg string) error {
if errors.Is(err, ErrUserQuit) {
if errors.Is(err, task.ErrUserQuit) {
return nil
}
return fmt.Errorf("%s: %w", msg, err)