subtask: allow to list volumes
This commit is contained in:
parent
747c774c98
commit
a0e852feab
30
go/tasks/list_dev_tool_cmd.go
Normal file
30
go/tasks/list_dev_tool_cmd.go
Normal 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"
|
||||
}
|
58
go/tasks/subtasks/list_volume.go
Normal file
58
go/tasks/subtasks/list_volume.go
Normal 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)))
|
||||
}
|
Loading…
Reference in New Issue
Block a user