-
Notifications
You must be signed in to change notification settings - Fork 253
/
config.go
116 lines (98 loc) · 2.86 KB
/
config.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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 commands 管理插件命令
package commands
import (
"encoding/hex"
"fmt"
"os"
"github.com/33cn/chain33/util"
"github.com/33cn/chain33/rpc/jsonclient"
rpctypes "github.com/33cn/chain33/rpc/types"
pty "github.com/33cn/chain33/system/dapp/manage/types"
"github.com/33cn/chain33/types"
"github.com/spf13/cobra"
)
// ConfigCmd config command
func ConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Configuration",
Args: cobra.MinimumNArgs(1),
}
cmd.AddCommand(
ConfigTxCmd(),
QueryConfigCmd(),
)
return cmd
}
// ConfigTxCmd config transaction
func ConfigTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config_tx",
Short: "set system config",
Run: configTx,
}
addConfigTxFlags(cmd)
return cmd
}
func addConfigTxFlags(cmd *cobra.Command) {
cmd.Flags().StringP("config_key", "c", "", "config key string")
cmd.MarkFlagRequired("config_key")
cmd.Flags().StringP("operation", "o", "", "adding or deletion operation")
cmd.MarkFlagRequired("operation")
cmd.Flags().StringP("value", "v", "", "operating object")
cmd.MarkFlagRequired("value")
}
func configTx(cmd *cobra.Command, args []string) {
title, _ := cmd.Flags().GetString("title")
cfg := types.GetCliSysParam(title)
paraName, _ := cmd.Flags().GetString("paraName")
key, _ := cmd.Flags().GetString("config_key")
op, _ := cmd.Flags().GetString("operation")
opAddr, _ := cmd.Flags().GetString("value")
v := &types.ModifyConfig{Key: key, Op: op, Value: opAddr, Addr: ""}
modify := &pty.ManageAction{
Ty: pty.ManageActionModifyConfig,
Value: &pty.ManageAction_Modify{Modify: v},
}
tx := &types.Transaction{Payload: types.Encode(modify)}
var err error
tx, err = types.FormatTx(cfg, util.GetParaExecName(paraName, "manage"), tx)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
txHex := types.Encode(tx)
fmt.Println(hex.EncodeToString(txHex))
}
// QueryConfigCmd query config
func QueryConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Short: "Query config item",
Run: queryConfig,
}
addQueryConfigFlags(cmd)
return cmd
}
func addQueryConfigFlags(cmd *cobra.Command) {
cmd.Flags().StringP("key", "k", "", "key string")
cmd.MarkFlagRequired("key")
}
func queryConfig(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
paraName, _ := cmd.Flags().GetString("paraName")
key, _ := cmd.Flags().GetString("key")
req := &types.ReqString{
Data: key,
}
var params rpctypes.Query4Jrpc
params.Execer = util.GetParaExecName(paraName, "manage")
params.FuncName = "GetConfigItem"
params.Payload = types.MustPBToJSON(req)
var res types.ReplyConfig
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
}