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))

	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() {
			if utils.MatchPathLength(task.Config.VolumePath, path, 1) {
				volumeList = append(volumeList, d.Name())
			}
		}

		return nil
	})

	utils.SuccessPrint(strings.Join(volumeList, "\n"))

	return nil
}

func (task *ListVolumeSubTask) String() string {
	return "List Volumes"
}
