Skip to content

Commit

Permalink
Changed filewalk library to "github.com/karrick/godirwalk"
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuench committed Aug 15, 2020
1 parent a9c7030 commit 396c608
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.12

require (
github.com/gookit/color v1.2.7
github.com/karrick/godirwalk v1.16.1
github.com/stretchr/testify v1.3.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gookit/color v1.2.7 h1:4qePMNWZhrmbfYJDix+J4V2l0iVW+6jQGjicELlN14E=
github.com/gookit/color v1.2.7/go.mod h1:AhIE+pS6D4Ql0SQWbBeXPHw7gY0/sjHoA4s/n1KB7xg=
github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw=
github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
64 changes: 48 additions & 16 deletions internal/watcher/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,62 @@ package watcher
import (
"github.com/cmuench/inotify-proxy/internal/profile/validator"
"github.com/gookit/color"
"github.com/karrick/godirwalk"
"os"
"path/filepath"
"time"
)

func visit(osPathname string, de *godirwalk.Dirent) error {
// we only process files
if de.IsDir() {
return nil
}

if !validator.IsPathValid(osPathname, selectedProfile) {
return godirwalk.SkipThis
}

fileChanged := isFileChanged(osPathname)
if fileChanged {
color.Style{color.FgGreen, color.OpBold}.Printf("Changed: %s | %s\n", osPathname, time.Now().Format("2006-01-02T15:04:05"))
}

return nil
}

func Watch(includedDirectories []string, watchFrequenceSeconds int, profile string) {
selectedProfile = profile

i := 0

for {
for _, directoryToWalk := range includedDirectories {
err := filepath.Walk(directoryToWalk, visit)
err := godirwalk.Walk(directoryToWalk, &godirwalk.Options{
Callback: visit,
Unsorted: true,
})

if err != nil {
panic(err)
}
}

time.Sleep(time.Duration(watchFrequenceSeconds) * time.Second)

if i%10 == 0 {
garbageCollection()
color.Info.Printf("Watching %d files ...\n", len(fileMap))
}

i++
}
}

func isFileChanged(path string, fileInfo os.FileInfo) bool {
func isFileChanged(path string) bool {
fileInfo, err := os.Stat(path)

if !validator.IsPathValid(path, selectedProfile) {
if err != nil {
color.Errorf("Cannot stat file %s\n", path)
return false
}

Expand Down Expand Up @@ -64,21 +96,21 @@ func isFileChanged(path string, fileInfo os.FileInfo) bool {
return changed
}

func visit(path string, fileInfo os.FileInfo, err error) error {

if err != nil {
return err
}

if fileInfo.IsDir() {
return nil
func garbageCollection() {
for path, _ := range fileMap {
if !fileExists(path) {
delete(fileMap, path)
color.Style{color.FgGray}.Printf("Deleted: %s\n", path)
}
}
}

fileChanged := isFileChanged(path, fileInfo)
func fileExists(path string) bool {
info, err := os.Stat(path)

if fileChanged {
color.Style{color.FgGreen, color.OpBold}.Printf("Changed: %s | %s\n", path, time.Now().Format("2006-01-02T15:04:05"))
if os.IsNotExist(err) {
return false
}

return nil
return !info.IsDir()
}

0 comments on commit 396c608

Please sign in to comment.