From a0e852feaba3898f5ce0baa7fe0a8103afa0f818 Mon Sep 17 00:00:00 2001 From: oscarzhou Date: Mon, 26 Dec 2022 15:28:24 +1300 Subject: [PATCH] subtask: allow to list volumes --- go/tasks/list_dev_tool_cmd.go | 30 +++++++++++++++++ go/tasks/subtasks/list_volume.go | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 go/tasks/list_dev_tool_cmd.go create mode 100644 go/tasks/subtasks/list_volume.go diff --git a/go/tasks/list_dev_tool_cmd.go b/go/tasks/list_dev_tool_cmd.go new file mode 100644 index 0000000..170b3f4 --- /dev/null +++ b/go/tasks/list_dev_tool_cmd.go @@ -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" +} diff --git a/go/tasks/subtasks/list_volume.go b/go/tasks/subtasks/list_volume.go new file mode 100644 index 0000000..887972a --- /dev/null +++ b/go/tasks/subtasks/list_volume.go @@ -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))) +}