-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
138 lines (129 loc) · 2.88 KB
/
client.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"io"
"net"
"os"
"os/signal"
"os/user"
"path/filepath"
"strings"
"gnd.la/log"
"github.com/fiam/stringutil"
)
const help = `available commands are:
start <service|all> : starts a service or all services, in priority order
stop <service|all> : stops a service or all services, in priority order
restart <service|all> : restart a service or all services, in priority order
list : list registered services
exit : close the shell
help : show help`
func sendCommand(serverAddr string, args []string) (bool, error) {
scheme, addr, err := parseServerAddr(serverAddr)
if err != nil {
return false, err
}
conn, err := net.Dial(scheme, addr)
if err != nil {
return false, err
}
defer conn.Close()
if err := encodeArgs(conn, args); err != nil {
return false, err
}
log.Debugf("sent command %s", args)
closed := false
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
defer signal.Stop(ch)
done := make(chan struct{}, 1)
defer func() {
done <- struct{}{}
}()
go func() {
select {
case <-ch:
closed = true
conn.Close()
case <-done:
}
}()
ok := true
for {
r, s, err := decodeResponse(conn)
if err != nil {
if closed {
return ok, nil
}
return ok, err
}
log.Debugf("received response %d", r)
switch r {
case respEnd:
return ok, nil
case respOk:
fmt.Print(s)
case respErr:
ok = false
fmt.Fprint(os.Stderr, s)
default:
return false, fmt.Errorf("invalid response type %d", r)
}
}
return ok, nil
}
func evalCommand(addr string, args []string) (bool, error) {
if len(args) > 0 {
switch strings.ToLower(args[0]) {
case "quit", "exit":
os.Exit(0)
case "help":
fmt.Fprintf(os.Stderr, "%s\n", help)
return true, nil
}
}
return sendCommand(addr, args)
}
func clientMain(addr string, args []string) (bool, error) {
createGovernatorUserDir()
if len(args) > 0 {
return evalCommand(addr, args)
}
r := newLineReader()
fmt.Printf("%s interactive shell\nType exit or press control+d to end\nType help to show available commands\n\n", AppName)
sendCommand(addr, []string{"list"})
for {
s, err := r.ReadLine()
if err == io.EOF {
fmt.Print("exit\n")
break
}
s = strings.TrimSpace(s)
if s != "" {
r.AddHistory(s)
fields, err := stringutil.SplitFields(s, " ")
if err != nil {
fmt.Fprintf(os.Stderr, "error reading input: %s\n", err)
continue
}
if _, err := evalCommand(addr, fields); err != nil {
fmt.Fprintf(os.Stderr, "error executing command: %s\n", err)
}
}
}
return true, nil
}
func governatorUserDir() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(usr.HomeDir, "."+AppName), nil
}
func createGovernatorUserDir() error {
dir, err := governatorUserDir()
if err != nil {
return err
}
return os.Mkdir(dir, 0755)
}