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 -}