From 45736184408a29659f0f213db8f0e47d1a66efea Mon Sep 17 00:00:00 2001 From: rasmus Date: Fri, 11 Nov 2022 12:17:17 +0200 Subject: [PATCH] watcher: mv utils to util.go --- pkg/watcher/util.go | 44 ++++++++++++++++++++++++++++++++++++++++++ pkg/watcher/watcher.go | 37 ----------------------------------- 2 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 pkg/watcher/util.go diff --git a/pkg/watcher/util.go b/pkg/watcher/util.go new file mode 100644 index 0000000..7d80a83 --- /dev/null +++ b/pkg/watcher/util.go @@ -0,0 +1,44 @@ +package watcher + +import ( + "os" + "path/filepath" + "strings" + + "github.com/fsnotify/fsnotify" +) + +func simulateInitialCreates(dirName string, eventChan chan<- fsnotify.Event) error { + dir, err := os.ReadDir(dirName) + if err != nil { + return err + } + + for _, file := range dir { + eventChan <- fsnotify.Event{ + Name: filepath.Join(dirName, file.Name()), + Op: fsnotify.Create, + } + } + + return nil +} + +func sliceToMap[T comparable](sl []T) map[T]interface{} { + m := make(map[T]interface{}) + + for _, k := range sl { + m[k] = nil + } + + return m +} + +func hasSlicePrefix(s string, sl []string) bool { + for _, prefix := range sl { + if strings.HasPrefix(s, prefix) { + return true + } + } + return false +} diff --git a/pkg/watcher/watcher.go b/pkg/watcher/watcher.go index a98ebf2..e5ae737 100644 --- a/pkg/watcher/watcher.go +++ b/pkg/watcher/watcher.go @@ -3,9 +3,7 @@ package watcher import ( "fmt" "log" - "os" "path/filepath" - "strings" "sync" "git.k-space.ee/k-space/logmower-shipper/pkg/file" @@ -150,38 +148,3 @@ var App = &cli.App{ return ctx.Err() }, } - -func simulateInitialCreates(dirName string, eventChan chan<- fsnotify.Event) error { - dir, err := os.ReadDir(dirName) - if err != nil { - return err - } - - for _, file := range dir { - eventChan <- fsnotify.Event{ - Name: filepath.Join(dirName, file.Name()), - Op: fsnotify.Create, - } - } - - return nil -} - -func sliceToMap[T comparable](sl []T) map[T]interface{} { - m := make(map[T]interface{}) - - for _, k := range sl { - m[k] = nil - } - - return m -} - -func hasSlicePrefix(s string, sl []string) bool { - for _, prefix := range sl { - if strings.HasPrefix(s, prefix) { - return true - } - } - return false -}