Skip to content

Commit

Permalink
UTILS: CLean & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Cian911 committed Apr 23, 2022
1 parent 08f7b43 commit 5bf54bf
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test-utils:
test-cmd:
@gotest -v ./cmd

test-all: test-watcher test-event test-utils test-cmd
test-all: test-all test-watcher test-watcher-observe test-event test-utils test-cmd

lint-watcher:
@golint ./watcher
Expand Down
10 changes: 9 additions & 1 deletion cli/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"regexp"
"runtime"

"github.com/cian911/switchboard/utils"
"github.com/cian911/switchboard/watcher"
Expand Down Expand Up @@ -73,7 +74,14 @@ func initCmd(runCmd cobra.Command) {
runCmd.PersistentFlags().StringP("path", "p", "", "Path you want to watch.")
runCmd.PersistentFlags().StringP("destination", "d", "", "Path you want files to be relocated.")
runCmd.PersistentFlags().StringP("ext", "e", "", "File type you want to watch for.")
runCmd.PersistentFlags().IntP("poll", "", 60, "Specify a polling time in seconds.")

if runtime.GOOS == "linux" {
log.Println("Setting polling interval for linux")
// Set the default poll interval to lower value on linux envs
runCmd.PersistentFlags().IntP("poll", "", 1, "Specify a polling time in seconds.")
} else {
runCmd.PersistentFlags().IntP("poll", "", 60, "Specify a polling time in seconds.")
}
runCmd.PersistentFlags().StringVar(&configFile, "config", "", "Pass an optional config file containing multiple paths to watch.")
runCmd.PersistentFlags().StringP("regex-pattern", "r", "", "Pass a regex pattern to watch for any files mathcing this pattern.")

Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ require (
)

require (
github.com/fatih/color v1.13.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/rakyll/gotest v0.0.6 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
Expand All @@ -18,6 +21,15 @@ github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
Expand All @@ -29,6 +41,8 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rakyll/gotest v0.0.6 h1:hBTqkO3jiuwYW/M9gL4bu0oTYcm8J6knQAAPUsJsz1I=
github.com/rakyll/gotest v0.0.6/go.mod h1:SkoesdNCWmiD4R2dljIUcfSnNdVZ12y8qK4ojDkc2Sc=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY=
Expand Down Expand Up @@ -61,10 +75,17 @@ golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
Expand Down
4 changes: 2 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func ExtractFileExt(path string) string {
return filepath.Ext(strings.Trim(path, "'"))
}

// Extract file path without the extension
// ExtractPathWithoutExt file path without the extension
func ExtractPathWithoutExt(path string) string {
return path[:len(path)-len(filepath.Ext(path))]
}

// Compare two filepaths and return a bool
// CompareFilePaths two filepaths and return a bool
func CompareFilePaths(p1, p2 string) bool {
if ExtractPathWithoutExt(p1) == p2 {
return true
Expand Down
6 changes: 5 additions & 1 deletion watcher/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
HelperPattern string
)

// TestEventSetup sets up a new event for testing purposes
func TestEventSetup(t *testing.T) *event.Event {
path := t.TempDir()
_, err := ioutil.TempFile(path, HelperFile)
Expand All @@ -36,8 +37,9 @@ func TestEventSetup(t *testing.T) *event.Event {
}
}

// SimulateMultipleEvents takes a list of operations as args
// TestSimulateMultipleEvents takes a list of operations as args
// ["CREATE", "WRITE", "CLOSEWRITE"]
// and returns them as a list of events
func TestSimulateMultipleEvents(operationList []string, t *testing.T) []event.Event {
eventList := []event.Event{}

Expand All @@ -49,6 +51,7 @@ func TestSimulateMultipleEvents(operationList []string, t *testing.T) []event.Ev
return eventList
}

// TestProducerConsumer returns a Producer/Consumer struct for testing
func TestProducerConsumer() (Producer, Consumer) {
var pw Producer = &PathWatcher{
Path: HelperPath,
Expand All @@ -67,6 +70,7 @@ func TestProducerConsumer() (Producer, Consumer) {
return pw, pc
}

// TestPathWatcher returns a test watcher
func TestPathWatcher() *PathWatcher {
return &PathWatcher{
Queue: NewQueue(),
Expand Down
49 changes: 30 additions & 19 deletions watcher/watcher_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package watcher

import (
"fmt"
"log"
"os"
"path/filepath"
Expand All @@ -15,11 +16,10 @@ import (
"github.com/fsnotify/fsnotify"
)

// TODO: Change this from an int to a bool
// Monitor for IN_CLOSE_WRITE events on these file exts
// A create event should immediatly follow
var specialWatchedFileExts = map[string]int{
".part": 1,
var specialWatchedFileExts = map[string]bool{
".part": true,
}

// Producer interface for the watcher
Expand Down Expand Up @@ -84,16 +84,20 @@ func (pc *PathConsumer) Receive(path, ev string) {
}

if !e.IsNewDirEvent() && e.Ext != pc.Ext && filepath.Dir(path) != pc.Path {
log.Printf("Not processing event - %v - %v\n\n", e, pc)
log.Printf("Not processing event - %v - %v", e, pc)
// Do not process event for consumers not watching file
return
}

if e.IsNewDirEvent() {
log.Println("Processing dir event")
pc.ProcessDirEvent(e)
} else if &pc.Pattern != nil && len(pc.Pattern.String()) != 0 {
match := validateRegexEventMatch(pc, e)

if match {
pc.Process(e)
}
} else if e.IsValidEvent(pc.Ext) {
log.Printf("Process valid event - %v\n\n", pc)
pc.Process(e)
}
}
Expand Down Expand Up @@ -150,9 +154,7 @@ func (pw *PathWatcher) Unregister(consumer *Consumer) {

// Observe the producer
func (pw *PathWatcher) Observe(pollInterval int) {
// TODO: Lower default pollInterval for linux machines
// We only want to poll on linux in the case of valid CREATE events
// that are not follwed by a IN_CLOSE_WRITE event
pw.Queue = NewQueue()
pw.Poll(pollInterval)

watcher, err := fsnotify.NewWatcher()
Expand Down Expand Up @@ -183,23 +185,18 @@ func (pw *PathWatcher) Observe(pollInterval int) {
for {
select {
case event := <-watcher.Events:

// Check for CREATE event
// -> If no event follows this for x seconds & is not in special file list
// -> Process event
// Check for IN_CLOSE_WRITE event
// -> If no event follows this for x seconds & is not in special file list
// -> Process event

if event.Op.String() == "CREATE" && utils.IsDir(event.Name) {
watcher.Add(event.Name)
} else if event.Op.String() == "CLOSEWRITE" {
ev := newEvent(event.Name, event.Op.String())

if specialWatchedFileExts[ev.Ext] == 1 {
log.Println("Adding event to queue.")
if specialWatchedFileExts[ev.Ext] {
// If the file is in the special file list
// add it to the queue and wait for it to be finished
log.Println("Adding CLOSEWRITE event to queue.")
pw.Queue.Add(*ev)
} else {
// Otherwise process the event immediatly
log.Printf("Notifying consumers: %v\n", ev)
pw.Notify(ev.Path, ev.Operation)
}
Expand All @@ -217,6 +214,20 @@ func (pw *PathWatcher) Observe(pollInterval int) {
<-done
}

func validateRegexEventMatch(pc *PathConsumer, event *event.Event) bool {
p := fmt.Sprintf(`%s/%s`, event.Path, event.File)
match := pc.Pattern.Match([]byte(p))

if match {
log.Println("Regex Pattern matched")
return true
}

log.Println("Regex did not match")
return false

}

// Notify consumers of an event
func (pw *PathWatcher) Notify(path, event string) {
for _, cons := range pw.Consumers {
Expand Down

0 comments on commit 5bf54bf

Please sign in to comment.