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