-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
126 lines (105 loc) · 2.47 KB
/
filter.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package surveillance
import (
"path/filepath"
"strings"
"github.com/AlexanderBrese/Gomon/pkg/configuration"
"github.com/AlexanderBrese/Gomon/pkg/utils"
)
type Filter struct {
config *configuration.Configuration
}
func NewFilter(cfg *configuration.Configuration) *Filter {
return &Filter{
config: cfg,
}
}
func (f *Filter) IsExcludedDir(path string) (bool, error) {
isIgnored, err := f.IsIgnoredDir(path)
if err != nil {
return false, err
}
isBuildDir, err := f.IsBuildDir(path)
if err != nil {
return false, err
}
isLogDir, err := f.IsLogDir(path)
if err != nil {
return false, err
}
isExcluded := isBuildDir || isLogDir || f.IsHiddenDir(path) || isIgnored
return isExcluded, nil
}
func (f *Filter) IsIncludedDir(dir string) (bool, error) {
incDirs := f.config.IncludeDirs
if len(incDirs) == 0 {
return true, nil
}
for _, d := range incDirs {
incDir, err := utils.CurrentAbsolutePath(d)
if err != nil {
return false, err
}
if dir == incDir || strings.HasPrefix(dir, incDir) {
return true, nil
}
}
return false, nil
}
func (f *Filter) IsBuildDir(path string) (bool, error) {
buildDir, err := f.config.BuildDir()
if err != nil {
return false, err
}
return path == buildDir, nil
}
func (f *Filter) IsLogDir(path string) (bool, error) {
logDir, err := f.config.LogDir()
if err != nil {
return false, err
}
return path == logDir, nil
}
func (f *Filter) IsHiddenDir(path string) bool {
return len(path) > 1 && strings.HasPrefix(filepath.Base(path), ".")
}
func (f *Filter) IsIgnoredDir(path string) (bool, error) {
relPath, err := utils.RelPath(f.config.Root, path)
if err != nil {
return false, err
}
rootParent := strings.Split(relPath, "/")[0]
for _, ignoredDir := range f.config.ExcludeDirs {
if rootParent == ignoredDir {
return true, nil
}
}
return false, nil
}
func (f *Filter) IsExcludedFile(path string) (bool, error) {
isIgnored, err := f.IsIgnoredFile(path)
if err != nil {
return false, err
}
return isIgnored || f.IsIgnoredExt(path), nil
}
func (f *Filter) IsIgnoredFile(path string) (bool, error) {
for _, f := range f.config.IgnoreFiles {
absIgnoredFile, err := utils.CurrentAbsolutePath(f)
if err != nil {
return false, err
}
if path == absIgnoredFile {
return true, nil
}
}
return false, nil
}
func (f *Filter) IsIgnoredExt(path string) bool {
ext := filepath.Ext(path)
for _, e := range f.config.IncludeExts {
if ext == "."+strings.TrimSpace(e) {
return false
}
}
return true
}