Skip to content

Commit

Permalink
DOCS: Add comments to all exported funcs & structs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cian911 committed Dec 18, 2021
1 parent b8fc6b1 commit cbb46ad
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@ var (
ws Watchers
)

// Watchers is a struct that contains a list of watchers.
// in yaml format
type Watchers struct {
Watchers []Watcher `yaml:"watchers,mapstructure"`
}

// Watcher is a struct that contains a path, destination, and file extention and event operation.
// in yaml format
type Watcher struct {
Path string `yaml:"path"`
Destination string `yaml:"destination"`
Ext string `yaml:"ext"`
Operation string `yaml:"operation"`
}

// Watch is the main function that runs the watcher.
func Watch() {
var runCmd = &cobra.Command{
Use: "watch",
Expand Down
3 changes: 3 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
)

// Event is a struct that holds the information for a file event
type Event struct {
File string
Path string
Expand All @@ -14,13 +15,15 @@ type Event struct {
Operation string
}

// Move moves the file to the destination
func (e *Event) Move() error {
log.Printf("Moving e.Path: %s to %s/%s\n", e.Path, e.Destination, e.File)

err := os.Rename(e.Path, fmt.Sprintf("%s/%s", e.Destination, e.File))
return err
}

// IsValidEvent checks if the event operation and file extension is valid
func (e *Event) IsValidEvent(ext string) bool {
if ext == e.Ext && e.Operation == "CREATE" {
return true
Expand Down
3 changes: 3 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"path/filepath"
)

// ExtractFileExt returns the file extension of a file
func ExtractFileExt(path string) string {
return filepath.Ext(path)
}

// ValidatePath checks if a path exists
func ValidatePath(path string) bool {
if path == "" {
return false
Expand All @@ -21,6 +23,7 @@ func ValidatePath(path string) bool {
return true
}

// ValidateFileExt checks if a file extension is valid
func ValidateFileExt(ext string) bool {
if ext == "" || ext[0:1] != "." {
return false
Expand Down
35 changes: 31 additions & 4 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,51 @@ import (
"github.com/fsnotify/fsnotify"
)

// Producer interface for the watcher
// Must implement Register(), Unregister(), and Observe(), and notify()
type Producer interface {
// Register a consumer to the producer
Register(consumer *Consumer)
// Unregister a consumer from the producer
Unregister(consumer *Consumer)
// Notify consumers of an event
notify(path, event string)
// Observe the producer
Observe()
}

// Consumer interface
// Must implement Receive(), and Process() methods
type Consumer interface {
// Receive an event from the producer
Receive(path, event string)
// Process an event
Process(e *event.Event)
}

// PathWatcher is a producer that watches a path for events
type PathWatcher struct {
// List of consumers
Consumers []*Consumer
Watcher fsnotify.Watcher
Path string
// Watcher instance
Watcher fsnotify.Watcher
// Path to watch
Path string
}

// PathConsumer is a consumer that consumes events from a path
// and moves them to a destination
type PathConsumer struct {
Path string
// Path to watch
Path string
// Destination to move files to
Destination string
Ext string
// File extenstion
Ext string
}

// Receive takes a path and an event operation, determines its validity
// and passes it to be processed it if valid
func (pc *PathConsumer) Receive(path, ev string) {
log.Printf("Event Received: %s, Path: %s\n", ev, path)

Expand All @@ -54,6 +75,7 @@ func (pc *PathConsumer) Receive(path, ev string) {
}
}

// Process takes an event and moves it to the destination
func (pc *PathConsumer) Process(e *event.Event) {
err := e.Move()
if err != nil {
Expand All @@ -63,14 +85,17 @@ func (pc *PathConsumer) Process(e *event.Event) {
}
}

// AddPath adds a path to the watcher
func (pw *PathWatcher) AddPath(path string) {
pw.Watcher.Add(path)
}

// Register a consumer to the producer
func (pw *PathWatcher) Register(consumer *Consumer) {
pw.Consumers = append(pw.Consumers, consumer)
}

// Unregister a consumer from the producer
func (pw *PathWatcher) Unregister(consumer *Consumer) {
for i, cons := range pw.Consumers {
if cons == consumer {
Expand All @@ -80,6 +105,7 @@ func (pw *PathWatcher) Unregister(consumer *Consumer) {
}
}

// Observe the producer
func (pw *PathWatcher) Observe() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
Expand Down Expand Up @@ -119,6 +145,7 @@ func (pw *PathWatcher) Observe() {
<-done
}

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

0 comments on commit cbb46ad

Please sign in to comment.