forked from davrodpin/mole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_rpc.go
62 lines (49 loc) · 1.19 KB
/
misc_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 cmd
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/davrodpin/mole/rpc"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
method, params string
miscRpcCmd = &cobra.Command{
Use: "rpc [alias or id] [method] [params]",
Short: "Executes a remote procedure call on a given mole instance",
Long: "Executes a remote procedure call on a given mole instance",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("not enough arguments.")
}
id = args[0]
method = args[1]
if len(args) == 3 {
params = args[2]
}
return nil
},
Run: func(cmd *cobra.Command, arg []string) {
resp, err := rpc.CallById(context.Background(), id, method, params)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"id": id,
}).Error("error executing remote procedure.")
os.Exit(1)
}
json, err := json.MarshalIndent(resp, "", " ")
if err != nil {
log.WithError(err).WithFields(log.Fields{
"id": id,
}).Error("error executing remote procedure.")
os.Exit(1)
}
fmt.Printf("%s\n", string(json))
},
}
)
func init() {
miscCmd.AddCommand(miscRpcCmd)
}