forked from davrodpin/mole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
62 lines (49 loc) · 1.15 KB
/
rpc.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
package mole
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"github.com/davrodpin/mole/fsutils"
"github.com/davrodpin/mole/rpc"
)
func init() {
rpc.Register("show-instance", ShowRpc)
}
// ShowRpc is a rpc callback that returns runtime information about the mole client.
func ShowRpc(params interface{}) (json.RawMessage, error) {
if cli == nil {
return nil, fmt.Errorf("client configuration could not be found.")
}
runtime, err := cli.Runtime()
if err != nil {
return nil, err
}
cj, err := json.Marshal(runtime)
if err != nil {
return nil, err
}
return json.RawMessage(cj), nil
}
// Rpc calls a remote procedure on another mole instance given its id or alias.
func Rpc(id, method string, params interface{}) (string, error) {
d, err := fsutils.InstanceDir(id)
if err != nil {
return "", err
}
rf := filepath.Join(d.Dir, "rpc")
addr, err := ioutil.ReadFile(rf)
if err != nil {
return "", err
}
resp, err := rpc.Call(context.Background(), string(addr), method, params)
if err != nil {
return "", err
}
r, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return "", err
}
return string(r), nil
}