make sure taks are in order and interfaces random
This commit is contained in:
@@ -137,16 +137,14 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
func taskLoop(ctx context.Context, application *task.App, surveyTasks map[string]task.Tasker, interfaces map[string]task.Interface) error {
|
||||
var iNames []string
|
||||
for name := range interfaces {
|
||||
iNames = append(iNames, name)
|
||||
}
|
||||
iNames := task.ListInterfaces()
|
||||
|
||||
if len(iNames) == 0 {
|
||||
return fmt.Errorf("no interfaces are available")
|
||||
}
|
||||
|
||||
if len(surveyTasks) == 0 {
|
||||
taskNames := task.ListTasks()
|
||||
if len(taskNames) == 0 {
|
||||
return fmt.Errorf("no tasks are available")
|
||||
}
|
||||
|
||||
@@ -155,7 +153,11 @@ func taskLoop(ctx context.Context, application *task.App, surveyTasks map[string
|
||||
})
|
||||
|
||||
idx := 0
|
||||
for _, t := range surveyTasks {
|
||||
for _, taskName := range taskNames {
|
||||
t, ok := surveyTasks[taskName]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
iIdx := idx % len(iNames)
|
||||
selected := interfaces[iNames[iIdx]]
|
||||
|
||||
|
||||
@@ -5,12 +5,14 @@ import (
|
||||
)
|
||||
|
||||
var interfaceRegistry = make(map[string]Interface)
|
||||
var interfaceOrder []string
|
||||
|
||||
func RegisterInterface(name string, iface Interface) {
|
||||
if _, exists := interfaceRegistry[name]; exists {
|
||||
panic(fmt.Sprintf("interface %q already registered", name))
|
||||
}
|
||||
interfaceRegistry[name] = iface
|
||||
interfaceOrder = append(interfaceOrder, name)
|
||||
}
|
||||
|
||||
func GetInterface(name string) (Interface, error) {
|
||||
@@ -22,20 +24,20 @@ func GetInterface(name string) (Interface, error) {
|
||||
}
|
||||
|
||||
func ListInterfaces() []string {
|
||||
names := make([]string, 0, len(interfaceRegistry))
|
||||
for name := range interfaceRegistry {
|
||||
names = append(names, name)
|
||||
}
|
||||
names := make([]string, len(interfaceOrder))
|
||||
copy(names, interfaceOrder)
|
||||
return names
|
||||
}
|
||||
|
||||
var taskRegistry = make(map[string]func(*App) Tasker)
|
||||
var taskOrder []string
|
||||
|
||||
func RegisterTask(name string, constructor func(*App) Tasker) {
|
||||
if _, exists := taskRegistry[name]; exists {
|
||||
panic(fmt.Sprintf("task %q already registered", name))
|
||||
}
|
||||
taskRegistry[name] = constructor
|
||||
taskOrder = append(taskOrder, name)
|
||||
}
|
||||
|
||||
func GetTask(name string, app *App) (Tasker, error) {
|
||||
@@ -47,9 +49,7 @@ func GetTask(name string, app *App) (Tasker, error) {
|
||||
}
|
||||
|
||||
func ListTasks() []string {
|
||||
names := make([]string, 0, len(taskRegistry))
|
||||
for name := range taskRegistry {
|
||||
names = append(names, name)
|
||||
}
|
||||
names := make([]string, len(taskOrder))
|
||||
copy(names, taskOrder)
|
||||
return names
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user