diff --git a/go/tasks/subtasks/list_volume.go b/go/tasks/subtasks/list_volume.go index d0854e2..5eb6cf4 100644 --- a/go/tasks/subtasks/list_volume.go +++ b/go/tasks/subtasks/list_volume.go @@ -22,8 +22,6 @@ func NewListVolumeSubTask(cfg *configs.Config) *ListVolumeSubTask { 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 { @@ -35,8 +33,7 @@ func (task *ListVolumeSubTask) Execute() error { } if d.IsDir() { - dirLength := lenPath(path) - if volumeLength+1 == dirLength { + if utils.MatchPathLength(task.Config.VolumePath, path, 1) { volumeList = append(volumeList, d.Name()) } } @@ -52,7 +49,3 @@ func (task *ListVolumeSubTask) Execute() error { func (task *ListVolumeSubTask) String() string { return "List Volumes" } - -func lenPath(path string) int { - return len(strings.Split(path, string(filepath.Separator))) -} diff --git a/go/utils/path.go b/go/utils/path.go new file mode 100644 index 0000000..6f73b19 --- /dev/null +++ b/go/utils/path.go @@ -0,0 +1,18 @@ +package utils + +import ( + "path/filepath" + "strings" +) + +// MatchPathLength matches the length of target path separated by path separator +// to the length of base path separated by path separator plus offset +func MatchPathLength(basePath, targetPath string, offset int) bool { + basePathLength := len(strings.Split(basePath, string(filepath.Separator))) + targetPathLength := len(strings.Split(targetPath, string(filepath.Separator))) + if basePathLength+offset == targetPathLength { + return true + } + + return false +}