/
main.go
66 lines (54 loc) · 1.24 KB
/
main.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
package main
import (
"net"
"os"
"time"
"github.com/DanielKrawisz/bmagent/bmrpc"
"github.com/DanielKrawisz/bmagent/cmd"
"github.com/DanielKrawisz/bmagent/cmd/rpc"
)
func request(req *rpc.BMRPCRequest) (*rpc.BMRPCReply, error) {
// Create an rpc connection to bmagent.
client, err := cmd.GRPCClient(&bmrpc.ClientConfig{
DisableTLS: true,
ConnectTo: net.JoinHostPort("localhost", "8446"),
Timeout: time.Second,
})
if err != nil {
println("request error...")
return nil, err
}
return client.Request(req)
}
func run(command cmd.Command, args []string) (*rpc.BMRPCReply, error) {
// Make an rpc request out of the command.
req, err := command.RPC()
if err != nil {
return nil, err
}
// Send it to the server.
return request(req)
}
func try(cmdName string, args []string) string {
// Check if the command exists.
command, err := cmd.ReadCommand(cmdName, args)
if err != nil {
return err.Error()
}
// Try to send it to the server.
response, err := run(command, args)
if err != nil {
println("response error")
return err.Error()
}
return rpc.Message(response)
}
func main() {
if len(os.Args) < 2 {
println("Put a help message here.")
return
}
name := os.Args[1]
args := os.Args[2:]
println(try(name, args))
}