-
Notifications
You must be signed in to change notification settings - Fork 13
/
zinit.go
114 lines (94 loc) · 2.21 KB
/
zinit.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Package zinit exposes function to interat with zinit service life cyle management
package zinit
import (
"bufio"
"fmt"
"net"
"strconv"
"strings"
"github.com/pkg/errors"
)
const defaultSocketPath = "/var/run/zinit.sock"
// Client is a client for zinit action
// it talks to zinit directly over its unis socket
type Client struct {
conn net.Conn
scan *bufio.Scanner
}
// New create a new Zinit client
func New(socket string) (*Client, error) {
if socket == "" {
socket = defaultSocketPath
}
conn, err := net.Dial("unix", socket)
if err != nil {
return nil, err
}
scan := bufio.NewScanner(conn)
return &Client{conn: conn, scan: scan}, nil
}
// Close closes the socket connection
func (c *Client) Close() error {
if c.conn != nil {
if err := c.conn.Close(); err != nil {
return err
}
}
c.conn = nil
return nil
}
func (c *Client) cmd(cmd string) (string, error) {
if c.conn == nil {
return "", fmt.Errorf("not connected, call Connect() before executing command ")
}
if _, err := c.conn.Write([]byte(cmd)); err != nil {
return "", err
}
if _, err := c.conn.Write([]byte("\n")); err != nil {
return "", err
}
return c.readResponse()
}
func (c *Client) readResponse() (string, error) {
var (
count uint64
status string
err error
)
headers := map[string]string{}
scanner := c.scan
for scanner.Scan() {
line := scanner.Text()
if line == "" {
// end of headers section
break
}
parts := strings.SplitN(line, ":", 2)
headers[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}
if err := scanner.Err(); err != nil {
return "", errors.Wrap(err, "error while reading socket")
}
count, err = strconv.ParseUint(headers["lines"], 10, 32)
if err != nil {
return "", errors.Wrapf(err, "failed to parse response length: %+v", headers)
}
status = headers["status"]
var content strings.Builder
for i := uint64(0); i < count; i++ {
if !scanner.Scan() {
break
}
if content.Len() > 0 {
content.WriteByte('\n')
}
content.WriteString(scanner.Text())
}
if err := scanner.Err(); err != nil {
return "", errors.Wrap(err, "error while reading socket")
}
if status == "error" {
return "", fmt.Errorf(content.String())
}
return content.String(), nil
}