-
Notifications
You must be signed in to change notification settings - Fork 2
/
path.go
81 lines (65 loc) · 1.71 KB
/
path.go
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
package validator
import (
"github.com/cmuench/inotify-proxy/internal/config"
"github.com/cmuench/inotify-proxy/internal/profile"
"path/filepath"
"strings"
)
// IsPathValid checks if a path matches criteria to be included in watch process
func IsPathValid(path string, entryConfig config.WatchEntry) bool {
if !isAllowedDirectory(path) {
return false
}
if len(entryConfig.Extensions) > 0 && !isAllowedFileExtension(path, entryConfig.Extensions) {
return false
}
if entryConfig.Profile == nil {
return true
}
var selectedProfile profile.Profile
switch *entryConfig.Profile {
case "less":
selectedProfile = profile.LESS
case "magento2":
selectedProfile = profile.Magento2
case "magento2-theme":
selectedProfile = profile.Magento2Theme
case "sass":
selectedProfile = profile.SASS
case "vue-storefront":
selectedProfile = profile.VueStorefront
case "javascript":
selectedProfile = profile.Javascript
default:
selectedProfile = profile.Default
}
return isAllowedFileExtension(path, selectedProfile.Extensions)
}
func isAllowedDirectory(path string) bool {
// Exclude some directories by default
excludedDirectories := [...]string{
"node_modules/",
".idea/",
".git/",
".svn/",
}
for _, excludedDirectory := range excludedDirectories {
if strings.Contains(path, excludedDirectory) {
return false
}
}
return true
}
func isAllowedFileExtension(path string, fileExtensions []string) bool {
// if profile contains only one extension definition with "*" then allow every extension.
if len(fileExtensions) == 1 && fileExtensions[0] == "*" {
return true
}
extension := filepath.Ext(path)
for _, a := range fileExtensions {
if a == extension {
return true
}
}
return false
}