Bump reva
This commit is contained in:
+4
-15
@@ -1,28 +1,17 @@
|
||||
package inotifywaitgo
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// Function to checkDependencies if inotifywait is installed
|
||||
// CheckDependencies verifies if inotifywait is installed.
|
||||
func checkDependencies() (bool, error) {
|
||||
cmd := exec.Command("bash", "-c", "which inotifywait")
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
path, err := exec.LookPath("inotifywait")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// Read the output of inotifywait and split it into lines
|
||||
scanner := bufio.NewScanner(stdout)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if line != "" {
|
||||
return true, nil
|
||||
}
|
||||
if path != "" {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
+5
-5
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GenerateBashCommands(s *Settings) ([]string, error) {
|
||||
func GenerateShellCommands(s *Settings) ([]string, error) {
|
||||
if s.Options == nil {
|
||||
return nil, errors.New(OPT_NIL)
|
||||
}
|
||||
@@ -34,27 +34,27 @@ func GenerateBashCommands(s *Settings) ([]string, error) {
|
||||
if !Contains(VALID_EVENTS, int(event)) {
|
||||
return nil, errors.New(INVALID_EVENT)
|
||||
}
|
||||
baseCmd = append(baseCmd, "-e ", EVENT_MAP[int(event)])
|
||||
baseCmd = append(baseCmd, "-e", EVENT_MAP[int(event)])
|
||||
}
|
||||
}
|
||||
|
||||
baseCmd = append(baseCmd, s.Dir)
|
||||
|
||||
// remove spaces on all elements
|
||||
// Trim spaces on all elements
|
||||
var outCmd []string
|
||||
for _, v := range baseCmd {
|
||||
outCmd = append(outCmd, strings.TrimSpace(v))
|
||||
}
|
||||
|
||||
if s.Verbose {
|
||||
fmt.Println("baseCmd:", outCmd)
|
||||
fmt.Println("Generated command:", strings.Join(outCmd, " "))
|
||||
}
|
||||
|
||||
return outCmd, nil
|
||||
}
|
||||
|
||||
// Contains checks if a slice contains an item
|
||||
func Contains[T string | int](slice []T, item T) bool {
|
||||
func Contains[T comparable](slice []T, item T) bool {
|
||||
for _, v := range slice {
|
||||
if v == item {
|
||||
return true
|
||||
|
||||
+2
-2
@@ -3,6 +3,6 @@ package inotifywaitgo
|
||||
import "os/exec"
|
||||
|
||||
func killOthers() error {
|
||||
cmd := exec.Command("bash", "-c", "pkill inotifywait").Run()
|
||||
return cmd
|
||||
cmd := exec.Command("pkill", "inotifywait")
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
+49
-33
@@ -10,18 +10,16 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Function that starts watching a path for new files and returns the file name (abspath) when a new file is finished writing
|
||||
// WatchPath starts watching a path for new files and returns the file name (abspath) when a new file is finished writing.
|
||||
func WatchPath(s *Settings) {
|
||||
// Check if inotifywait is installed
|
||||
ok, err := checkDependencies()
|
||||
if !ok || err != nil {
|
||||
if ok, err := checkDependencies(); !ok || err != nil {
|
||||
s.ErrorChan <- fmt.Errorf(NOT_INSTALLED)
|
||||
return
|
||||
}
|
||||
|
||||
// check if dir exists
|
||||
_, err = os.Stat(s.Dir)
|
||||
if os.IsNotExist(err) {
|
||||
// Check if the directory exists
|
||||
if _, err := os.Stat(s.Dir); os.IsNotExist(err) {
|
||||
s.ErrorChan <- fmt.Errorf(DIR_NOT_EXISTS)
|
||||
return
|
||||
}
|
||||
@@ -31,8 +29,8 @@ func WatchPath(s *Settings) {
|
||||
killOthers()
|
||||
}
|
||||
|
||||
// Generate bash command
|
||||
cmdString, err := GenerateBashCommands(s)
|
||||
// Generate shell command
|
||||
cmdString, err := GenerateShellCommands(s)
|
||||
if err != nil {
|
||||
s.ErrorChan <- err
|
||||
return
|
||||
@@ -45,6 +43,7 @@ func WatchPath(s *Settings) {
|
||||
s.ErrorChan <- err
|
||||
return
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
s.ErrorChan <- err
|
||||
return
|
||||
@@ -53,52 +52,69 @@ func WatchPath(s *Settings) {
|
||||
// Read the output of inotifywait and split it into lines
|
||||
scanner := bufio.NewScanner(stdout)
|
||||
for scanner.Scan() {
|
||||
log.Println(scanner.Text())
|
||||
line := scanner.Text()
|
||||
log.Println(line)
|
||||
|
||||
r := csv.NewReader(strings.NewReader(line))
|
||||
|
||||
parts, err := r.Read()
|
||||
parts, err := parseLine(line)
|
||||
if err != nil || len(parts) < 2 {
|
||||
s.ErrorChan <- fmt.Errorf(INVALID_OUTPUT)
|
||||
continue
|
||||
}
|
||||
|
||||
// Extract the input file name from the inotifywait output
|
||||
prefix := parts[0]
|
||||
file := parts[2]
|
||||
prefix, file := parts[0], parts[2]
|
||||
eventStrs := strings.Split(parts[1], ",")
|
||||
|
||||
eventsStr := strings.Split(parts[1], ",")
|
||||
if s.Verbose {
|
||||
for _, eventStr := range eventsStr {
|
||||
for _, eventStr := range eventStrs {
|
||||
log.Printf("eventStr: <%s>, <%s>", eventStr, line)
|
||||
}
|
||||
}
|
||||
var eventsEvents []EVENT
|
||||
isDir := false
|
||||
|
||||
for _, eventStr := range eventsStr {
|
||||
if eventStr == FlagIsdir {
|
||||
isDir = true
|
||||
continue
|
||||
}
|
||||
|
||||
eventStr = strings.ToLower(eventStr)
|
||||
event, ok := EVENT_MAP_REVERSE[eventStr]
|
||||
if !ok {
|
||||
s.ErrorChan <- fmt.Errorf("invalid eventStr: <%s>, <%s>", eventStr, line)
|
||||
continue
|
||||
}
|
||||
eventsEvents = append(eventsEvents, EVENT(event))
|
||||
events, isDir := parseEvents(eventStrs, line, s)
|
||||
if events == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
event := FileEvent{
|
||||
Filename: prefix + file,
|
||||
Events: eventsEvents,
|
||||
Events: events,
|
||||
IsDir: isDir,
|
||||
}
|
||||
|
||||
// Send the file name to the channel
|
||||
s.FileEvents <- event
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
s.ErrorChan <- err
|
||||
}
|
||||
}
|
||||
|
||||
// parseLine parses a line of inotifywait output.
|
||||
func parseLine(line string) ([]string, error) {
|
||||
r := csv.NewReader(strings.NewReader(line))
|
||||
return r.Read()
|
||||
}
|
||||
|
||||
// parseEvents parses event strings into EVENT types.
|
||||
func parseEvents(eventStrs []string, line string, s *Settings) ([]EVENT, bool) {
|
||||
var events []EVENT
|
||||
isDir := false
|
||||
|
||||
for _, eventStr := range eventStrs {
|
||||
if eventStr == FlagIsdir {
|
||||
isDir = true
|
||||
continue
|
||||
}
|
||||
|
||||
eventStr = strings.ToLower(eventStr)
|
||||
event, ok := EVENT_MAP_REVERSE[eventStr]
|
||||
if !ok {
|
||||
s.ErrorChan <- fmt.Errorf("invalid eventStr: <%s>, <%s>", eventStr, line)
|
||||
return nil, false
|
||||
}
|
||||
events = append(events, EVENT(event))
|
||||
}
|
||||
|
||||
return events, isDir
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user