watcher: mv utils to util.go

This commit is contained in:
rasmus 2022-11-11 12:17:17 +02:00
parent bc936b09a5
commit 4573618440
2 changed files with 44 additions and 37 deletions

44
pkg/watcher/util.go Normal file
View File

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

View File

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