-
Notifications
You must be signed in to change notification settings - Fork 1
/
echo.go
80 lines (70 loc) · 2 KB
/
echo.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
package commands
import (
"encoding/json"
"fmt"
"os"
"github.com/assetcloud/chain/rpc/jsonclient"
echotypes "github.com/assetcloud/plugin/plugin/dapp/echo/types/echo"
"github.com/spf13/cobra"
)
// EchoCmd 本执行器的命令行初始化总入口
func EchoCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "echo",
Short: "echo commandline interface",
Args: cobra.MinimumNArgs(1),
}
cmd.AddCommand(
QueryCmd(), // 查询消息记录
// 如果有其它命令,在这里加入
)
return cmd
}
// QueryCmd query 命令
func QueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Short: "query message history",
Run: queryMesage,
}
addPingPangFlags(cmd)
return cmd
}
func addPingPangFlags(cmd *cobra.Command) {
// type参数,指定查询的消息类型,为uint32类型,默认值为1,通过-t参数指定
cmd.Flags().Uint32P("type", "t", 1, "message type, 1:ping 2:pang")
//cmd.MarkFlagRequired("type")
// message参数,执行消息内容,为string类型,默认值为空,通过-m参数制定
cmd.Flags().StringP("message", "m", "", "message content")
cmd.MarkFlagRequired("message")
}
func queryMesage(cmd *cobra.Command, args []string) {
// 这个是命令行的默认参数,可以制定调用哪一个服务地址
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
echoType, _ := cmd.Flags().GetUint32("type")
msg, _ := cmd.Flags().GetString("message")
// 创建RPC客户端,调用我们实现的QueryPing服务接口
client, err := jsonclient.NewJSONClient(rpcLaddr)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
// 初始化查询参数结构
var action = &echotypes.Query{Msg: msg}
if echoType != 1 {
fmt.Fprintln(os.Stderr, "not support")
return
}
var result echotypes.QueryResult
err = client.Call("echo.QueryPing", action, &result)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
data, err := json.MarshalIndent(result, "", " ")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Println(string(data))
}