Skip to content

Commit ee738a5

Browse files
authored
Merge pull request #19 from Kilgaloon/development
When new recipe is created, add it to queue
2 parents 7dbce75 + a6bac76 commit ee738a5

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.vscode
2-
debug
2+
/bin/debug
3+
/bin/leprechaun
4+

configs/client.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# we want to log our errors, and specify where
2-
error_log = ./logs/error-client.log
2+
error_log = ../logs/error-client.log
33
# we want to log our info about app to know whats going on
4-
info_log = ./logs/info-client.log
4+
info_log = ../logs/info-client.log
55
# path to recipes directory
6-
recipes_path = ./recipes
6+
recipes_path = ../recipes
77

88

99
[variables]

src/client/main.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package client
22

33
import (
4-
"../log"
4+
"fmt"
55
"io/ioutil"
66
"time"
7+
"../log"
8+
"github.com/fsnotify/fsnotify"
79
)
810

911
// Client settings and configurations
@@ -25,7 +27,34 @@ func Start(iniPath *string) {
2527

2628
q := BuildQueue(client.Config.recipesPath, files)
2729

30+
// watch for new recipes
31+
watcher, err := fsnotify.NewWatcher()
32+
if err != nil {
33+
client.Logs.Error("Failed to create watcher")
34+
}
35+
36+
defer watcher.Close()
37+
38+
go func() {
39+
for {
40+
select {
41+
case event := <-watcher.Events:
42+
if event.Op&fsnotify.Create == fsnotify.Create {
43+
AddToQueue(&q.Stack, event.Name)
44+
}
45+
case err := <-watcher.Errors:
46+
fmt.Println("error:", err)
47+
}
48+
}
49+
}()
50+
51+
err = watcher.Add(client.Config.recipesPath)
52+
if err != nil {
53+
fmt.Println(err)
54+
}
55+
2856
for {
57+
2958
go ProcessQueue(&q, client)
3059

3160
time.Sleep(60 * time.Second)

src/client/queue.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99
"strings"
10+
"path/filepath"
1011
)
1112

1213
// Queue stack for pulling out recipes
@@ -28,6 +29,13 @@ func BuildQueue(path string, files []os.FileInfo) Queue {
2829
return q
2930
}
3031

32+
// AddToQueue takes freshly created recipes and add them to queue
33+
func AddToQueue(stack *[]recipe.Recipe, path string) {
34+
if filepath.Ext(path) == ".yml" {
35+
*stack = append(*stack, recipe.Build(path))
36+
}
37+
}
38+
3139
// ProcessQueue queue
3240
func ProcessQueue(queue *Queue, client *Client) {
3341
for index, r := range queue.Stack {

0 commit comments

Comments
 (0)