refactor to make cli better and simplify the code
This commit is contained in:
78
cmd/pm/runner.go
Normal file
78
cmd/pm/runner.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/cmd/pm/tasks"
|
||||
survey "github.com/LazyBachelor/LazyPM/internal/commands/survey"
|
||||
"github.com/LazyBachelor/LazyPM/internal/models"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func runStartCmd(cmd *cobra.Command, args []string) error {
|
||||
app := survey.AppFromContext(cmd.Context())
|
||||
interfaces := initInterfaces()
|
||||
surveyTasks := initTasks(app)
|
||||
|
||||
if cmd.Flags().Changed("interface") {
|
||||
if _, ok := interfaces[survey.InterfaceType]; !ok {
|
||||
return fmt.Errorf("invalid interface, valid are %v", task.ListInterfaces())
|
||||
}
|
||||
interfaces = map[string]task.Interface{
|
||||
survey.InterfaceType: interfaces[survey.InterfaceType],
|
||||
}
|
||||
}
|
||||
|
||||
if cmd.Flags().Changed("task") {
|
||||
if surveyTask := surveyTasks[survey.Task]; surveyTask == nil {
|
||||
return fmt.Errorf("invalid task, valid are %v", task.ListTasks())
|
||||
}
|
||||
|
||||
surveyTasks = map[string]task.Tasker{
|
||||
survey.Task: surveyTasks[survey.Task],
|
||||
}
|
||||
}
|
||||
|
||||
if err := newIntroModel().Run(); err != nil {
|
||||
return returnIfUserQuit(err, "failed to run intro")
|
||||
}
|
||||
|
||||
if err := taskLoop(cmd.Context(), surveyTasks, interfaces); err != nil {
|
||||
return returnIfUserQuit(err, "task loop failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func taskLoop(ctx context.Context, surveyTasks map[string]task.Tasker, interfaces map[string]task.Interface) error {
|
||||
var iNames []string
|
||||
for name := range interfaces {
|
||||
iNames = append(iNames, name)
|
||||
}
|
||||
|
||||
rand.Shuffle(len(iNames), func(i, j int) {
|
||||
iNames[i], iNames[j] = iNames[j], iNames[i]
|
||||
})
|
||||
|
||||
idx := 0
|
||||
for _, t := range surveyTasks {
|
||||
iIdx := idx % len(iNames)
|
||||
selected := interfaces[iNames[iIdx]]
|
||||
|
||||
if err := task.RunTask(ctx, t, selected, tasks.InterfaceToType(selected)); err != nil {
|
||||
return err
|
||||
}
|
||||
idx++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func returnIfUserQuit(err error, msg string) error {
|
||||
if errors.Is(err, models.ErrUserQuit) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s: %w", msg, err)
|
||||
}
|
||||
Reference in New Issue
Block a user