github.com/turingchain2020/turingchain@v1.1.21/system/dapp/manage/commands/config.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package commands 管理插件命令 6 package commands 7 8 import ( 9 "encoding/hex" 10 "fmt" 11 "os" 12 13 "github.com/turingchain2020/turingchain/util" 14 15 "github.com/turingchain2020/turingchain/rpc/jsonclient" 16 rpctypes "github.com/turingchain2020/turingchain/rpc/types" 17 pty "github.com/turingchain2020/turingchain/system/dapp/manage/types" 18 "github.com/turingchain2020/turingchain/types" 19 "github.com/spf13/cobra" 20 ) 21 22 // ConfigCmd config command 23 func ConfigCmd() *cobra.Command { 24 cmd := &cobra.Command{ 25 Use: "config", 26 Short: "Configuration", 27 Args: cobra.MinimumNArgs(1), 28 } 29 30 cmd.AddCommand( 31 ConfigTxCmd(), 32 QueryConfigCmd(), 33 ) 34 35 return cmd 36 } 37 38 // ConfigTxCmd config transaction 39 func ConfigTxCmd() *cobra.Command { 40 cmd := &cobra.Command{ 41 Use: "config_tx", 42 Short: "set system config", 43 Run: configTx, 44 } 45 addConfigTxFlags(cmd) 46 return cmd 47 } 48 49 func addConfigTxFlags(cmd *cobra.Command) { 50 cmd.Flags().StringP("config_key", "c", "", "config key string") 51 cmd.MarkFlagRequired("config_key") 52 53 cmd.Flags().StringP("operation", "o", "", "adding or deletion operation") 54 cmd.MarkFlagRequired("operation") 55 56 cmd.Flags().StringP("value", "v", "", "operating object") 57 cmd.MarkFlagRequired("value") 58 59 } 60 61 func configTx(cmd *cobra.Command, args []string) { 62 title, _ := cmd.Flags().GetString("title") 63 cfg := types.GetCliSysParam(title) 64 paraName, _ := cmd.Flags().GetString("paraName") 65 key, _ := cmd.Flags().GetString("config_key") 66 op, _ := cmd.Flags().GetString("operation") 67 opAddr, _ := cmd.Flags().GetString("value") 68 69 v := &types.ModifyConfig{Key: key, Op: op, Value: opAddr, Addr: ""} 70 modify := &pty.ManageAction{ 71 Ty: pty.ManageActionModifyConfig, 72 Value: &pty.ManageAction_Modify{Modify: v}, 73 } 74 tx := &types.Transaction{Payload: types.Encode(modify)} 75 var err error 76 tx, err = types.FormatTx(cfg, util.GetParaExecName(paraName, "manage"), tx) 77 if err != nil { 78 fmt.Fprintln(os.Stderr, err) 79 return 80 } 81 txHex := types.Encode(tx) 82 fmt.Println(hex.EncodeToString(txHex)) 83 } 84 85 // QueryConfigCmd query config 86 func QueryConfigCmd() *cobra.Command { 87 cmd := &cobra.Command{ 88 Use: "query", 89 Short: "Query config item", 90 Run: queryConfig, 91 } 92 addQueryConfigFlags(cmd) 93 return cmd 94 } 95 96 func addQueryConfigFlags(cmd *cobra.Command) { 97 cmd.Flags().StringP("key", "k", "", "key string") 98 cmd.MarkFlagRequired("key") 99 } 100 101 func queryConfig(cmd *cobra.Command, args []string) { 102 rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") 103 paraName, _ := cmd.Flags().GetString("paraName") 104 key, _ := cmd.Flags().GetString("key") 105 req := &types.ReqString{ 106 Data: key, 107 } 108 var params rpctypes.Query4Jrpc 109 params.Execer = util.GetParaExecName(paraName, "manage") 110 params.FuncName = "GetConfigItem" 111 params.Payload = types.MustPBToJSON(req) 112 113 var res types.ReplyConfig 114 ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.Query", params, &res) 115 ctx.Run() 116 }