github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/cmd/bytomcli/commands/bytomcli.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "os" 6 "regexp" 7 8 "github.com/spf13/cobra" 9 10 "github.com/bytom/bytom/util" 11 ) 12 13 // bytomcli usage template 14 var usageTemplate = `Usage:{{if .Runnable}} 15 {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}} 16 {{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}} 17 18 Aliases: 19 {{.NameAndAliases}}{{end}}{{if .HasExample}} 20 21 Examples: 22 {{.Example}}{{end}}{{if .HasAvailableSubCommands}} 23 24 Available Commands: 25 {{range .Commands}}{{if (and .IsAvailableCommand (.Name | WalletDisable))}} 26 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}} 27 28 available with wallet enable: 29 {{range .Commands}}{{if (and .IsAvailableCommand (.Name | WalletEnable))}} 30 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}} 31 32 Flags: 33 {{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}} 34 35 Global Flags: 36 {{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}} 37 38 Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}} 39 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}} 40 41 Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}} 42 ` 43 44 // commandError is an error used to signal different error situations in command handling. 45 type commandError struct { 46 s string 47 userError bool 48 } 49 50 func (c commandError) Error() string { 51 return c.s 52 } 53 54 func (c commandError) isUserError() bool { 55 return c.userError 56 } 57 58 func newUserError(a ...interface{}) commandError { 59 return commandError{s: fmt.Sprintln(a...), userError: true} 60 } 61 62 func newSystemError(a ...interface{}) commandError { 63 return commandError{s: fmt.Sprintln(a...), userError: false} 64 } 65 66 func newSystemErrorF(format string, a ...interface{}) commandError { 67 return commandError{s: fmt.Sprintf(format, a...), userError: false} 68 } 69 70 // Catch some of the obvious user errors from Cobra. 71 // We don't want to show the usage message for every error. 72 // The below may be to generic. Time will show. 73 var userErrorRegexp = regexp.MustCompile("argument|flag|shorthand") 74 75 func isUserError(err error) bool { 76 if cErr, ok := err.(commandError); ok && cErr.isUserError() { 77 return true 78 } 79 80 return userErrorRegexp.MatchString(err.Error()) 81 } 82 83 // BytomcliCmd is Bytomcli's root command. 84 // Every other command attached to BytomcliCmd is a child command to it. 85 var BytomcliCmd = &cobra.Command{ 86 Use: "bytomcli", 87 Short: "Bytomcli is a commond line client for bytom core (a.k.a. bytomd)", 88 Run: func(cmd *cobra.Command, args []string) { 89 if len(args) < 1 { 90 cmd.SetUsageTemplate(usageTemplate) 91 cmd.Usage() 92 } 93 }, 94 } 95 96 // Execute adds all child commands to the root command BytomcliCmd and sets flags appropriately. 97 func Execute() { 98 99 AddCommands() 100 AddTemplateFunc() 101 102 if _, err := BytomcliCmd.ExecuteC(); err != nil { 103 os.Exit(util.ErrLocalExe) 104 } 105 } 106 107 // AddCommands adds child commands to the root command BytomcliCmd. 108 func AddCommands() { 109 BytomcliCmd.AddCommand(createAccessTokenCmd) 110 BytomcliCmd.AddCommand(listAccessTokenCmd) 111 BytomcliCmd.AddCommand(deleteAccessTokenCmd) 112 BytomcliCmd.AddCommand(checkAccessTokenCmd) 113 114 BytomcliCmd.AddCommand(createAccountCmd) 115 BytomcliCmd.AddCommand(deleteAccountCmd) 116 BytomcliCmd.AddCommand(listAccountsCmd) 117 BytomcliCmd.AddCommand(updateAccountAliasCmd) 118 BytomcliCmd.AddCommand(createAccountReceiverCmd) 119 BytomcliCmd.AddCommand(listAddressesCmd) 120 BytomcliCmd.AddCommand(validateAddressCmd) 121 BytomcliCmd.AddCommand(listPubKeysCmd) 122 123 BytomcliCmd.AddCommand(createAssetCmd) 124 BytomcliCmd.AddCommand(getAssetCmd) 125 BytomcliCmd.AddCommand(listAssetsCmd) 126 BytomcliCmd.AddCommand(updateAssetAliasCmd) 127 128 BytomcliCmd.AddCommand(getTransactionCmd) 129 BytomcliCmd.AddCommand(listTransactionsCmd) 130 131 BytomcliCmd.AddCommand(getUnconfirmedTransactionCmd) 132 BytomcliCmd.AddCommand(listUnconfirmedTransactionsCmd) 133 BytomcliCmd.AddCommand(decodeRawTransactionCmd) 134 135 BytomcliCmd.AddCommand(listUnspentOutputsCmd) 136 BytomcliCmd.AddCommand(listBalancesCmd) 137 138 BytomcliCmd.AddCommand(rescanWalletCmd) 139 BytomcliCmd.AddCommand(walletInfoCmd) 140 141 BytomcliCmd.AddCommand(buildTransactionCmd) 142 BytomcliCmd.AddCommand(signTransactionCmd) 143 BytomcliCmd.AddCommand(submitTransactionCmd) 144 BytomcliCmd.AddCommand(estimateTransactionGasCmd) 145 146 BytomcliCmd.AddCommand(getBlockCountCmd) 147 BytomcliCmd.AddCommand(getBlockHashCmd) 148 BytomcliCmd.AddCommand(getBlockCmd) 149 BytomcliCmd.AddCommand(getBlockHeaderCmd) 150 151 BytomcliCmd.AddCommand(createKeyCmd) 152 BytomcliCmd.AddCommand(deleteKeyCmd) 153 BytomcliCmd.AddCommand(listKeysCmd) 154 BytomcliCmd.AddCommand(updateKeyAliasCmd) 155 BytomcliCmd.AddCommand(resetKeyPwdCmd) 156 BytomcliCmd.AddCommand(checkKeyPwdCmd) 157 158 BytomcliCmd.AddCommand(signMsgCmd) 159 BytomcliCmd.AddCommand(verifyMsgCmd) 160 BytomcliCmd.AddCommand(decodeProgCmd) 161 162 BytomcliCmd.AddCommand(createTransactionFeedCmd) 163 BytomcliCmd.AddCommand(listTransactionFeedsCmd) 164 BytomcliCmd.AddCommand(deleteTransactionFeedCmd) 165 BytomcliCmd.AddCommand(getTransactionFeedCmd) 166 BytomcliCmd.AddCommand(updateTransactionFeedCmd) 167 168 BytomcliCmd.AddCommand(netInfoCmd) 169 BytomcliCmd.AddCommand(gasRateCmd) 170 171 BytomcliCmd.AddCommand(versionCmd) 172 } 173 174 // AddTemplateFunc adds usage template to the root command BytomcliCmd. 175 func AddTemplateFunc() { 176 walletEnableCmd := []string{ 177 createAccountCmd.Name(), 178 listAccountsCmd.Name(), 179 deleteAccountCmd.Name(), 180 updateAccountAliasCmd.Name(), 181 createAccountReceiverCmd.Name(), 182 listAddressesCmd.Name(), 183 validateAddressCmd.Name(), 184 listPubKeysCmd.Name(), 185 186 createAssetCmd.Name(), 187 getAssetCmd.Name(), 188 listAssetsCmd.Name(), 189 updateAssetAliasCmd.Name(), 190 191 createKeyCmd.Name(), 192 deleteKeyCmd.Name(), 193 listKeysCmd.Name(), 194 resetKeyPwdCmd.Name(), 195 checkKeyPwdCmd.Name(), 196 signMsgCmd.Name(), 197 198 buildTransactionCmd.Name(), 199 signTransactionCmd.Name(), 200 201 getTransactionCmd.Name(), 202 listTransactionsCmd.Name(), 203 listUnspentOutputsCmd.Name(), 204 listBalancesCmd.Name(), 205 206 rescanWalletCmd.Name(), 207 walletInfoCmd.Name(), 208 } 209 210 cobra.AddTemplateFunc("WalletEnable", func(cmdName string) bool { 211 for _, name := range walletEnableCmd { 212 if name == cmdName { 213 return true 214 } 215 } 216 return false 217 }) 218 219 cobra.AddTemplateFunc("WalletDisable", func(cmdName string) bool { 220 for _, name := range walletEnableCmd { 221 if name == cmdName { 222 return false 223 } 224 } 225 return true 226 }) 227 }