make tasks use map instead of slice
This commit is contained in:
@@ -26,15 +26,15 @@ func initInterfaces() map[string]task.Interface {
|
|||||||
return interfaces
|
return interfaces
|
||||||
}
|
}
|
||||||
|
|
||||||
func initTasks(app *service.App) []task.Tasker {
|
func initTasks(app *service.App) map[string]task.Tasker {
|
||||||
var taskList []task.Tasker
|
taskMap := make(map[string]task.Tasker)
|
||||||
for _, name := range task.ListTasks() {
|
for _, name := range task.ListTasks() {
|
||||||
t, err := task.GetTasks(name, app)
|
t, err := task.GetTask(name, app)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
taskList = append(taskList, t)
|
taskMap[name] = t
|
||||||
}
|
}
|
||||||
|
|
||||||
return taskList
|
return taskMap
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,15 +32,14 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Flags().Changed("stage") {
|
if cmd.Flags().Changed("task") {
|
||||||
if surveyCmd.Task < 1 || surveyCmd.Task > len(surveyTasks) {
|
if surveyTask := surveyTasks[surveyCmd.Task]; surveyTask == nil {
|
||||||
return fmt.Errorf("invalid stage")
|
return fmt.Errorf("invalid task, valid are %v", task.ListTasks())
|
||||||
}
|
}
|
||||||
if err := task.RunTask(cmd.Context(), surveyTasks[surveyCmd.Task-1],
|
|
||||||
interfaces[surveyCmd.InterfaceType], tasks.InterfaceToType(interfaces[surveyCmd.InterfaceType])); err != nil {
|
surveyTasks = map[string]task.Tasker{
|
||||||
return err
|
surveyCmd.Task: surveyTasks[surveyCmd.Task],
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := newIntroModel().Run(); err != nil {
|
if err := newIntroModel().Run(); err != nil {
|
||||||
@@ -53,7 +52,7 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskLoop(ctx context.Context, surveyTasks []task.Tasker, interfaces map[string]task.Interface) error {
|
func taskLoop(ctx context.Context, surveyTasks map[string]task.Tasker, interfaces map[string]task.Interface) error {
|
||||||
var iNames []string
|
var iNames []string
|
||||||
for name := range interfaces {
|
for name := range interfaces {
|
||||||
iNames = append(iNames, name)
|
iNames = append(iNames, name)
|
||||||
@@ -63,14 +62,16 @@ func taskLoop(ctx context.Context, surveyTasks []task.Tasker, interfaces map[str
|
|||||||
iNames[i], iNames[j] = iNames[j], iNames[i]
|
iNames[i], iNames[j] = iNames[j], iNames[i]
|
||||||
})
|
})
|
||||||
|
|
||||||
for i, t := range surveyTasks {
|
idx := 0
|
||||||
idx := i % len(iNames)
|
for _, t := range surveyTasks {
|
||||||
selected := interfaces[iNames[idx]]
|
iIdx := idx % len(iNames)
|
||||||
|
selected := interfaces[iNames[iIdx]]
|
||||||
|
|
||||||
if err := task.RunTask(ctx, t, selected, tasks.InterfaceToType(selected)); err != nil {
|
if err := task.RunTask(ctx, t, selected, tasks.InterfaceToType(selected)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
idx++
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,6 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
InterfaceType string
|
|
||||||
Task int
|
|
||||||
)
|
|
||||||
|
|
||||||
// RootCmd is the base command for the survey CLI.
|
// RootCmd is the base command for the survey CLI.
|
||||||
var RootCmd = &cobra.Command{
|
var RootCmd = &cobra.Command{
|
||||||
Use: "survey",
|
Use: "survey",
|
||||||
@@ -24,6 +19,4 @@ Your responses will be kept confidential and used solely for research purposes.`
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RootCmd.CompletionOptions.DisableDefaultCmd = true
|
RootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||||
StartCmd.Flags().StringVarP(&InterfaceType, "interface", "i", "tui", "Specify interface.")
|
|
||||||
StartCmd.Flags().IntVarP(&Task, "task", "t", 1, "Run task directly")
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,18 @@ package surveyCmd
|
|||||||
|
|
||||||
import "github.com/spf13/cobra"
|
import "github.com/spf13/cobra"
|
||||||
|
|
||||||
|
var (
|
||||||
|
InterfaceType string
|
||||||
|
Task string
|
||||||
|
)
|
||||||
|
|
||||||
// StartCmd is the start command - RunE is set in cmd/survey/
|
// StartCmd is the start command - RunE is set in cmd/survey/
|
||||||
var StartCmd = &cobra.Command{
|
var StartCmd = &cobra.Command{
|
||||||
Use: "start",
|
Use: "start",
|
||||||
Short: "Start the user survey",
|
Short: "Start the user survey",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
StartCmd.Flags().StringVarP(&Task, "task", "t", "", "Specify task.")
|
||||||
|
StartCmd.Flags().StringVarP(&InterfaceType, "interface", "i", "", "Specify interface.")
|
||||||
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func RegisterTask(name string, constructor func(*service.App) Tasker) {
|
|||||||
taskRegistry[name] = constructor
|
taskRegistry[name] = constructor
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTasks(name string, app *service.App) (Tasker, error) {
|
func GetTask(name string, app *service.App) (Tasker, error) {
|
||||||
constructor, ok := taskRegistry[name]
|
constructor, ok := taskRegistry[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("task %q not found", name)
|
return nil, fmt.Errorf("task %q not found", name)
|
||||||
|
|||||||
Reference in New Issue
Block a user