forked from go-routeros/routeros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.go
64 lines (56 loc) · 1.12 KB
/
reply.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
package routeros
import (
"bytes"
"fmt"
"github.com/go-routeros/routeros/proto"
)
// Reply has all the sentences from a reply.
type Reply struct {
Re []*proto.Sentence
Done *proto.Sentence
}
func (r *Reply) String() string {
b := &bytes.Buffer{}
for _, re := range r.Re {
fmt.Fprintf(b, "%s\n", re)
}
fmt.Fprintf(b, "%s", r.Done)
return b.String()
}
// readReply reads one reply synchronously. It returns the reply.
func (c *Client) readReply() (*Reply, error) {
r := &Reply{}
var lastErr error
for {
sen, err := c.r.ReadSentence()
if err != nil {
return nil, err
}
done, err := r.processSentence(sen)
if err != nil {
if done {
return nil, err
}
lastErr = err
}
if done {
return r, lastErr
}
}
}
func (r *Reply) processSentence(sen *proto.Sentence) (bool, error) {
switch sen.Word {
case "!re":
r.Re = append(r.Re, sen)
case "!done":
r.Done = sen
return true, nil
case "!trap", "!fatal":
return sen.Word == "!fatal", &DeviceError{sen}
case "":
// API docs say that empty sentences should be ignored
default:
return true, &UnknownReplyError{sen}
}
return false, nil
}