From c9e3696ffdf0381f6ad9ad0a6800ad42db7d5a8c Mon Sep 17 00:00:00 2001 From: oscarzhou Date: Sun, 8 Jan 2023 21:35:23 +1300 Subject: [PATCH] chore: move general task functions to a new folder --- go/tasks/{ => common}/exit.go | 5 ++++- go/tasks/common/general_task.go | 28 ++++++++++++++++++++++++++++ go/tasks/common/return.go | 22 ++++++++++++++++++++++ go/tasks/{ => common}/tasker.go | 13 +++++++++---- 4 files changed, 63 insertions(+), 5 deletions(-) rename go/tasks/{ => common}/exit.go (75%) create mode 100644 go/tasks/common/general_task.go create mode 100644 go/tasks/common/return.go rename go/tasks/{ => common}/tasker.go (78%) diff --git a/go/tasks/exit.go b/go/tasks/common/exit.go similarity index 75% rename from go/tasks/exit.go rename to go/tasks/common/exit.go index 2067151..f60404d 100644 --- a/go/tasks/exit.go +++ b/go/tasks/common/exit.go @@ -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" } diff --git a/go/tasks/common/general_task.go b/go/tasks/common/general_task.go new file mode 100644 index 0000000..55afc9c --- /dev/null +++ b/go/tasks/common/general_task.go @@ -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 "" +} diff --git a/go/tasks/common/return.go b/go/tasks/common/return.go new file mode 100644 index 0000000..9b6df28 --- /dev/null +++ b/go/tasks/common/return.go @@ -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" +} diff --git a/go/tasks/tasker.go b/go/tasks/common/tasker.go similarity index 78% rename from go/tasks/tasker.go rename to go/tasks/common/tasker.go index ce5be30..4088902 100644 --- a/go/tasks/tasker.go +++ b/go/tasks/common/tasker.go @@ -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 { - taskItems = append(taskItems, NewExitTask()) +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")