portainer-devtool/go/tasks/subtasks/list_volume.go

59 lines
1.1 KiB
Go
Raw Normal View History

2022-12-26 13:28:24 +11:00
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 {
2022-12-26 18:55:06 +11:00
return "List Volumes"
2022-12-26 13:28:24 +11:00
}
func lenPath(path string) int {
return len(strings.Split(path, string(filepath.Separator)))
}