| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package notify_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"log" |
| 9 |
"path/filepath" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/syncthing/notify" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
func ExampleWatch() { |
| 17 |
|
| 18 |
|
| 19 |
c := make(chan notify.EventInfo, 1) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
if err := notify.Watch(".", c, notify.Create, notify.Remove); err != nil { |
| 24 |
log.Fatal(err) |
| 25 |
} |
| 26 |
defer notify.Stop(c) |
| 27 |
|
| 28 |
|
| 29 |
ei := <-c |
| 30 |
log.Println("Got event:", ei) |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
func ExampleWatch_recursive() { |
| 35 |
|
| 36 |
|
| 37 |
c := make(chan notify.EventInfo, 1) |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
if err := notify.Watch("./...", c, notify.Remove); err != nil { |
| 42 |
log.Fatal(err) |
| 43 |
} |
| 44 |
defer notify.Stop(c) |
| 45 |
|
| 46 |
|
| 47 |
ei := <-c |
| 48 |
log.Println("Got event:", ei) |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
func ExampleStop() { |
| 54 |
waitfor := func(path string, e notify.Event, timeout time.Duration) bool { |
| 55 |
dir, file := filepath.Split(path) |
| 56 |
c := make(chan notify.EventInfo, 1) |
| 57 |
|
| 58 |
if err := notify.Watch(dir, c, e); err != nil { |
| 59 |
log.Fatal(err) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
defer notify.Stop(c) |
| 65 |
|
| 66 |
t := time.After(timeout) |
| 67 |
|
| 68 |
for { |
| 69 |
select { |
| 70 |
case ei := <-c: |
| 71 |
if filepath.Base(ei.Path()) == file { |
| 72 |
return true |
| 73 |
} |
| 74 |
case <-t: |
| 75 |
return false |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if waitfor("index.lock", notify.Create, 5*time.Second) { |
| 81 |
log.Println("The git repository was locked") |
| 82 |
} |
| 83 |
|
| 84 |
if waitfor("index.lock", notify.Remove, 5*time.Second) { |
| 85 |
log.Println("The git repository was unlocked") |
| 86 |
} |
| 87 |
} |