Skip to content

Commit

Permalink
WIP: Begin to add capability to determine MKDIR events and scan folde…
Browse files Browse the repository at this point in the history
…r contents.
  • Loading branch information
Cian911 committed Dec 21, 2021
1 parent 4f5e0c7 commit 1bac31f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func registerMultiConsumers() {
pw.Register(&pc)
}

log.Println("Observing")
pw.Observe()
}

Expand Down
10 changes: 10 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"log"
"os"

"github.com/cian911/switchboard/utils"
)

// Event is a struct that holds the information for a file event
Expand Down Expand Up @@ -36,3 +38,11 @@ func (e *Event) IsValidEvent(ext string) bool {

return false
}

func (e *Event) IsNewDirEvent() bool {
if e.Ext == "" && utils.ValidatePath(e.Path) {
return true
}

return false
}
13 changes: 13 additions & 0 deletions event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ func TestEvent(t *testing.T) {
log.Fatal("event.Move() should have thrown error but didn't.")
}
})

t.Run("It determines if the event is a new dir", func(t *testing.T) {
event := eventSetup(t)
event.File = "input"
event.Ext = ""

want := true
got := event.IsNewDirEvent()

if want != got {
t.Errorf("Wanted new dir event but didn't get it: want=%t, got=%t", want, got)
}
})
}

func eventSetup(t *testing.T) *Event {
Expand Down
15 changes: 15 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"io/ioutil"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -31,3 +32,17 @@ func ValidateFileExt(ext string) bool {

return true
}

func ScanFilesInDir(path string) (map[string]string, error) {
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}

fileList := map[string]string{}
/* for _, file := range files { */
/* */
/* } */

return fileList, nil
}
9 changes: 8 additions & 1 deletion watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ func (pc *PathConsumer) Receive(path, ev string) {

if e.IsValidEvent(pc.Ext) {
log.Println("Event is valid")
pc.Process(e)

if e.IsNewDirEvent() {
log.Println("Event is a new dir")
// Recursively scan dir for items with our ext
// Then add all recursive dirs as paths
} else {
pc.Process(e)
}
}
}

Expand Down

0 comments on commit 1bac31f

Please sign in to comment.