chore: move general task functions to a new folder

This commit is contained in:
oscarzhou 2023-01-08 21:35:23 +13:00
parent 4940258365
commit c9e3696ffd
4 changed files with 63 additions and 5 deletions

View File

@ -1,4 +1,4 @@
package tasks
package common
import "errors"
@ -13,6 +13,9 @@ func (task *ExitTask) Execute() error {
return errors.New("exit")
}
func (task *ExitTask) SetParentTaskers(tasks []Tasker) {
}
func (task *ExitTask) String() string {
return "Exit"
}

View File

@ -0,0 +1,28 @@
package common
import (
"ocl/portainer-devtool/configs"
)
type GeneralTask struct {
Config *configs.Config
ParentTasks []Tasker
}
func NewGeneralTask(cfg *configs.Config) *GeneralTask {
return &GeneralTask{
Config: cfg,
}
}
func (task *GeneralTask) Execute() error {
return nil
}
func (task *GeneralTask) SetParentTaskers(tasks []Tasker) {
task.ParentTasks = tasks
}
func (task *GeneralTask) String() string {
return ""
}

22
go/tasks/common/return.go Normal file
View File

@ -0,0 +1,22 @@
package common
type ReturnTask struct {
ParentTasks []Tasker
}
func NewReturnTask(tasks []Tasker) *ReturnTask {
return &ReturnTask{
ParentTasks: tasks,
}
}
func (task *ReturnTask) Execute() error {
return ListCommandMenu(task.ParentTasks, "", false, nil)
}
func (task *ReturnTask) SetParentTaskers(tasks []Tasker) {
}
func (task *ReturnTask) String() string {
return "Go Back"
}

View File

@ -1,4 +1,4 @@
package tasks
package common
import (
"fmt"
@ -10,11 +10,17 @@ import (
type Tasker interface {
Execute() error
String() string
SetParentTaskers(tasks []Tasker)
}
// ListCommandMenu iterates task items to display them // on the screen as the menu options
func ListCommandMenu(taskItems []Tasker, menuDesp string) error {
func ListCommandMenu(taskItems []Tasker, menuDesp string, rootMenu bool, parentTaskItems []Tasker) error {
if rootMenu {
taskItems = append(taskItems, NewExitTask())
} else {
taskItems = append(taskItems, NewReturnTask(parentTaskItems))
}
for {
printMainMenu := func() {
taskNames := []string{}
@ -33,7 +39,6 @@ func ListCommandMenu(taskItems []Tasker, menuDesp string) error {
}
option := utils.SelectMenuItem(printMainMenu)
index, err := strconv.Atoi(option)
if err != nil {
utils.ErrorPrint("please type the option number\n")