Skip to content

Commit

Permalink
enhanced version, not using slice in chan
Browse files Browse the repository at this point in the history
  • Loading branch information
pablodz committed Dec 10, 2023
1 parent 7a95381 commit 4d84dda
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 59 deletions.
1 change: 1 addition & 0 deletions .version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.0.4
20 changes: 12 additions & 8 deletions example/watcher.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package example
package main

import "github.com/pablodz/inotifywaitgo/inotifywaitgo"
import (
"fmt"

func Example() {
dir := "./safasfsas"
files := make(chan []byte)
errors := make(chan []byte)
"github.com/pablodz/inotifywaitgo/inotifywaitgo"
)

func main() {
dir := "./"
files := make(chan string)
errors := make(chan error)

go inotifywaitgo.WatchPath(&inotifywaitgo.Settings{
Dir: dir,
Expand All @@ -23,9 +27,9 @@ loopFiles:
for {
select {
case file := <-files:
println(string(file))
fmt.Printf("File: %s\n", file)
case err := <-errors:
println(string(err))
fmt.Printf("Error: %s\n", err)
break loopFiles
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/pablodz/inotifywaitgo

go 1.20
go 1.21
4 changes: 2 additions & 2 deletions inotifywaitgo/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ type Settings struct {
// Directory to watch
Dir string
// Channel to send the file name to
OutFiles chan []byte
OutFiles chan string
// Channel to send errors to
ErrorChan chan []byte
ErrorChan chan error
// Options for inotifywait
Options *OptionsInotify
// Kill other inotifywait processes
Expand Down
15 changes: 8 additions & 7 deletions inotifywaitgo/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package inotifywaitgo

import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
Expand All @@ -12,14 +13,14 @@ func WatchPath(s *Settings) {
// Check if inotifywait is installed
ok, err := checkDependencies()
if !ok || err != nil {
s.ErrorChan <- []byte(NOT_INSTALLED)
s.ErrorChan <- fmt.Errorf(NOT_INSTALLED)
return
}

// check if dir exists
_, err = os.Stat(s.Dir)
if os.IsNotExist(err) {
s.ErrorChan <- []byte(DIR_NOT_EXISTS)
s.ErrorChan <- fmt.Errorf(DIR_NOT_EXISTS)
return
}

Expand All @@ -31,19 +32,19 @@ func WatchPath(s *Settings) {
// Generate bash command
cmdString, err := GenerateBashCommands(s)
if err != nil {
s.ErrorChan <- []byte(err.Error())
s.ErrorChan <- err
return
}

// Start inotifywait in the input directory and watch for close_write events
cmd := exec.Command(cmdString[0], cmdString[1:]...)
stdout, err := cmd.StdoutPipe()
if err != nil {
s.ErrorChan <- []byte(err.Error())
s.ErrorChan <- err
return
}
if err := cmd.Start(); err != nil {
s.ErrorChan <- []byte(err.Error())
s.ErrorChan <- err
return
}

Expand All @@ -53,14 +54,14 @@ func WatchPath(s *Settings) {
line := scanner.Text()
parts := strings.Split(line, " ")
if len(parts) < 2 {
s.ErrorChan <- []byte(INVALID_OUTPUT)
s.ErrorChan <- fmt.Errorf(INVALID_OUTPUT)
continue
}

// Extract the input file name from the inotifywait output
prefix := parts[0]
file := parts[len(parts)-1]
// Send the file name to the channel
s.OutFiles <- []byte(prefix + file)
s.OutFiles <- fmt.Sprintf("%s%s", prefix, file)
}
}
41 changes: 0 additions & 41 deletions main.go

This file was deleted.

0 comments on commit 4d84dda

Please sign in to comment.