Skip to content

Commit

Permalink
FEAT: Add support for regex command in single & multi consumer modes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cian911 committed Mar 6, 2022
1 parent e8ed493 commit 6026313
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
15 changes: 13 additions & 2 deletions cli/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ type Watcher struct {
// Ext is the file extention you want to watch for
Ext string `yaml:"ext"`
// Operation is the event operation you want to watch for
// CREATE, MODIFY, REMOVE, CHMOD, WRITE itc.
// CREATE, MODIFY, REMOVE, CHMOD, WRITE etc.
Operation string `yaml:"operation"`
// Pattern is the regex pattern to match against events
Pattern string `yaml:"pattern"`
}

// Watch is the main function that runs the watcher.
Expand Down Expand Up @@ -116,7 +118,7 @@ func validateFlags() {
log.Fatalf("Destination cannot be found. Does the path exist?: %s", viper.GetString("destination"))
}

if !utils.ValidateFileExt(viper.GetString("ext")) {
if !utils.ValidateFileExt(viper.GetString("ext")) && len(viper.GetString("regex-pattern")) == 0 {
log.Fatalf("Ext is not valid. A file extention should contain a '.': %s", viper.GetString("ext"))
}

Expand All @@ -127,6 +129,8 @@ func validateFlags() {
if regexErr != nil {
log.Fatalf("Regex pattern is not valid. Please check it again: %v", regexErr)
}
} else {
regexPattern, _ = utils.ValidateRegexPattern("")
}
}

Expand All @@ -146,10 +150,17 @@ func registerMultiConsumers() {
pw.(*watcher.PathWatcher).AddPath(v.Path)
}

regexPattern, regexErr = utils.ValidateRegexPattern(v.Pattern)

if regexErr != nil {
log.Fatalf("Regex pattern is not valid. Please check it again: %v", regexErr)
}

var pc watcher.Consumer = &watcher.PathConsumer{
Path: v.Path,
Destination: v.Destination,
Ext: v.Ext,
Pattern: *regexPattern,
}

pw.Register(&pc)
Expand Down
4 changes: 2 additions & 2 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/cian911/switchboard/utils"
)

var ValidOperations = map[string]bool{
var validOperations = map[string]bool{
"CREATE": true,
"WRITE": true,
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func (e *Event) Move(path, file string) error {

// IsValidEvent checks if the event operation and file extension is valid
func (e *Event) IsValidEvent(ext string) bool {
if ext == e.Ext && ValidOperations[e.Operation] {
if ext == e.Ext && validOperations[e.Operation] {
return true
}

Expand Down
2 changes: 1 addition & 1 deletion watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (pc *PathConsumer) Receive(path, ev string) {

if e.IsNewDirEvent() {
pc.ProcessDirEvent(e)
} else if &pc.Pattern != nil {
} else if &pc.Pattern != nil && len(pc.Pattern.String()) != 0 {
// Check if event matches regex pattern
p := fmt.Sprintf(`%s/%s`, e.Path, e.File)
match := pc.Pattern.Match([]byte(p))
Expand Down
11 changes: 6 additions & 5 deletions yaml/config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
pollingInterval: 120
pollingInterval: 10
watchers:
- path: "/Users/cian.gallagher/input"
destination: "/Users/cian.gallagher/output"
- path: "/home/cian/Downloads"
destination: "/home/cian/Documents"
ext: ".txt"
- path: "/Users/cian.gallagher/input"
destination: "/Users/cian.gallagher/output"
pattern: "(?i)(financial-report-[a-z]+-[0-9]+.txt)"
- path: "/home/cian/Downloads"
destination: "/home/cian/Videos"
ext: ".mp4"

0 comments on commit 6026313

Please sign in to comment.