-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.go
More file actions
88 lines (78 loc) · 1.85 KB
/
path.go
File metadata and controls
88 lines (78 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"regexp"
)
func GetCurrentPath() string {
path, err := os.Getwd()
if err != nil {
Crashf("Can not get current path due to %v", err.Error())
}
return path
}
func selectDirs(info []fs.FileInfo) []string {
var files []string
for _, file := range info {
if file.IsDir() {
files = append(files, file.Name())
}
}
return files
}
func getNestedFolders(paths []string) []string {
var nestedPath []string
for _, path := range paths {
nestedPath = append(nestedPath, getAllSubFolders(path)...)
}
return nestedPath
}
func fullfilPaths(rootPath string, content []string) []string {
var fulFilledContent []string
for _, contentPath := range content {
fulFilledContent = append(fulFilledContent, filepath.Join(rootPath, contentPath))
}
return fulFilledContent
}
func getAllSubFolders(path string) []string {
fileInfo, _ := ioutil.ReadDir(path)
return append(
getNestedFolders(
fullfilPaths(
path,
selectDirs(fileInfo),
),
),
path,
)
}
func containFileToWatch(root string, paths []fs.FileInfo, matchPattern func(string) bool) bool {
for _, path := range paths {
if matchPattern(filepath.Join(root, path.Name())) {
return true
}
}
return false
}
func GetFoldersToWatch(path string, matchPattern func(string) bool) []string {
allPaths := getAllSubFolders(path)
var selectedPaths []string
for _, folderPath := range allPaths {
dirContent, _ := ioutil.ReadDir(folderPath)
if containFileToWatch(folderPath, dirContent, matchPattern) {
selectedPaths = append(selectedPaths, folderPath)
}
}
return selectedPaths
}
func GetRelativeToRoot(path string) string {
slashExp, _ := regexp.Compile(`\/`)
rootPath := GetCurrentPath()
rootExp, _ := regexp.Compile(slashExp.ReplaceAllLiteralString(
rootPath,
`\/`,
))
return rootExp.ReplaceAllLiteralString(path, ".")
}