subtask: allow to list volumes

This commit is contained in:
oscarzhou 2022-12-26 15:28:24 +13:00
parent 747c774c98
commit a0e852feab
2 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package tasks
import (
"ocl/portainer-devtool/configs"
"ocl/portainer-devtool/tasks/subtasks"
)
type ListDevToolCommandTask struct {
Config *configs.Config
}
func NewListDevToolCommandTask(cfg *configs.Config) *ListDevToolCommandTask {
return &ListDevToolCommandTask{
Config: cfg,
}
}
func (task *ListDevToolCommandTask) Execute() error {
subTaskItems := []Tasker{
subtasks.NewListVolumeSubTask(task.Config),
}
ListCommandMenu(subTaskItems, "Which management commands do you want to choose:")
return nil
}
func (task *ListDevToolCommandTask) String() string {
return "List Dev Tool Commands"
}

View File

@ -0,0 +1,58 @@
package subtasks
import (
"fmt"
"io/fs"
"ocl/portainer-devtool/configs"
"ocl/portainer-devtool/utils"
"path/filepath"
"strings"
)
type ListVolumeSubTask struct {
Config *configs.Config
}
func NewListVolumeSubTask(cfg *configs.Config) *ListVolumeSubTask {
return &ListVolumeSubTask{
Config: cfg,
}
}
func (task *ListVolumeSubTask) Execute() error {
utils.HighlightPrint(fmt.Sprintf("Volume path: %s", task.Config.VolumePath))
volumeLength := lenPath(task.Config.VolumePath)
volumeList := []string{" "}
filepath.WalkDir(task.Config.VolumePath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == task.Config.VolumePath {
return nil
}
if d.IsDir() {
dirLength := lenPath(path)
if volumeLength+1 == dirLength {
volumeList = append(volumeList, d.Name())
}
}
return nil
})
utils.SuccessPrint(strings.Join(volumeList, "\n"))
return nil
}
func (task *ListVolumeSubTask) String() string {
return "List Volume"
}
func lenPath(path string) int {
return len(strings.Split(path, string(filepath.Separator)))
}