2023-01-08 19:35:23 +11:00
|
|
|
package common
|
2022-09-21 15:42:47 +10:00
|
|
|
|
2022-12-25 23:56:02 +11:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"ocl/portainer-devtool/utils"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2022-09-21 15:42:47 +10:00
|
|
|
type Tasker interface {
|
|
|
|
Execute() error
|
|
|
|
String() string
|
2023-01-08 19:35:23 +11:00
|
|
|
SetParentTaskers(tasks []Tasker)
|
2022-09-21 15:42:47 +10:00
|
|
|
}
|
2022-12-25 23:56:02 +11:00
|
|
|
|
|
|
|
// ListCommandMenu iterates task items to display them // on the screen as the menu options
|
2023-01-08 19:35:23 +11:00
|
|
|
func ListCommandMenu(taskItems []Tasker, menuDesp string, rootMenu bool, parentTaskItems []Tasker) error {
|
|
|
|
if rootMenu {
|
|
|
|
taskItems = append(taskItems, NewExitTask())
|
|
|
|
} else {
|
|
|
|
taskItems = append(taskItems, NewReturnTask(parentTaskItems))
|
|
|
|
}
|
|
|
|
|
2022-12-25 23:56:02 +11:00
|
|
|
for {
|
|
|
|
printMainMenu := func() {
|
|
|
|
taskNames := []string{}
|
|
|
|
for _, task := range taskItems {
|
|
|
|
taskNames = append(taskNames, task.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.PrintMenu(menuDesp, taskNames)
|
|
|
|
|
|
|
|
// utils.MenuPrint("Which repository or action do you want to operate:", `
|
|
|
|
// 1. Portainer EE Repository
|
|
|
|
// 2. Portainer CE Repository
|
|
|
|
// 3. Portainer Agent Repository
|
|
|
|
// 4. Others
|
|
|
|
// 5. Quit`)
|
|
|
|
}
|
|
|
|
|
|
|
|
option := utils.SelectMenuItem(printMainMenu)
|
|
|
|
index, err := strconv.Atoi(option)
|
|
|
|
if err != nil {
|
|
|
|
utils.ErrorPrint("please type the option number\n")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if index < 1 || index > len(taskItems) {
|
|
|
|
utils.ErrorPrint(fmt.Sprintf("no such option %s, please select again\n", option))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = taskItems[index-1].Execute()
|
|
|
|
if err != nil {
|
|
|
|
utils.ErrorPrint(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|