예제 #1
0
파일: watchman.go 프로젝트: qjpcpu/watchman
// Fetch an event of watching list, if there's no event available the function would blocked
func (man *Watchman) PullEvent() (router.Message, error) {
	raw, err := man.client.Read()
	if err != nil {
		return router.Message{}, err
	}
	m, err := router.ParseMessage(raw)
	if err != nil {
		return router.Message{}, err
	}
	fn := ""
	for name, _ := range man.paths {
		if inWatch(m.FileName, name) {
			fn = name
			break
		}
	}
	if _, ok := man.paths[fn]; ok && m.Event == 0x0 {
		return m, errors.New("SYSTEM")
	}
	if m.Event == 0x0 || fn == "" || man.paths[fn]&m.Event == 0 {
		return m, errors.New("You dont' need it.")
	}
	m.Event = m.Event & man.paths[fn]
	return m, nil
}
예제 #2
0
파일: alfred.go 프로젝트: qjpcpu/watchman
// The request message must like:
//{
//    "Event":0,
//    "FileName":"+/path/to/file",  or "FileName":"-/path/to/file"  // +/- means add or remove watch
//    ....
//}
func (em *Distributer) PullRequest() (map[string]string, error) {
	str, err := em.Read()
	if err != nil {
		logging.Debug("Pull request:", err)
		return nil, err
	}
	m, err := router.ParseMessage(str)
	if err != nil {
		logging.Debug("Pull request:", err)
		return nil, err
	}
	if m.Event != 0 {
		return nil, errors.New("Invalid request")
	}
	msg := make(map[string]string)
	if strings.HasPrefix(m.FileName, "+") {
		msg["ACTION"] = "ADD"
		msg["PATH"] = strings.TrimLeft(m.FileName, "+")
		return msg, nil
	} else if strings.HasPrefix(m.FileName, "-") {
		msg["ACTION"] = "REMOVE"
		msg["PATH"] = strings.TrimLeft(m.FileName, "-")
		return msg, nil
	}
	return nil, errors.New("Shouldn't come here")
}