forked from go-routeros/routeros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.go
executable file
·58 lines (52 loc) · 1.05 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
package routeros
import (
"bytes"
"fmt"
"github.com/hainguyen8y/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{}
for {
sen, err := c.r.ReadSentence()
if err != nil {
return nil, err
}
done, err := r.processSentence(sen)
if err != nil {
return nil, err
}
if done {
return r, nil
}
}
}
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 true, &DeviceError{sen}
case "":
// API docs say that empty sentences should be ignored
default:
return true, &UnknownReplyError{sen}
}
return false, nil
}