/
cli.go
99 lines (91 loc) · 2.29 KB
/
cli.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cli
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/PhenixChain/devchain/common/log"
"github.com/PhenixChain/devchain/pluginmgr"
"github.com/PhenixChain/devchain/rpc/jsonclient"
rpctypes "github.com/PhenixChain/devchain/rpc/types"
"github.com/PhenixChain/devchain/system/dapp/commands"
"github.com/PhenixChain/devchain/types"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: types.GetTitle() + "-cli",
Short: types.GetTitle() + " client tools",
}
var closeCmd = &cobra.Command{
Use: "close",
Short: "Close " + types.GetTitle(),
Run: func(cmd *cobra.Command, args []string) {
rpcLaddr, err := cmd.Flags().GetString("rpc_laddr")
if err != nil {
panic(err)
}
// rpc, _ := jsonrpc.NewJSONClient(rpcLaddr)
// rpc.Call("Chain33.CloseQueue", nil, nil)
var res rpctypes.Reply
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.CloseQueue", nil, &res)
ctx.Run()
},
}
func init() {
rootCmd.AddCommand(
commands.CertCmd(),
commands.AccountCmd(),
commands.BlockCmd(),
commands.CoinsCmd(),
commands.ExecCmd(),
commands.MempoolCmd(),
commands.NetCmd(),
commands.SeedCmd(),
commands.StatCmd(),
commands.TxCmd(),
commands.WalletCmd(),
commands.VersionCmd(),
commands.OneStepSendCmd(),
closeCmd,
commands.AssetCmd(),
)
}
func testTLS(RPCAddr string) string {
rpcaddr := RPCAddr
if !strings.HasPrefix(rpcaddr, "http://") {
return RPCAddr
}
// if http://
if rpcaddr[len(rpcaddr)-1] != '/' {
rpcaddr += "/"
}
rpcaddr += "test"
/* #nosec */
resp, err := http.Get(rpcaddr)
if err != nil {
return "https://" + RPCAddr[7:]
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
return RPCAddr
}
return "https://" + RPCAddr[7:]
}
//Run :
func Run(RPCAddr, ParaName string) {
//test tls is enable
RPCAddr = testTLS(RPCAddr)
pluginmgr.AddCmd(rootCmd)
log.SetLogLevel("error")
types.S("RPCAddr", RPCAddr)
types.S("ParaName", ParaName)
rootCmd.PersistentFlags().String("rpc_laddr", types.GStr("RPCAddr"), "http url")
rootCmd.PersistentFlags().String("paraName", types.GStr("ParaName"), "parachain")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}