OSDN Git Service

Add txfeed commands to cobra & Change accesstoken rpc to jsend format (#140)
[bytom/bytom.git] / cmd / cobra / commands / txfeed.go
1 package commands
2
3 import (
4         "context"
5         "fmt"
6
7         "github.com/spf13/cobra"
8         jww "github.com/spf13/jwalterweatherman"
9 )
10
11 type txFeed struct {
12         Alias  string `json:"alias"`
13         Filter string `json:"filter,omitempty"`
14 }
15
16 type respArrayTxFeed struct {
17         Status string    `json:"status,omitempty"`
18         Msg    string    `json:"msg,omitempty"`
19         Data   []*txFeed `json:"data,omitempty"`
20 }
21
22 type respTxFeed struct {
23         Status string `json:"status,omitempty"`
24         Msg    string `json:"msg,omitempty"`
25         Data   txFeed `json:"data,omitempty"`
26 }
27
28 var createTransactionFeedCmd = &cobra.Command{
29         Use:   "create-transaction-feed",
30         Short: "Create a transaction feed filter",
31         Run: func(cmd *cobra.Command, args []string) {
32                 if len(args) != 2 {
33                         jww.ERROR.Println("create-transaction-feed needs 2 args")
34                         return
35                 }
36
37                 var in txFeed
38                 in.Alias = args[0]
39                 in.Filter = args[1]
40
41                 var response interface{}
42
43                 client := mustRPCClient()
44                 client.Call(context.Background(), "/create-transaction-feed", &in, &response)
45
46                 var rawresp resp
47
48                 if err := parseresp(response, &rawresp); err != nil {
49                         jww.ERROR.Println("parse response error")
50                         return
51                 }
52
53                 if rawresp.Status == "success" {
54                         jww.FEEDBACK.Printf("%v\n", rawresp.Data)
55                         return
56                 }
57
58                 if rawresp.Status == "error" {
59                         jww.ERROR.Println(rawresp.Msg)
60                         return
61                 }
62         },
63 }
64
65 var listTransactionFeedsCmd = &cobra.Command{
66         Use:   "list-transaction-feeds",
67         Short: "list all of transaction feeds",
68         Run: func(cmd *cobra.Command, args []string) {
69                 if len(args) != 0 {
70                         jww.ERROR.Println("list-transaction-feeds takes no args")
71                         return
72                 }
73
74                 var in requestQuery
75                 var response interface{}
76
77                 client := mustRPCClient()
78                 client.Call(context.Background(), "/list-transaction-feeds", in, &response)
79
80                 var rawresp respArrayTxFeed
81                 if err := parseresp(response, &rawresp); err != nil {
82                         jww.ERROR.Println("parse response error")
83                         return
84                 }
85
86                 if rawresp.Status == "success" {
87                         for i, v := range rawresp.Data {
88                                 fmt.Println(i, v.Alias, v.Filter)
89                         }
90                         return
91                 }
92
93                 if rawresp.Status == "error" {
94                         jww.ERROR.Println(rawresp.Msg)
95                         return
96                 }
97         },
98 }
99
100 var deleteTransactionFeedCmd = &cobra.Command{
101         Use:   "delete-transaction-feed",
102         Short: "Delete a transaction feed filter",
103         Run: func(cmd *cobra.Command, args []string) {
104                 if len(args) != 1 {
105                         jww.ERROR.Println("delete-transaction-feed needs 1 args")
106                         return
107                 }
108
109                 var in txFeed
110                 in.Alias = args[0]
111
112                 var response interface{}
113
114                 client := mustRPCClient()
115                 client.Call(context.Background(), "/delete-transaction-feed", &in, &response)
116
117                 var rawresp resp
118                 if err := parseresp(response, &rawresp); err != nil {
119                         jww.ERROR.Println("parse response error")
120                         return
121                 }
122
123                 if rawresp.Status == "success" {
124                         jww.FEEDBACK.Printf("%v\n", rawresp.Data)
125                         return
126                 }
127
128                 if rawresp.Status == "error" {
129                         jww.ERROR.Println(rawresp.Msg)
130                         return
131                 }
132         },
133 }
134
135 var getTransactionFeedCmd = &cobra.Command{
136         Use:   "get-transaction-feed",
137         Short: "get a transaction feed by alias",
138         Run: func(cmd *cobra.Command, args []string) {
139                 if len(args) != 1 {
140                         jww.ERROR.Println("get-transaction-feed needs 1 args")
141                         return
142                 }
143
144                 var in txFeed
145                 in.Alias = args[0]
146                 var response interface{}
147
148                 client := mustRPCClient()
149                 client.Call(context.Background(), "/get-transaction-feed", &in, &response)
150
151                 var rawresp respTxFeed
152
153                 if err := parseresp(response, &rawresp); err != nil {
154                         jww.ERROR.Println("parse response error")
155                         return
156                 }
157
158                 if rawresp.Status == "success" {
159                         fmt.Println(rawresp.Data)
160                         return
161                 }
162
163                 if rawresp.Status == "error" {
164                         jww.ERROR.Println(rawresp.Msg)
165                         return
166                 }
167         },
168 }
169
170 var updateTransactionFeedCmd = &cobra.Command{
171         Use:   "update-transaction-feed",
172         Short: "Update transaction feed",
173         Run: func(cmd *cobra.Command, args []string) {
174                 if len(args) != 2 {
175                         jww.ERROR.Println("update-transaction-feed needs 2 args")
176                         return
177                 }
178
179                 var in txFeed
180                 in.Alias = args[0]
181                 in.Filter = args[1]
182
183                 var response interface{}
184
185                 client := mustRPCClient()
186                 client.Call(context.Background(), "/update-transaction-feed", &in, &response)
187
188                 var rawresp resp
189                 if err := parseresp(response, &rawresp); err != nil {
190                         jww.ERROR.Println("parse response error")
191                         return
192                 }
193
194                 if rawresp.Status == "success" {
195                         jww.FEEDBACK.Printf("%v\n", rawresp.Data)
196                         return
197                 }
198
199                 if rawresp.Status == "error" {
200                         jww.ERROR.Println(rawresp.Msg)
201                         return
202                 }
203         },
204 }