Debian Codegraph @

clinotify/clinotify.go

raw symbols SCIP
1 // clinotify provides an example file system watching command line app. It
2 // scans the file system, and every 15 seconds prints out the files being
3 // watched and their current state.
4 package main
5
6 import (
7 "flag"
8 "fmt"
9 "github.com/gokyle/fswatch"
10 "os"
11 "time"
12 )
13
14 var dur time.Duration
15
16 func init() {
17 if len(os.Args) == 1 {
18 fmt.Println("[+] not watching anything, exiting.")
19 os.Exit(1)
20 }
21 dur, _ = time.ParseDuration("15s")
22 }
23
24 func main() {
25 var w *fswatch.Watcher
26
27 auto_watch := flag.Bool("a", false, "auto add new files in directories")
28 flag.Parse()
29 paths := flag.Args()
30 if *auto_watch {
31 w = fswatch.NewAutoWatcher(paths...)
32 } else {
33 w = fswatch.NewWatcher(paths...)
34 }
35 fmt.Println("[+] listening...")
36
37 l := w.Start()
38 go func() {
39 for {
40 n, ok := <-l
41 if !ok {
42 return
43 }
44 var status_text string
45 switch n.Event {
46 case fswatch.CREATED:
47 status_text = "was created"
48 case fswatch.DELETED:
49 status_text = "was deleted"
50 case fswatch.MODIFIED:
51 status_text = "was modified"
52 case fswatch.PERM:
53 status_text = "permissions changed"
54 case fswatch.NOEXIST:
55 status_text = "doesn't exist"
56 case fswatch.NOPERM:
57 status_text = "has invalid permissions"
58 case fswatch.INVALID:
59 status_text = "is invalid"
60 }
61 fmt.Printf("[+] %s %s\n", n.Path, status_text)
62 }
63 }()
64 go func() {
65 for {
66 <-time.After(dur)
67 if !w.Active() {
68 fmt.Println("[!] not watching anything")
69 os.Exit(1)
70 }
71 fmt.Printf("[-] watching: %+v\n", w.State())
72 }
73 }()
74 time.Sleep(60 * time.Second)
75 fmt.Println("[+] stopping...")
76 w.Stop()
77 time.Sleep(5 * time.Second)
78 }