OSDN Git Service

add api calculate-transaction-gas
[bytom/bytom.git] / cmd / bytomcli / commands / bytomcli.go
1 package commands
2
3 import (
4         "fmt"
5         "os"
6         "regexp"
7
8         "github.com/spf13/cobra"
9
10         "github.com/bytom/util"
11 )
12
13 // commandError is an error used to signal different error situations in command handling.
14 type commandError struct {
15         s         string
16         userError bool
17 }
18
19 func (c commandError) Error() string {
20         return c.s
21 }
22
23 func (c commandError) isUserError() bool {
24         return c.userError
25 }
26
27 func newUserError(a ...interface{}) commandError {
28         return commandError{s: fmt.Sprintln(a...), userError: true}
29 }
30
31 func newSystemError(a ...interface{}) commandError {
32         return commandError{s: fmt.Sprintln(a...), userError: false}
33 }
34
35 func newSystemErrorF(format string, a ...interface{}) commandError {
36         return commandError{s: fmt.Sprintf(format, a...), userError: false}
37 }
38
39 // Catch some of the obvious user errors from Cobra.
40 // We don't want to show the usage message for every error.
41 // The below may be to generic. Time will show.
42 var userErrorRegexp = regexp.MustCompile("argument|flag|shorthand")
43
44 func isUserError(err error) bool {
45         if cErr, ok := err.(commandError); ok && cErr.isUserError() {
46                 return true
47         }
48
49         return userErrorRegexp.MatchString(err.Error())
50 }
51
52 // BytomcliCmd is Bytomcli's root command.
53 // Every other command attached to BytomcliCmd is a child command to it.
54 var BytomcliCmd = &cobra.Command{
55         Use:   "bytomcli",
56         Short: "Bytomcli is a commond line client for bytom core (a.k.a. bytomd)",
57         Run: func(cmd *cobra.Command, args []string) {
58                 if len(args) < 1 {
59                         cmd.Usage()
60                 }
61         },
62 }
63
64 // Execute adds all child commands to the root command BytomcliCmd and sets flags appropriately.
65 func Execute() {
66
67         AddCommands()
68
69         if _, err := BytomcliCmd.ExecuteC(); err != nil {
70                 os.Exit(util.ErrLocalExe)
71         }
72 }
73
74 // AddCommands adds child commands to the root command BytomcliCmd.
75 func AddCommands() {
76         BytomcliCmd.AddCommand(createAccessTokenCmd)
77         BytomcliCmd.AddCommand(listAccessTokenCmd)
78         BytomcliCmd.AddCommand(deleteAccessTokenCmd)
79         BytomcliCmd.AddCommand(checkAccessTokenCmd)
80
81         BytomcliCmd.AddCommand(createAccountCmd)
82         BytomcliCmd.AddCommand(deleteAccountCmd)
83         BytomcliCmd.AddCommand(listAccountsCmd)
84         BytomcliCmd.AddCommand(createAccountReceiverCmd)
85         BytomcliCmd.AddCommand(listAddressesCmd)
86         BytomcliCmd.AddCommand(validateAddressCmd)
87
88         BytomcliCmd.AddCommand(createAssetCmd)
89         BytomcliCmd.AddCommand(listAssetsCmd)
90         BytomcliCmd.AddCommand(updateAssetAliasCmd)
91
92         BytomcliCmd.AddCommand(getTransactionCmd)
93         BytomcliCmd.AddCommand(listTransactionsCmd)
94         BytomcliCmd.AddCommand(listUnspentOutputsCmd)
95         BytomcliCmd.AddCommand(listBalancesCmd)
96
97         BytomcliCmd.AddCommand(buildTransactionCmd)
98         BytomcliCmd.AddCommand(signTransactionCmd)
99         BytomcliCmd.AddCommand(submitTransactionCmd)
100         BytomcliCmd.AddCommand(calculateTransactionGasCmd)
101         BytomcliCmd.AddCommand(signSubTransactionCmd)
102
103         BytomcliCmd.AddCommand(getBlockCountCmd)
104         BytomcliCmd.AddCommand(blockHashCmd)
105         BytomcliCmd.AddCommand(getBlockCmd)
106         BytomcliCmd.AddCommand(getBlockHeaderByHashCmd)
107         BytomcliCmd.AddCommand(getBlockTransactionsCountByHashCmd)
108         BytomcliCmd.AddCommand(getBlockTransactionsCountByHeightCmd)
109
110         BytomcliCmd.AddCommand(createKeyCmd)
111         BytomcliCmd.AddCommand(deleteKeyCmd)
112         BytomcliCmd.AddCommand(listKeysCmd)
113         BytomcliCmd.AddCommand(resetKeyPwdCmd)
114         BytomcliCmd.AddCommand(exportPrivateCmd)
115         BytomcliCmd.AddCommand(importPrivateCmd)
116         BytomcliCmd.AddCommand(importKeyProgressCmd)
117
118         BytomcliCmd.AddCommand(isMiningCmd)
119
120         BytomcliCmd.AddCommand(netInfoCmd)
121         BytomcliCmd.AddCommand(netListeningCmd)
122         BytomcliCmd.AddCommand(peerCountCmd)
123         BytomcliCmd.AddCommand(netSyncingCmd)
124
125         BytomcliCmd.AddCommand(gasRateCmd)
126
127         BytomcliCmd.AddCommand(createTransactionFeedCmd)
128         BytomcliCmd.AddCommand(listTransactionFeedsCmd)
129         BytomcliCmd.AddCommand(deleteTransactionFeedCmd)
130         BytomcliCmd.AddCommand(getTransactionFeedCmd)
131         BytomcliCmd.AddCommand(updateTransactionFeedCmd)
132
133         BytomcliCmd.AddCommand(versionCmd)
134 }