Permalink
Browse files

First step towards managing events and rules

  • Loading branch information...
1 parent f8e3f60 commit 307536b116cbc6e3287075de0f628f09b9a2ddad @mrnugget committed Mar 28, 2013
Showing with 40 additions and 11 deletions.
  1. +9 −6 config.go
  2. +30 −0 manage.go
  3. +1 −5 watchgopher.go
View
@@ -7,7 +7,7 @@ import (
type Rule struct {
Path string
- Run string
+ Run string
}
func ParseConfig(path string) (rules []*Rule, err error) {
@@ -28,13 +28,16 @@ func ParseConfig(path string) (rules []*Rule, err error) {
for path, v := range config {
attributes := v.(map[string]interface{})
run := attributes["run"].(string)
-
- for len(path) > 0 && path[len(path)-1] == '/' {
- path = path[0 : len(path)-1]
- }
-
+ path = stripTrailingSlash(path)
rules = append(rules, &Rule{path, run})
}
return rules, nil
}
+
+func stripTrailingSlash(path string) string {
+ for len(path) > 0 && path[len(path)-1] == '/' {
+ path = path[0 : len(path)-1]
+ }
+ return path
+}
View
@@ -0,0 +1,30 @@
+package main
+
+import (
+ "fmt"
+ "github.com/howeyc/fsnotify"
+ "path"
+)
+
+func Manage(events chan *fsnotify.FileEvent, rules []*Rule) {
+ for ev := range events {
+ fmt.Println("RECEIVED EVENT")
+
+ dir, file := path.Split(ev.Name)
+ dir = stripTrailingSlash(dir)
+
+ rule := ruleWithPath(rules, dir)
+ if rule != nil {
+ fmt.Printf("RUN: %s, FILE ARG: %s\n", rule.Run, file)
+ }
+ }
+}
+
+func ruleWithPath(rules []*Rule, path string) (rule *Rule) {
+ for _, rule := range rules {
+ if rule.Path == path {
+ return rule
+ }
+ }
+ return nil
+}
View
@@ -41,11 +41,7 @@ func main() {
}
}()
- // @TODO: Manage the events, check if a rule applies (file is in path),
- // then run script with arguments
- for ev := range watcher.Events {
- fmt.Println("EVENT - File:", ev.Name)
- }
+ Manage(watcher.Events, rules)
// @TODO: If filename matches a pattern (e.g. `*.jpg`), pass it to a worker,
// that shells out and runs configured command with two arguments:

0 comments on commit 307536b

Please sign in to comment.