8
8
"os"
9
9
"os/signal"
10
10
"path/filepath"
11
+ "regexp"
11
12
"strings"
12
13
"sync"
13
14
"time"
@@ -122,12 +123,13 @@ Initialises all the consumers along with pre-populating genericDiffPaths used by
122
123
func (c Configuration ) consumers (db * pkg.AgentDB , genericDiffPaths * []string ) (consumers pkg.BaseConsumers ) {
123
124
fs := afero .NewOsFs ()
124
125
var existingConsumersFiles = make (map [string ]bool )
126
+ listOfRegexpsExcludes := c .compileRegex (c .Consumers .Excludes )
125
127
126
128
if c .Consumers .Root != "" {
127
129
fs = afero .NewBasePathFs (fs , c .Consumers .Root )
128
130
}
129
131
if c .Consumers .Access != "" {
130
- if ! c .isFileToBeExcluded (c .Consumers .Access , existingConsumersFiles ) {
132
+ if ! c .isFileToBeExcluded (c .Consumers .Access , existingConsumersFiles , listOfRegexpsExcludes ) {
131
133
state := & pkg.AccessState {
132
134
AccessListener : pkg .NewAccessListener (
133
135
pkg .AccessFileOpt (fs , c .Consumers .Access , c .logger ()),
@@ -138,7 +140,8 @@ func (c Configuration) consumers(db *pkg.AgentDB, genericDiffPaths *[]string) (c
138
140
}
139
141
}
140
142
if c .Consumers .Users .Shadow != "" && c .Consumers .Users .Passwd != "" {
141
- if ! c .isFileToBeExcluded (c .Consumers .Users .Shadow , existingConsumersFiles ) || ! c .isFileToBeExcluded (c .Consumers .Users .Passwd , existingConsumersFiles ) {
143
+ if ! c .isFileToBeExcluded (c .Consumers .Users .Shadow , existingConsumersFiles , listOfRegexpsExcludes ) ||
144
+ ! c .isFileToBeExcluded (c .Consumers .Users .Passwd , existingConsumersFiles , listOfRegexpsExcludes ) {
142
145
state := & pkg.UsersState {
143
146
UsersListener : pkg .NewUsersListener (func (l * pkg.UsersListener ) {
144
147
l .Passwd = c .Consumers .Users .Passwd
@@ -155,7 +158,7 @@ func (c Configuration) consumers(db *pkg.AgentDB, genericDiffPaths *[]string) (c
155
158
//get list of files to watch
156
159
genericDiffFiles := c .getListOfFiles (fs , c .Consumers .GenericDiff )
157
160
for _ , genericDiffFile := range genericDiffFiles {
158
- if ! c .isFileToBeExcluded (genericDiffFile .File , existingConsumersFiles ) {
161
+ if ! c .isFileToBeExcluded (genericDiffFile .File , existingConsumersFiles , listOfRegexpsExcludes ) {
159
162
state := & pkg.GenericDiffState {
160
163
GenericDiffListener : pkg .NewGenericDiffListener (
161
164
pkg .GenericDiffFileOpt (fs , genericDiffFile .File , c .logger ()),
@@ -171,7 +174,7 @@ func (c Configuration) consumers(db *pkg.AgentDB, genericDiffPaths *[]string) (c
171
174
if len (c .Consumers .Generic ) > 0 {
172
175
genericFiles := c .getListOfFiles (fs , c .Consumers .Generic )
173
176
for _ , genericFile := range genericFiles {
174
- if ! c .isFileToBeExcluded (genericFile .File , existingConsumersFiles ) {
177
+ if ! c .isFileToBeExcluded (genericFile .File , existingConsumersFiles , listOfRegexpsExcludes ) {
175
178
genericFile := genericFile
176
179
state := & pkg.GenericState {
177
180
GenericListener : pkg .NewGenericListener (func (l * pkg.GenericListener ) {
@@ -189,22 +192,36 @@ func (c Configuration) consumers(db *pkg.AgentDB, genericDiffPaths *[]string) (c
189
192
return consumers
190
193
}
191
194
195
+ // Gets list of regexp objects from regexp paths
196
+ func (c Configuration ) compileRegex (listofPaths []string ) []* regexp.Regexp {
197
+ logger := c .logger ()
198
+ var regexpObjects []* regexp.Regexp
199
+ for _ , path := range listofPaths {
200
+ reg , err := regexp .Compile (path )
201
+ if err != nil {
202
+ logger .Error ().Err (err ).Msgf ("Error while compiling regex: %v" , err )
203
+ continue
204
+ }
205
+ regexpObjects = append (regexpObjects , reg )
206
+ }
207
+ return regexpObjects
208
+ }
209
+
192
210
/* Checks if file belongs to exclusion list or is already assigned to a consumer and excludes it accordingly
193
211
true: if file needs to be excluded
194
212
false: otherwise
195
213
*/
196
- func (c Configuration ) isFileToBeExcluded (file string , existingConsumersFiles map [string ]bool ) bool {
214
+ func (c Configuration ) isFileToBeExcluded (file string , existingConsumersFiles map [string ]bool , listOfRegexpsExcludes [] * regexp. Regexp ) bool {
197
215
logger := c .logger ()
198
216
isFileExcluded := false
199
-
200
- for _ , excludeFile := range c . Consumers . Excludes {
201
- if strings . HasPrefix ( file , excludeFile ) {
217
+ for _ , excludeRegexp := range listOfRegexpsExcludes {
218
+ matches := excludeRegexp . MatchString ( file )
219
+ if matches {
202
220
logger .Debug ().Msgf ("File belongs to exclusion list, excluding from monitoring: %v" , file )
203
221
isFileExcluded = true
204
222
break
205
223
}
206
224
}
207
-
208
225
return isFileExcluded || existingConsumersFiles [file ]
209
226
}
210
227
@@ -396,7 +413,7 @@ func (c Configuration) watcher() (*pkg.Watcher, error) {
396
413
}
397
414
}
398
415
return pkg .NewWatcher (func (w * pkg.Watcher ) {
399
- w .Logger , w .Consumers , w .FIM , w .Database , w .Key , w .Excludes , w .GenericDiff = logger , consumers .Consumers (), fim , database , c .key , c .Consumers .Excludes , genericDiffPaths
416
+ w .Logger , w .Consumers , w .FIM , w .Database , w .Key , w .Excludes , w .GenericDiff = logger , consumers .Consumers (), fim , database , c .key , c .compileRegex ( c . Consumers .Excludes ) , genericDiffPaths
400
417
}), nil
401
418
}
402
419
0 commit comments