Skip to content

Commit

Permalink
add GetFilePathsUnderDirectoryWithIgnore function (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 24, 2023
1 parent aa593c4 commit c9ca3b4
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions internal/file/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package file

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/JunNishimura/Goit/internal/store"
)

var (
Expand Down Expand Up @@ -48,3 +51,35 @@ func GetFilePathsUnderDirectory(path string) ([]string, error) {

return filePaths, nil
}

func GetFilePathsUnderDirectoryWithIgnore(path string, ignore *store.Ignore) ([]string, error) {
files, err := os.ReadDir(path)
if err != nil {
return nil, err
}

var filePaths []string
for _, file := range files {
var filePath string
if path == "" || path == "." {
filePath = file.Name()
} else {
filePath = fmt.Sprintf("%s/%s", path, file.Name())
}
if ignore.IsIncluded(filePath) {
continue
}

if file.IsDir() {
gotFilePaths, err := GetFilePathsUnderDirectoryWithIgnore(filePath, ignore)
if err != nil {
return nil, err
}
filePaths = append(filePaths, gotFilePaths...)
} else {
filePaths = append(filePaths, filePath)
}
}

return filePaths, nil
}

0 comments on commit c9ca3b4

Please sign in to comment.