OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / cmd / vaporcli / commands / util.go
1 package commands
2
3 import (
4         stdjson "encoding/json"
5         "os"
6         "time"
7
8         jww "github.com/spf13/jwalterweatherman"
9
10         "github.com/vapor/crypto/ed25519/chainkd"
11         "github.com/vapor/encoding/json"
12         chainjson "github.com/vapor/encoding/json"
13         "github.com/vapor/util"
14 )
15
16 // accountIns is used for account related request.
17 type accountIns struct {
18         RootXPubs   []chainkd.XPub `json:"root_xpubs"`
19         Quorum      int            `json:"quorum"`
20         Alias       string         `json:"alias"`
21         AccessToken string         `json:"access_token"`
22 }
23
24 // assetIns is used for asset related request.
25 type assetIns struct {
26         RootXPubs       []chainkd.XPub         `json:"root_xpubs"`
27         Quorum          int                    `json:"quorum"`
28         Alias           string                 `json:"alias"`
29         Definition      map[string]interface{} `json:"definition"`
30         IssuanceProgram chainjson.HexBytes     `json:"issuance_program"`
31         AccessToken     string                 `json:"access_token"`
32 }
33
34 type requestQuery struct {
35         Filter       string        `json:"filter,omitempty"`
36         FilterParams []interface{} `json:"filter_params,omitempty"`
37         SumBy        []string      `json:"sum_by,omitempty"`
38         PageSize     int           `json:"page_size"`
39         AscLongPoll  bool          `json:"ascending_with_long_poll,omitempty"`
40         Timeout      json.Duration `json:"timeout"`
41         After        string        `json:"after"`
42         StartTimeMS  uint64        `json:"start_time,omitempty"`
43         EndTimeMS    uint64        `json:"end_time,omitempty"`
44         TimestampMS  uint64        `json:"timestamp,omitempty"`
45         Type         string        `json:"type"`
46         Aliases      []string      `json:"aliases,omitempty"`
47 }
48
49 // txFeed
50 type txFeed struct {
51         Alias  string `json:"alias"`
52         Filter string `json:"filter,omitempty"`
53 }
54
55 type respArrayTxFeed struct {
56         Status string    `json:"status,omitempty"`
57         Msg    string    `json:"msg,omitempty"`
58         Data   []*txFeed `json:"data,omitempty"`
59 }
60
61 type respTxFeed struct {
62         Status string `json:"status,omitempty"`
63         Msg    string `json:"msg,omitempty"`
64         Data   txFeed `json:"data,omitempty"`
65 }
66
67 type accessToken struct {
68         ID      string    `json:"id,omitempty"`
69         Token   string    `json:"token,omitempty"`
70         Type    string    `json:"type,omitempty"`
71         Secret  string    `json:"secret,omitempty"`
72         Created time.Time `json:"created_at,omitempty"`
73 }
74
75 func printJSON(data interface{}) {
76         dataMap, ok := data.(map[string]interface{})
77         if ok != true {
78                 jww.ERROR.Println("invalid type assertion")
79                 os.Exit(util.ErrLocalParse)
80         }
81
82         rawData, err := stdjson.MarshalIndent(dataMap, "", "  ")
83         if err != nil {
84                 jww.ERROR.Println(err)
85                 os.Exit(util.ErrLocalParse)
86         }
87
88         jww.FEEDBACK.Println(string(rawData))
89 }
90
91 func printJSONList(data interface{}) {
92         dataList, ok := data.([]interface{})
93         if ok != true {
94                 jww.ERROR.Println("invalid type assertion")
95                 os.Exit(util.ErrLocalParse)
96         }
97
98         for idx, item := range dataList {
99                 jww.FEEDBACK.Println(idx, ":")
100                 rawData, err := stdjson.MarshalIndent(item, "", "  ")
101                 if err != nil {
102                         jww.ERROR.Println(err)
103                         os.Exit(util.ErrLocalParse)
104                 }
105
106                 jww.FEEDBACK.Println(string(rawData))
107         }
108 }