add command to specify task

add runTask to run spesific task with spesific interface
This commit is contained in:
Robin Olsen
2026-02-17 20:03:43 +01:00
parent 5044b31cff
commit 0ffe4f11b3
5 changed files with 98 additions and 77 deletions

View File

@@ -10,60 +10,71 @@ import (
"github.com/LazyBachelor/LazyPM/pkg/task"
)
func taskLoop(ctx context.Context, surveyTasks []*task.Task, interfaces []task.Interface) error {
interfaceIndex := rand.Int() % len(interfaces)
func runTask(ctx context.Context, t *task.Task, i task.Interface) error {
t.SetInterface(i)
t.SetInterfaceType(tasks.InterfaceToType(i))
for _, t := range surveyTasks {
doneChan := make(chan bool, 1)
quitChan := make(chan bool, 1)
feedbackChan := make(chan task.ValidationFeedback, 10)
t.SetInterface(interfaces[interfaceIndex])
t.SetInterfaceType(tasks.InterfaceToType(interfaces[interfaceIndex]))
if validated, ok := i.(task.ValidatedInterface); ok {
validated.SetChannels(feedbackChan, quitChan)
}
doneChan := make(chan bool, 1)
quitChan := make(chan bool, 1)
feedbackChan := make(chan task.ValidationFeedback, 10)
if err := t.Initialize(ctx); err != nil {
return fmt.Errorf("failed to initialize task: %w", err)
}
if validated, ok := interfaces[interfaceIndex].(task.ValidatedInterface); ok {
validated.SetChannels(feedbackChan, quitChan)
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.Initialize(ctx); err != nil {
return fmt.Errorf("failed to initialize task: %w", err)
}
if err := t.StartQuestionnaire(); err != nil {
return returnIfUserQuit(err, "failed to start questionnaire")
}
return nil
}
if err := t.IntroduceTask(); err != nil {
return returnIfUserQuit(err, "failed to display task introduction screen")
}
func taskLoop(ctx context.Context, surveyTasks []*task.Task, interfaces map[string]task.Interface) error {
var ifaceNames []string
for name := range interfaces {
ifaceNames = append(ifaceNames, name)
}
t.SetChannels(feedbackChan, doneChan, quitChan)
rand.Shuffle(len(ifaceNames), func(i, j int) {
ifaceNames[i], ifaceNames[j] = ifaceNames[j], ifaceNames[i]
})
go t.StartValidationLoop(ctx)
for i, task := range surveyTasks {
idx := i % len(ifaceNames)
selected := interfaces[ifaceNames[idx]]
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")
}
interfaceIndex++
if interfaceIndex >= len(interfaces) {
interfaceIndex = 0
if err := runTask(ctx, task, selected); err != nil {
return err
}
}
return nil