Exemple #1
0
func (mod *htmod) route(m hub.Msg, id hub.Id) {
	var (
		err error
		msg hub.Msg
	)
	switch m.Head {
	case hub.Signon:
		// send reports for all working packages
		msg, err = hub.Marshal("reports", mod.src.AllReports())
	case "stat":
		var path string
		if err = m.Unmarshal(&path); err != nil {
			break
		}
		msg, err = mod.stat(path)
	case "subscribe", "unsubscribe", "revise", "publish":
		mod.docroute(m, id)
		return
	case "complete", "format":
		mod.actionRoute(m, id)
		return
	default:
		msg, err = hub.Marshal("unknown", m.Head)
	}
	if err != nil {
		log.Println(err)
		return
	}
	mod.SendMsg(msg, id)
}
Exemple #2
0
func (mod *htmod) docroute(m hub.Msg, from hub.Id) {
	var rev apiRev
	err := m.Unmarshal(&rev)
	if err != nil {
		log.Println(err)
		return
	}
	rev.User = from
	mod.docs.Lock()
	defer mod.docs.Unlock()
	doc, found := mod.docs.all[rev.Id]
	if found {
		doc.Lock()
		defer doc.Unlock()
	}
	mod.handlerev(m.Head, rev, doc)
}
Exemple #3
0
func (mod *htmod) actionRoute(m hub.Msg, from hub.Id) {
	var req actionReq
	err := m.Unmarshal(&req)
	if err != nil {
		log.Println(err)
		return
	}
	mod.docs.RLock()
	doc, found := mod.docs.all[req.Id]
	mod.docs.RUnlock()
	if !found {
		log.Println("no document open with id", req)
		return
	}
	if pl := len(doc.Path); pl < 3 || doc.Path[pl-3:] != ".go" {
		log.Println("document not a go file", doc.Path)
		return
	}
	switch {
	case m.Head == "complete" && gocode != "":
		cmd := &exec.Cmd{
			Path: gocode,
			Args: []string{
				"gocode",
				"-f=json",
				"autocomplete",
				fmt.Sprint(req.Offs),
			},
		}
		var buf bytes.Buffer
		doc.Lock()
		buf.Write(([]byte)(*doc.Doc))
		doc.Unlock()
		cmd.Stdin = &buf
		data, err := cmd.CombinedOutput()
		if err != nil {
			log.Println(err, data)
			return
		}
		if len(data) < 10 {
			return
		}
		data = data[4 : len(data)-1]
		m, err = hub.Marshal("complete", struct {
			actionReq
			Proposed *json.RawMessage
		}{req, (*json.RawMessage)(&data)})
		if err != nil {
			log.Println(err)
			return
		}
		mod.SendMsg(m, from)
	case m.Head == "format":
		doc.Lock()
		defer doc.Unlock()
		data, err := format.Source(([]byte)(*doc.Doc))
		if err != nil {
			log.Println(err, data)
		}
		rev := doc.Rev()
		ops := doc.diffops(data)
		if ops != nil {
			mod.handlerev("revise", apiRev{Id: req.Id, Rev: rev, Ops: ops}, doc)
		}
		return
	default:
		log.Println("unexpected msg", m.Head)
		return
	}
}