8 "github.com/spf13/cobra"
10 "github.com/vapor/util"
13 // bytomcli usage template
14 var usageTemplate = `Usage:{{if .Runnable}}
15 {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
16 {{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
19 {{.NameAndAliases}}{{end}}{{if .HasExample}}
22 {{.Example}}{{end}}{{if .HasAvailableSubCommands}}
25 {{range .Commands}}{{if (and .IsAvailableCommand (.Name | WalletDisable))}}
26 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}
28 available with wallet enable:
29 {{range .Commands}}{{if (and .IsAvailableCommand (.Name | WalletEnable))}}
30 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
33 {{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
36 {{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
38 Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
39 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
41 Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
44 // commandError is an error used to signal different error situations in command handling.
45 type commandError struct {
50 func (c commandError) Error() string {
54 func (c commandError) isUserError() bool {
58 func newUserError(a ...interface{}) commandError {
59 return commandError{s: fmt.Sprintln(a...), userError: true}
62 func newSystemError(a ...interface{}) commandError {
63 return commandError{s: fmt.Sprintln(a...), userError: false}
66 func newSystemErrorF(format string, a ...interface{}) commandError {
67 return commandError{s: fmt.Sprintf(format, a...), userError: false}
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")
75 func isUserError(err error) bool {
76 if cErr, ok := err.(commandError); ok && cErr.isUserError() {
80 return userErrorRegexp.MatchString(err.Error())
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{
87 Short: "Bytomcli is a commond line client for bytom core (a.k.a. bytomd)",
88 Run: func(cmd *cobra.Command, args []string) {
90 cmd.SetUsageTemplate(usageTemplate)
96 // Execute adds all child commands to the root command BytomcliCmd and sets flags appropriately.
102 if _, err := BytomcliCmd.ExecuteC(); err != nil {
103 os.Exit(util.ErrLocalExe)
107 // AddCommands adds child commands to the root command BytomcliCmd.
109 BytomcliCmd.AddCommand(createAccessTokenCmd)
110 BytomcliCmd.AddCommand(listAccessTokenCmd)
111 BytomcliCmd.AddCommand(deleteAccessTokenCmd)
112 BytomcliCmd.AddCommand(checkAccessTokenCmd)
114 BytomcliCmd.AddCommand(createAccountCmd)
115 BytomcliCmd.AddCommand(deleteAccountCmd)
116 BytomcliCmd.AddCommand(listAccountsCmd)
117 BytomcliCmd.AddCommand(createAccountReceiverCmd)
118 BytomcliCmd.AddCommand(listAddressesCmd)
119 BytomcliCmd.AddCommand(validateAddressCmd)
120 BytomcliCmd.AddCommand(listPubKeysCmd)
122 BytomcliCmd.AddCommand(createAssetCmd)
123 BytomcliCmd.AddCommand(getAssetCmd)
124 BytomcliCmd.AddCommand(listAssetsCmd)
125 BytomcliCmd.AddCommand(updateAssetAliasCmd)
127 BytomcliCmd.AddCommand(getTransactionCmd)
128 BytomcliCmd.AddCommand(listTransactionsCmd)
130 BytomcliCmd.AddCommand(getUnconfirmedTransactionCmd)
131 BytomcliCmd.AddCommand(listUnconfirmedTransactionsCmd)
132 BytomcliCmd.AddCommand(decodeRawTransactionCmd)
134 BytomcliCmd.AddCommand(listUnspentOutputsCmd)
135 BytomcliCmd.AddCommand(listBalancesCmd)
137 BytomcliCmd.AddCommand(rescanWalletCmd)
138 BytomcliCmd.AddCommand(walletInfoCmd)
140 BytomcliCmd.AddCommand(buildTransactionCmd)
141 BytomcliCmd.AddCommand(signTransactionCmd)
142 BytomcliCmd.AddCommand(submitTransactionCmd)
143 BytomcliCmd.AddCommand(estimateTransactionGasCmd)
145 BytomcliCmd.AddCommand(getBlockCountCmd)
146 BytomcliCmd.AddCommand(getBlockHashCmd)
147 BytomcliCmd.AddCommand(getBlockCmd)
148 BytomcliCmd.AddCommand(getBlockHeaderCmd)
149 BytomcliCmd.AddCommand(getDifficultyCmd)
150 BytomcliCmd.AddCommand(getHashRateCmd)
152 BytomcliCmd.AddCommand(createKeyCmd)
153 BytomcliCmd.AddCommand(deleteKeyCmd)
154 BytomcliCmd.AddCommand(listKeysCmd)
155 BytomcliCmd.AddCommand(resetKeyPwdCmd)
156 BytomcliCmd.AddCommand(checkKeyPwdCmd)
158 BytomcliCmd.AddCommand(signMsgCmd)
159 BytomcliCmd.AddCommand(verifyMsgCmd)
160 BytomcliCmd.AddCommand(decodeProgCmd)
162 BytomcliCmd.AddCommand(createTransactionFeedCmd)
163 BytomcliCmd.AddCommand(listTransactionFeedsCmd)
164 BytomcliCmd.AddCommand(deleteTransactionFeedCmd)
165 BytomcliCmd.AddCommand(getTransactionFeedCmd)
166 BytomcliCmd.AddCommand(updateTransactionFeedCmd)
168 BytomcliCmd.AddCommand(isMiningCmd)
169 BytomcliCmd.AddCommand(setMiningCmd)
171 BytomcliCmd.AddCommand(netInfoCmd)
172 BytomcliCmd.AddCommand(gasRateCmd)
174 BytomcliCmd.AddCommand(versionCmd)
177 // AddTemplateFunc adds usage template to the root command BytomcliCmd.
178 func AddTemplateFunc() {
179 walletEnableCmd := []string{
180 createAccountCmd.Name(),
181 listAccountsCmd.Name(),
182 deleteAccountCmd.Name(),
183 createAccountReceiverCmd.Name(),
184 listAddressesCmd.Name(),
185 validateAddressCmd.Name(),
186 listPubKeysCmd.Name(),
188 createAssetCmd.Name(),
190 listAssetsCmd.Name(),
191 updateAssetAliasCmd.Name(),
196 resetKeyPwdCmd.Name(),
197 checkKeyPwdCmd.Name(),
200 buildTransactionCmd.Name(),
201 signTransactionCmd.Name(),
203 getTransactionCmd.Name(),
204 listTransactionsCmd.Name(),
205 listUnspentOutputsCmd.Name(),
206 listBalancesCmd.Name(),
208 rescanWalletCmd.Name(),
209 walletInfoCmd.Name(),
212 cobra.AddTemplateFunc("WalletEnable", func(cmdName string) bool {
213 for _, name := range walletEnableCmd {
221 cobra.AddTemplateFunc("WalletDisable", func(cmdName string) bool {
222 for _, name := range walletEnableCmd {