-
Notifications
You must be signed in to change notification settings - Fork 12
/
command.go
76 lines (62 loc) · 1.5 KB
/
command.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
package livestatus
import (
"fmt"
"net"
"strings"
"time"
)
// Command represents a Livestatus command instance.
type Command struct {
name string
args []string
writeTimeout time.Duration
}
// NewCommand creates a new Livestatus command instance.
func NewCommand(name string, args ...string) *Command {
return &Command{
name: name,
args: args,
}
}
// Arg appends a new argument to the command.
func (c *Command) Arg(v interface{}) *Command {
c.args = append(c.args, fmt.Sprintf("%v", v))
return c
}
// WriteTimeout sets the connection timeout for write operations.
// A value of 0 disables the timeout.
func (c *Command) WriteTimeout(timeout time.Duration) *Command {
c.writeTimeout = timeout
return c
}
// String returns a string representation of the Livestatus command.
func (c Command) String() string {
s := fmt.Sprintf("COMMAND [%d] %s", time.Now().Unix(), c.name)
if len(c.args) > 0 {
s += ";" + strings.Join(c.args, ";")
}
s += "\n\n"
return s
}
func (c Command) handle(conn net.Conn) (*Response, error) {
cmd := c.String()
lcmd := len(cmd)
if c.writeTimeout > 0 {
conn.SetWriteDeadline(time.Now().Add(c.writeTimeout))
} else {
// disable timeout
conn.SetWriteDeadline(time.Time{})
}
// Send query data
n, err := conn.Write([]byte(cmd))
if err != nil {
return nil, err
}
if n != lcmd {
return nil, fmt.Errorf("incomplete write to livestatus. Wrote %d bytes while %d were to be written", n, lcmd)
}
return nil, nil
}
func (c Command) keepAlive() bool {
return true
}