github.com/slantview/etcdctl@v0.1.3-0.20131011185546-5aaeca137f94/watch.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "github.com/coreos/etcd/store" 7 "os" 8 "os/signal" 9 ) 10 11 const WatchUsage = `usage: etcdctl [etcd flags] watch <key> [watch flags] 12 special flags: -f forever watch a key until CTRL+C 13 -i watch from the given index` 14 15 var ( 16 watchFlag = flag.NewFlagSet("watch", flag.ExitOnError) 17 forever = watchFlag.Bool("f", false, "forever watch at the key") 18 index = watchFlag.Int64("i", 0, "watch from the given index") 19 ) 20 21 func init() { 22 registerCommand("watch", WatchUsage, 2, 6, watch) 23 } 24 25 func watch(args []string) error { 26 key := args[1] 27 watchFlag.Parse(args[2:]) 28 29 if *forever { 30 31 c := make(chan os.Signal, 1) 32 signal.Notify(c, os.Interrupt) 33 34 stop := make(chan bool) 35 36 go func() { 37 <-c 38 stop <- true 39 os.Exit(0) 40 }() 41 42 receiver := make(chan *store.Response) 43 go client.Watch(key, uint64(*index), receiver, stop) 44 45 for { 46 resp := <-receiver 47 fmt.Println(resp.Action, " ", resp.Key, " ", resp.Value) 48 } 49 50 } else { 51 resp, err := client.Watch(key, uint64(*index), nil, nil) 52 if err != nil { 53 return err 54 } 55 fmt.Println(resp.Action, " ", resp.Key, " ", resp.Value) 56 } 57 58 return nil 59 }