From cbb46adf5dbc3a13947ff469eae956416cb8be5d Mon Sep 17 00:00:00 2001 From: Cian Gallagher Date: Sat, 18 Dec 2021 14:05:18 +0000 Subject: [PATCH] DOCS: Add comments to all exported funcs & structs. --- cmd/watch.go | 5 +++++ event/event.go | 3 +++ utils/utils.go | 3 +++ watcher/watcher.go | 35 +++++++++++++++++++++++++++++++---- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/cmd/watch.go b/cmd/watch.go index 1512de0..e80a181 100644 --- a/cmd/watch.go +++ b/cmd/watch.go @@ -21,10 +21,14 @@ 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"` @@ -32,6 +36,7 @@ type Watcher struct { Operation string `yaml:"operation"` } +// Watch is the main function that runs the watcher. func Watch() { var runCmd = &cobra.Command{ Use: "watch", diff --git a/event/event.go b/event/event.go index e5b0427..f32b7e1 100644 --- a/event/event.go +++ b/event/event.go @@ -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 @@ -14,6 +15,7 @@ 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) @@ -21,6 +23,7 @@ func (e *Event) Move() error { 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 diff --git a/utils/utils.go b/utils/utils.go index 0210d5f..c7f4d99 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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 @@ -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 diff --git a/watcher/watcher.go b/watcher/watcher.go index 1ab9a28..525e59c 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -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) @@ -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 { @@ -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 { @@ -80,6 +105,7 @@ func (pw *PathWatcher) Unregister(consumer *Consumer) { } } +// Observe the producer func (pw *PathWatcher) Observe() { watcher, err := fsnotify.NewWatcher() if err != nil { @@ -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)