Skip to content

Commit 4d84dda

Browse files
committed
enhanced version, not using slice in chan
1 parent 7a95381 commit 4d84dda

File tree

6 files changed

+24
-59
lines changed

6 files changed

+24
-59
lines changed

.version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.0.4

example/watcher.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
package example
1+
package main
22

3-
import "github.com/pablodz/inotifywaitgo/inotifywaitgo"
3+
import (
4+
"fmt"
45

5-
func Example() {
6-
dir := "./safasfsas"
7-
files := make(chan []byte)
8-
errors := make(chan []byte)
6+
"github.com/pablodz/inotifywaitgo/inotifywaitgo"
7+
)
8+
9+
func main() {
10+
dir := "./"
11+
files := make(chan string)
12+
errors := make(chan error)
913

1014
go inotifywaitgo.WatchPath(&inotifywaitgo.Settings{
1115
Dir: dir,
@@ -23,9 +27,9 @@ loopFiles:
2327
for {
2428
select {
2529
case file := <-files:
26-
println(string(file))
30+
fmt.Printf("File: %s\n", file)
2731
case err := <-errors:
28-
println(string(err))
32+
fmt.Printf("Error: %s\n", err)
2933
break loopFiles
3034
}
3135
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/pablodz/inotifywaitgo
22

3-
go 1.20
3+
go 1.21

inotifywaitgo/models.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ type Settings struct {
44
// Directory to watch
55
Dir string
66
// Channel to send the file name to
7-
OutFiles chan []byte
7+
OutFiles chan string
88
// Channel to send errors to
9-
ErrorChan chan []byte
9+
ErrorChan chan error
1010
// Options for inotifywait
1111
Options *OptionsInotify
1212
// Kill other inotifywait processes

inotifywaitgo/watcher.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package inotifywaitgo
22

33
import (
44
"bufio"
5+
"fmt"
56
"os"
67
"os/exec"
78
"strings"
@@ -12,14 +13,14 @@ func WatchPath(s *Settings) {
1213
// Check if inotifywait is installed
1314
ok, err := checkDependencies()
1415
if !ok || err != nil {
15-
s.ErrorChan <- []byte(NOT_INSTALLED)
16+
s.ErrorChan <- fmt.Errorf(NOT_INSTALLED)
1617
return
1718
}
1819

1920
// check if dir exists
2021
_, err = os.Stat(s.Dir)
2122
if os.IsNotExist(err) {
22-
s.ErrorChan <- []byte(DIR_NOT_EXISTS)
23+
s.ErrorChan <- fmt.Errorf(DIR_NOT_EXISTS)
2324
return
2425
}
2526

@@ -31,19 +32,19 @@ func WatchPath(s *Settings) {
3132
// Generate bash command
3233
cmdString, err := GenerateBashCommands(s)
3334
if err != nil {
34-
s.ErrorChan <- []byte(err.Error())
35+
s.ErrorChan <- err
3536
return
3637
}
3738

3839
// Start inotifywait in the input directory and watch for close_write events
3940
cmd := exec.Command(cmdString[0], cmdString[1:]...)
4041
stdout, err := cmd.StdoutPipe()
4142
if err != nil {
42-
s.ErrorChan <- []byte(err.Error())
43+
s.ErrorChan <- err
4344
return
4445
}
4546
if err := cmd.Start(); err != nil {
46-
s.ErrorChan <- []byte(err.Error())
47+
s.ErrorChan <- err
4748
return
4849
}
4950

@@ -53,14 +54,14 @@ func WatchPath(s *Settings) {
5354
line := scanner.Text()
5455
parts := strings.Split(line, " ")
5556
if len(parts) < 2 {
56-
s.ErrorChan <- []byte(INVALID_OUTPUT)
57+
s.ErrorChan <- fmt.Errorf(INVALID_OUTPUT)
5758
continue
5859
}
5960

6061
// Extract the input file name from the inotifywait output
6162
prefix := parts[0]
6263
file := parts[len(parts)-1]
6364
// Send the file name to the channel
64-
s.OutFiles <- []byte(prefix + file)
65+
s.OutFiles <- fmt.Sprintf("%s%s", prefix, file)
6566
}
6667
}

main.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)