add command to specify task
add runTask to run spesific task with spesific interface
This commit is contained in:
@@ -3,14 +3,12 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/pkg/cli/repl"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/tui"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/web"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var interfaceType string
|
||||
var stage int
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "survey",
|
||||
@@ -42,29 +40,37 @@ var submitCmd = &cobra.Command{
|
||||
func runStartCmd(cmd *cobra.Command, args []string) error {
|
||||
interfaces := initInterfaces()
|
||||
|
||||
if cmd.Flags().Changed("interface") {
|
||||
switch interfaceType {
|
||||
case "tui":
|
||||
interfaces = []task.Interface{tui.NewTui()}
|
||||
case "repl":
|
||||
interfaces = []task.Interface{repl.NewRepl()}
|
||||
case "web":
|
||||
interfaces = []task.Interface{web.NewWeb()}
|
||||
default:
|
||||
return fmt.Errorf("invalid interface type: %s", interfaceType)
|
||||
}
|
||||
}
|
||||
|
||||
if err := newIntroModel().Run(); err != nil {
|
||||
return returnIfUserQuit(err, "failed to run intro")
|
||||
}
|
||||
|
||||
svc, cleanup, err := initializeServices(cmd.Context())
|
||||
if err != nil {
|
||||
return returnIfUserQuit(err, "failed to initialize services")
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
tasks := initTasks(svc)
|
||||
|
||||
if cmd.Flags().Changed("interface") {
|
||||
if _, ok := interfaces[interfaceType]; !ok {
|
||||
return fmt.Errorf("invalid interface, valid are (tui, repl, web)")
|
||||
}
|
||||
interfaces = map[string]task.Interface{
|
||||
interfaceType: interfaces[interfaceType],
|
||||
}
|
||||
}
|
||||
|
||||
if cmd.Flags().Changed("stage") {
|
||||
if stage < 1 || stage > len(tasks) {
|
||||
return fmt.Errorf("invalid stage")
|
||||
}
|
||||
if err := runTask(cmd.Context(), tasks[stage-1], interfaces[interfaceType]); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := newIntroModel().Run(); err != nil {
|
||||
return returnIfUserQuit(err, "failed to run intro")
|
||||
}
|
||||
|
||||
surveyTasks := initTasks(svc)
|
||||
|
||||
if err := taskLoop(cmd.Context(), surveyTasks, interfaces); err != nil {
|
||||
@@ -75,7 +81,8 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
|
||||
|
||||
func init() {
|
||||
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||
startCmd.Flags().StringVarP(&interfaceType, "interface", "i", "", "Specify which interface to use for the survey (tui, repl, web).")
|
||||
startCmd.Flags().StringVarP(&interfaceType, "interface", "i", "tui", "Specify interface.")
|
||||
startCmd.Flags().IntVarP(&stage, "stage", "s", 1, "Run stage directly")
|
||||
rootCmd.AddCommand(startCmd)
|
||||
rootCmd.AddCommand(submitCmd)
|
||||
}
|
||||
|
||||
@@ -27,6 +27,10 @@ func initTasks(svc *service.Services) []*task.Task {
|
||||
}
|
||||
}
|
||||
|
||||
func initInterfaces() []task.Interface {
|
||||
return []task.Interface{repl.NewRepl(), tui.NewTui(), web.NewWeb()}
|
||||
func initInterfaces() map[string]task.Interface {
|
||||
return map[string]task.Interface{
|
||||
"repl": repl.NewRepl(),
|
||||
"tui": tui.NewTui(),
|
||||
"web": web.NewWeb(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,7 +114,8 @@ func (m introModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
||||
func (m introModel) View() string {
|
||||
if m.width < 55 || m.height < 16 {
|
||||
return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, "Terminal too small.")
|
||||
return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center,
|
||||
style.TextStyle.Render("Terminal too small."))
|
||||
}
|
||||
|
||||
var content string
|
||||
@@ -127,10 +128,9 @@ func (m introModel) View() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
boxWidth := min(m.width-10, 90)
|
||||
boxWidth := min(m.width-10, 80)
|
||||
|
||||
boxStyle := lipgloss.NewStyle().
|
||||
Border(style.DefaultBorder).
|
||||
boxStyle := style.BorderStyle.
|
||||
Margin(1, 0).Padding(2, 4).Width(boxWidth)
|
||||
|
||||
var b strings.Builder
|
||||
@@ -138,7 +138,7 @@ func (m introModel) View() string {
|
||||
b.WriteString(style.TitleStyle.Render(IntroTitle))
|
||||
b.WriteString("\n")
|
||||
|
||||
b.WriteString(boxStyle.Render(content))
|
||||
b.WriteString(boxStyle.Render(style.TextStyle.Render(content)))
|
||||
b.WriteString("\n")
|
||||
|
||||
helpText := "Press " + keys.Continue.Help().Key + " to continue • " +
|
||||
|
||||
@@ -10,19 +10,15 @@ import (
|
||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||
)
|
||||
|
||||
func taskLoop(ctx context.Context, surveyTasks []*task.Task, interfaces []task.Interface) error {
|
||||
interfaceIndex := rand.Int() % len(interfaces)
|
||||
|
||||
for _, t := range surveyTasks {
|
||||
|
||||
t.SetInterface(interfaces[interfaceIndex])
|
||||
t.SetInterfaceType(tasks.InterfaceToType(interfaces[interfaceIndex]))
|
||||
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 := interfaces[interfaceIndex].(task.ValidatedInterface); ok {
|
||||
if validated, ok := i.(task.ValidatedInterface); ok {
|
||||
validated.SetChannels(feedbackChan, quitChan)
|
||||
}
|
||||
|
||||
@@ -60,10 +56,25 @@ func taskLoop(ctx context.Context, surveyTasks []*task.Task, interfaces []task.I
|
||||
if err := t.StartQuestionnaire(); err != nil {
|
||||
return returnIfUserQuit(err, "failed to start questionnaire")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
interfaceIndex++
|
||||
if interfaceIndex >= len(interfaces) {
|
||||
interfaceIndex = 0
|
||||
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)
|
||||
}
|
||||
|
||||
rand.Shuffle(len(ifaceNames), func(i, j int) {
|
||||
ifaceNames[i], ifaceNames[j] = ifaceNames[j], ifaceNames[i]
|
||||
})
|
||||
|
||||
for i, task := range surveyTasks {
|
||||
idx := i % len(ifaceNames)
|
||||
selected := interfaces[ifaceNames[idx]]
|
||||
|
||||
if err := runTask(ctx, task, selected); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -8,7 +8,7 @@ var (
|
||||
SecondaryColor = lipgloss.AdaptiveColor{Light: "#ff6f61", Dark: "#ff6347"}
|
||||
AccentColor = lipgloss.AdaptiveColor{Light: "#6a5acd", Dark: "#9370db"}
|
||||
Background = lipgloss.AdaptiveColor{Light: "#ffffff", Dark: "#1e1e1e"}
|
||||
TextColor = lipgloss.AdaptiveColor{Light: "#000000", Dark: "#ffffff"}
|
||||
TextColor = lipgloss.AdaptiveColor{Light: "#252525b7", Dark: "#ffffff"}
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -22,7 +22,6 @@ var (
|
||||
|
||||
var (
|
||||
TitleStyle = lipgloss.NewStyle().Foreground(PrimaryColor).Bold(true)
|
||||
DescriptionStyle = lipgloss.NewStyle().Foreground(TextColor).Italic(true)
|
||||
DetailStyle = lipgloss.NewStyle().Foreground(SecondaryColor)
|
||||
TextStyle = lipgloss.NewStyle().Foreground(TextColor)
|
||||
HelpStyle = lipgloss.NewStyle().Align(lipgloss.Center).Foreground(AccentColor)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user