github.com/turingchain2020/turingchain@v1.1.21/util/cli/cli.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 cli 6 7 import ( 8 "fmt" 9 "net/http" 10 "os" 11 "strings" 12 13 "github.com/turingchain2020/turingchain/common/log" 14 "github.com/turingchain2020/turingchain/pluginmgr" 15 "github.com/turingchain2020/turingchain/rpc/jsonclient" 16 rpctypes "github.com/turingchain2020/turingchain/rpc/types" 17 "github.com/turingchain2020/turingchain/system/dapp/commands" 18 "github.com/turingchain2020/turingchain/types" 19 "github.com/spf13/cobra" 20 ) 21 22 //Run : 23 func Run(RPCAddr, ParaName, name string) { 24 // cli 命令只打印错误级别到控制台 25 log.SetLogLevel("error") 26 configPath := "" 27 for i, arg := range os.Args[:] { 28 if arg == "--conf" && i+1 <= len(os.Args)-1 { // --conf turingchain.toml 可以配置读入cli配置文件路径 29 configPath = os.Args[i+1] 30 break 31 } 32 if strings.HasPrefix(arg, "--conf=") { // --conf="turingchain.toml" 33 configPath = strings.TrimPrefix(arg, "--conf=") 34 break 35 } 36 } 37 if configPath == "" { 38 if name == "" { 39 configPath = "turingchain.toml" 40 } else { 41 configPath = name + ".toml" 42 } 43 } 44 45 exist, _ := pathExists(configPath) 46 var turingchainCfg *types.TuringchainConfig 47 if exist { 48 turingchainCfg = types.NewTuringchainConfig(types.ReadFile(configPath)) 49 } else { 50 cfgstring := types.GetDefaultCfgstring() 51 if ParaName != "" { 52 cfgstring = strings.Replace(cfgstring, "Title=\"local\"", fmt.Sprintf("Title=\"%s\"", ParaName), 1) 53 cfgstring = strings.Replace(cfgstring, "FixTime=false", "CoinSymbol=\"para\"", 1) 54 } 55 turingchainCfg = types.NewTuringchainConfig(cfgstring) 56 } 57 58 types.SetCliSysParam(turingchainCfg.GetTitle(), turingchainCfg) 59 60 rootCmd := &cobra.Command{ 61 Use: turingchainCfg.GetTitle() + "-cli", 62 Short: turingchainCfg.GetTitle() + " client tools", 63 } 64 65 closeCmd := &cobra.Command{ 66 Use: "close", 67 Short: "Close " + turingchainCfg.GetTitle(), 68 Run: func(cmd *cobra.Command, args []string) { 69 rpcLaddr, err := cmd.Flags().GetString("rpc_laddr") 70 if err != nil { 71 panic(err) 72 } 73 // rpc, _ := jsonrpc.NewJSONClient(rpcLaddr) 74 // rpc.Call("Turingchain.CloseQueue", nil, nil) 75 var res rpctypes.Reply 76 ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.CloseQueue", nil, &res) 77 ctx.Run() 78 }, 79 } 80 81 rootCmd.AddCommand( 82 commands.CertCmd(), 83 commands.AccountCmd(), 84 commands.BlockCmd(), 85 commands.CoinsCmd(), 86 commands.ExecCmd(), 87 commands.MempoolCmd(), 88 commands.NetCmd(), 89 commands.SeedCmd(), 90 commands.StatCmd(), 91 commands.TxCmd(), 92 commands.WalletCmd(), 93 commands.VersionCmd(), 94 commands.OneStepSendCmd(), 95 closeCmd, 96 commands.AssetCmd(), 97 ) 98 99 //test tls is enable 100 RPCAddr = testTLS(RPCAddr) 101 pluginmgr.AddCmd(rootCmd) 102 log.SetLogLevel("error") 103 turingchainCfg.S("RPCAddr", RPCAddr) 104 turingchainCfg.S("ParaName", ParaName) 105 rootCmd.PersistentFlags().String("rpc_laddr", turingchainCfg.GStr("RPCAddr"), "http url") 106 rootCmd.PersistentFlags().String("paraName", turingchainCfg.GStr("ParaName"), "parachain") 107 rootCmd.PersistentFlags().String("title", turingchainCfg.GetTitle(), "get title name") 108 rootCmd.PersistentFlags().MarkHidden("title") 109 rootCmd.PersistentFlags().String("conf", "", "cli config") 110 if err := rootCmd.Execute(); err != nil { 111 fmt.Println(err) 112 os.Exit(1) 113 } 114 } 115 116 func testTLS(RPCAddr string) string { 117 rpcaddr := RPCAddr 118 if !strings.HasPrefix(rpcaddr, "http://") { 119 return RPCAddr 120 } 121 // if http:// 122 if rpcaddr[len(rpcaddr)-1] != '/' { 123 rpcaddr += "/" 124 } 125 rpcaddr += "test" 126 /* #nosec */ 127 resp, err := http.Get(rpcaddr) 128 if err != nil { 129 return "https://" + RPCAddr[7:] 130 } 131 defer resp.Body.Close() 132 if resp.StatusCode == 200 { 133 return RPCAddr 134 } 135 return "https://" + RPCAddr[7:] 136 } 137 138 func pathExists(path string) (bool, error) { 139 _, err := os.Stat(path) 140 if err == nil { 141 return true, nil 142 } 143 if os.IsNotExist(err) { 144 return false, nil 145 } 146 return false, err 147 }