-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.go
105 lines (92 loc) · 2.53 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package watchman
import (
"github.com/sjansen/watchman/protocol"
)
// Client provides a high-level interface to Watchman.
type Client struct {
conn *protocol.Connection
stop func(bool)
requests chan<- protocol.Request
responses <-chan result
updates <-chan interface{}
}
// Connect connects to or starts the Watchman server and returns a
// new Client.
func Connect() (c *Client, err error) {
conn, err := protocol.Connect()
if err != nil {
return
}
loop, stop := startEventLoop(conn)
c = &Client{
conn: conn,
stop: stop,
requests: loop.requests,
responses: loop.responses,
updates: loop.updates,
}
return
}
func (c *Client) send(req protocol.Request) (res protocol.ResponsePDU, err error) {
c.requests <- req
result := <-c.responses
if result.err == nil {
res = result.pdu
} else {
err = result.err
}
return
}
// AddWatch requests that the Watchman server monitor a directory for changes.
//
// Please note that Watchman may reuse an existing watch, or choose to start
// watching a parent of the requested directory.
//
// For details, see: https://facebook.github.io/watchman/docs/cmd/watch-project.html
func (c *Client) AddWatch(path string) (*Watch, error) {
req := &protocol.WatchProjectRequest{Path: path}
pdu, err := c.send(req)
if err != nil {
return nil, err
}
res := protocol.NewWatchProjectResponse(pdu)
w := &Watch{
client: c,
root: res.Watch(),
}
return w, nil
}
// Close closes the connection to the Watchman server.
func (c *Client) Close() error {
c.stop(false)
return nil
}
// HasCapability checks if the Watchman server supports a feature.
//
// For details, see: https://facebook.github.io/watchman/docs/capabilities.html
func (c *Client) HasCapability(capability string) bool {
return c.conn.HasCapability(capability)
}
// ListWatches returns a list of directories that Watchman is monitoring.
func (c *Client) ListWatches() (roots []string, err error) {
req := &protocol.WatchListRequest{}
if pdu, err := c.send(req); err == nil {
res := protocol.NewWatchListResponse(pdu)
roots = res.Roots()
}
return
}
// Notifications returns a channel that emits unilateral messages
// from the Watchman server.
func (c *Client) Notifications() <-chan interface{} {
return c.updates
}
// SockName returns the location of then UNIX domain socket used
// to communicate with the Watchman server.
func (c *Client) SockName() string {
return c.conn.SockName()
}
// Version returns the version of the Watchman server.
func (c *Client) Version() string {
return c.conn.Version()
}