Skip to content

Commit 4fca19a

Browse files
authored
Wildcard path support for generic[diff] consumers (#42)
* Wildcard path support for generic[diff] consumers
1 parent 192e8e5 commit 4fca19a

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

cmd/main.go

+27-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ func (c Configuration) logger() (logger zerolog.Logger) {
116116
return logger
117117
}
118118

119-
func (c Configuration) consumers(db *pkg.AgentDB) (consumers pkg.BaseConsumers) {
119+
/*
120+
Initialises all the consumers along with pre-populating genericDiffPaths used by watcher
121+
*/
122+
func (c Configuration) consumers(db *pkg.AgentDB, genericDiffPaths *[]string) (consumers pkg.BaseConsumers) {
120123
fs := afero.NewOsFs()
121124
var existingConsumersFiles = make(map[string]bool)
122125

@@ -160,6 +163,8 @@ func (c Configuration) consumers(db *pkg.AgentDB) (consumers pkg.BaseConsumers)
160163
}
161164
consumers = append(consumers, &pkg.BaseConsumer{AgentDB: db, ParserLoader: state})
162165
existingConsumersFiles[genericDiffFile.File] = true
166+
//this variable is used by watcher to get the complete list of paths to monitor, instead of the list from the config
167+
*genericDiffPaths = append(*genericDiffPaths, genericDiffFile.File)
163168
}
164169
}
165170
}
@@ -203,11 +208,28 @@ func (c Configuration) isFileToBeExcluded(file string, existingConsumersFiles ma
203208
return isFileExcluded || existingConsumersFiles[file]
204209
}
205210

211+
// Gets the full list of paths to monitor
212+
func (c Configuration) getCompleteListOfPaths(pathList []string) []string {
213+
logger := c.logger()
214+
var completePathList []string
215+
for _, path := range pathList {
216+
completePath, err := filepath.Glob(path)
217+
if err != nil {
218+
logger.Error().Err(err).Msgf("Error getting complete list of paths to register: %v", err)
219+
continue
220+
}
221+
completePathList = append(completePathList, completePath...)
222+
}
223+
return completePathList
224+
}
225+
206226
// Gets list of files to be monitored from all files/dirs listed in the config
207227
func (c Configuration) getListOfFiles(fs afero.Fs, pathList []string) []FileInfo {
208228
logger := c.logger()
209229
var filesToMonitor []FileInfo
210-
for _, fullPath := range pathList {
230+
completeListOfPaths := c.getCompleteListOfPaths(pathList)
231+
232+
for _, fullPath := range completeListOfPaths {
211233
fullPath := fullPath
212234
pkgFile := pkg.NewFile(func(file *pkg.File) {
213235
file.Fs, file.Path, file.Logger = fs, fullPath, logger
@@ -353,6 +375,7 @@ func (c Configuration) metrics() (*pkg.Metrics, error) {
353375

354376
func (c Configuration) watcher() (*pkg.Watcher, error) {
355377
logger := c.logger()
378+
var genericDiffPaths []string
356379
logger.Debug().Str("db", c.Database).Msg("opening bolt database")
357380
db, err := bolt.Open(c.Database, 0600, nil)
358381
if err != nil {
@@ -365,15 +388,15 @@ func (c Configuration) watcher() (*pkg.Watcher, error) {
365388
}
366389

367390
database := &pkg.AgentDB{Logger: logger, DB: db}
368-
consumers := c.consumers(database)
391+
consumers := c.consumers(database, &genericDiffPaths)
369392

370393
for _, consumer := range consumers {
371394
if err := consumer.Init(); err != nil {
372395
logger.Fatal().Err(err).Msg("failed to init consumer")
373396
}
374397
}
375398
return pkg.NewWatcher(func(w *pkg.Watcher) {
376-
w.Logger, w.Consumers, w.FIM, w.Database, w.Key, w.Excludes, w.GenericDiff = logger, consumers.Consumers(), fim, database, c.key, c.Consumers.Excludes, c.Consumers.GenericDiff
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
377400
}), nil
378401
}
379402

0 commit comments

Comments
 (0)