-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
106 lines (82 loc) · 2.01 KB
/
core.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package filesys
import (
"errors"
"io/ioutil"
"log"
"github.com/deeper-x/ique/client"
"github.com/deeper-x/ique/configuration"
"github.com/deeper-x/ique/myutils"
"github.com/deeper-x/ique/snstools"
"github.com/fsnotify/fsnotify"
)
// OS is the main package interface
type OS interface {
AddListener() error
ReadFileContent(string) (string, error)
}
// FileManager is in charge for OS management
type FileManager struct {
Pwd string
}
const name = configuration.QueueName
// ReadFileContent read file content and return it
func (fm FileManager) ReadFileContent(fileName string) (string, error) {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return "", err
}
return string(data), nil
}
// AddListener listen for file creation
func (fm FileManager) AddListener() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() error {
log.Printf("Monitoring %v ...\n", fm.Pwd)
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return errors.New("Error: Watching event")
}
if event.Op.String() == "CREATE" {
created := event.Name
log.Printf("#TODO trigger push + deletion of %s\n", created)
// Listening dir content
dataContent, err := fm.ReadFileContent(created)
myutils.FailsOnError(err, "Failed to read file content...")
pitch := client.Pitch{}
err = client.Run(&pitch, name, dataContent)
myutils.FailsOnError(err, "Failed running sender...")
c := snstools.NewClient()
_, err = snstools.PushToSNS(c, dataContent)
myutils.FailsOnError(err, "Failed to send message to AWS::SNS")
}
case err, ok := <-watcher.Errors:
if !ok {
return err
}
log.Println("Error:", err)
}
}
}()
err = watcher.Add(fm.Pwd)
if err != nil {
log.Println(err)
return err
}
<-done
return nil
}
// RunListen is the listener runner
func RunListen(o OS) error {
err := o.AddListener()
if err != nil {
return err
}
return nil
}