From a38e32aa78098e07fe5ce686aa2e1104a3f00c7f Mon Sep 17 00:00:00 2001 From: Robin Olsen Date: Tue, 17 Feb 2026 10:24:15 +0100 Subject: [PATCH] use cobra for cli delete pkg/runner and put funcs in task add start command --- cmd/survey/cmd.go | 48 +++++++++++++++++++++++++++++ cmd/survey/main.go | 17 ++++++++++ cmd/survey/{survey.go => runner.go} | 29 ----------------- pkg/task/runner.go | 44 -------------------------- pkg/task/task.go | 36 ++++++++++++++++++++++ 5 files changed, 101 insertions(+), 73 deletions(-) create mode 100644 cmd/survey/cmd.go create mode 100644 cmd/survey/main.go rename cmd/survey/{survey.go => runner.go} (69%) delete mode 100644 pkg/task/runner.go diff --git a/cmd/survey/cmd.go b/cmd/survey/cmd.go new file mode 100644 index 0000000..5d0b6fb --- /dev/null +++ b/cmd/survey/cmd.go @@ -0,0 +1,48 @@ +package main + +import ( + "errors" + "log" + "os" + + "github.com/LazyBachelor/LazyPM/pkg/task" + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "survey", + Short: "Run the user survey", +} + +var startCmd = &cobra.Command{ + Use: "start", + Short: "Start the user survey", + Run: func(cmd *cobra.Command, args []string) { + if err := newIntroModel().Run(); err != nil { + if errors.Is(err, ErrUserQuit) { + os.Exit(0) + } + log.Fatalf("Failed to run intro screen: %v\n", err) + } + + svc, close, err := initializeServices(cmd.Context()) + if err != nil { + log.Fatalf("Failed to initialize services: %v\n", err) + } + defer close() + + surveyTasks := initTasks() + interfaces := initInterfaces() + + if err := taskLoop(cmd.Context(), svc, surveyTasks, interfaces); err != nil { + if errors.Is(err, task.ErrUserQuit) { + os.Exit(0) + } + log.Fatalf("Task loop failed: %v\n", err) + } + }, +} + +func init() { + rootCmd.AddCommand(startCmd) +} diff --git a/cmd/survey/main.go b/cmd/survey/main.go new file mode 100644 index 0000000..2456cb0 --- /dev/null +++ b/cmd/survey/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "context" + "log" + + "github.com/charmbracelet/fang" +) + +func main() { + ctx := context.Background() + + if err := fang.Execute(ctx, rootCmd, + fang.WithColorSchemeFunc(fang.AnsiColorScheme)); err != nil { + log.Fatalf("Failed to execute command: %v\n", err) + } +} diff --git a/cmd/survey/survey.go b/cmd/survey/runner.go similarity index 69% rename from cmd/survey/survey.go rename to cmd/survey/runner.go index 429a559..a64190a 100644 --- a/cmd/survey/survey.go +++ b/cmd/survey/runner.go @@ -4,41 +4,12 @@ import ( "context" "errors" "fmt" - "log" "math/rand" - "os" "github.com/LazyBachelor/LazyPM/internal/service" "github.com/LazyBachelor/LazyPM/pkg/task" ) -func main() { - ctx := context.Background() - - if err := newIntroModel().Run(); err != nil { - if errors.Is(err, ErrUserQuit) { - os.Exit(0) - } - log.Fatalf("Failed to run intro screen: %v\n", err) - } - - svc, close, err := initializeServices(ctx) - if err != nil { - log.Fatalf("Failed to initialize services: %v\n", err) - } - defer close() - - surveyTasks := initTasks() - interfaces := initInterfaces() - - if err := taskLoop(ctx, svc, surveyTasks, interfaces); err != nil { - if errors.Is(err, task.ErrUserQuit) { - os.Exit(0) - } - log.Fatalf("Task loop failed: %v\n", err) - } -} - func taskLoop(ctx context.Context, svc *service.Services, surveyTasks []*task.Task, interfaces []task.Interface) error { interfaceIndex := rand.Int() % len(interfaces) diff --git a/pkg/task/runner.go b/pkg/task/runner.go deleted file mode 100644 index 5d43706..0000000 --- a/pkg/task/runner.go +++ /dev/null @@ -1,44 +0,0 @@ -package task - -import ( - "context" - "fmt" - - tea "github.com/charmbracelet/bubbletea" -) - -func (t *Task) IntroduceTask() error { - if t.aboutScreen == nil { - return fmt.Errorf("aboutScreen is not set") - } - model, err := tea.NewProgram(t.aboutScreen, tea.WithAltScreen()).Run() - if err != nil { - return err - } - if m, ok := model.(interface{ GetUserQuit() bool }); ok && m.GetUserQuit() { - return ErrUserQuit - } - return nil -} - -func (t *Task) StartInterface(ctx context.Context, cfg TaskConfig) error { - if t.interfaceType == nil { - return fmt.Errorf("interfaceType is not set") - } - - return t.interfaceType.Run(ctx, cfg) -} - -func (t *Task) StartQuestionnaire() error { - if t.questionnaire == nil { - return fmt.Errorf("questionnaire is not set") - } - model, err := tea.NewProgram(t.questionnaire, tea.WithAltScreen()).Run() - if err != nil { - return err - } - if m, ok := model.(interface{ GetUserQuit() bool }); ok && m.GetUserQuit() { - return ErrUserQuit - } - return nil -} diff --git a/pkg/task/task.go b/pkg/task/task.go index 2a9e548..f995446 100644 --- a/pkg/task/task.go +++ b/pkg/task/task.go @@ -25,6 +25,42 @@ func NewTask(aboutScreen tea.Model, questionnaire tea.Model) *Task { } } +func (t *Task) IntroduceTask() error { + if t.aboutScreen == nil { + return fmt.Errorf("aboutScreen is not set") + } + model, err := tea.NewProgram(t.aboutScreen, tea.WithAltScreen()).Run() + if err != nil { + return err + } + if m, ok := model.(interface{ GetUserQuit() bool }); ok && m.GetUserQuit() { + return ErrUserQuit + } + return nil +} + +func (t *Task) StartInterface(ctx context.Context, cfg TaskConfig) error { + if t.interfaceType == nil { + return fmt.Errorf("interfaceType is not set") + } + + return t.interfaceType.Run(ctx, cfg) +} + +func (t *Task) StartQuestionnaire() error { + if t.questionnaire == nil { + return fmt.Errorf("questionnaire is not set") + } + model, err := tea.NewProgram(t.questionnaire, tea.WithAltScreen()).Run() + if err != nil { + return err + } + if m, ok := model.(interface{ GetUserQuit() bool }); ok && m.GetUserQuit() { + return ErrUserQuit + } + return nil +} + func (t *Task) SetConfigFunc(fn ConfigFunc) { t.Config = fn() }