OSDN Git Service

Merge pull request #651 from Bytom/crazy-miner
[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(signSubTransactionCmd)
101
102         BytomcliCmd.AddCommand(getBlockCountCmd)
103         BytomcliCmd.AddCommand(blockHashCmd)
104         BytomcliCmd.AddCommand(getBlockCmd)
105         BytomcliCmd.AddCommand(getBlockHeaderByHashCmd)
106         BytomcliCmd.AddCommand(getBlockTransactionsCountByHashCmd)
107         BytomcliCmd.AddCommand(getBlockTransactionsCountByHeightCmd)
108
109         BytomcliCmd.AddCommand(createKeyCmd)
110         BytomcliCmd.AddCommand(deleteKeyCmd)
111         BytomcliCmd.AddCommand(listKeysCmd)
112         BytomcliCmd.AddCommand(resetKeyPwdCmd)
113         BytomcliCmd.AddCommand(exportPrivateCmd)
114         BytomcliCmd.AddCommand(importPrivateCmd)
115         BytomcliCmd.AddCommand(importKeyProgressCmd)
116
117         BytomcliCmd.AddCommand(isMiningCmd)
118
119         BytomcliCmd.AddCommand(netInfoCmd)
120         BytomcliCmd.AddCommand(netListeningCmd)
121         BytomcliCmd.AddCommand(peerCountCmd)
122         BytomcliCmd.AddCommand(netSyncingCmd)
123
124         BytomcliCmd.AddCommand(gasRateCmd)
125
126         BytomcliCmd.AddCommand(createTransactionFeedCmd)
127         BytomcliCmd.AddCommand(listTransactionFeedsCmd)
128         BytomcliCmd.AddCommand(deleteTransactionFeedCmd)
129         BytomcliCmd.AddCommand(getTransactionFeedCmd)
130         BytomcliCmd.AddCommand(updateTransactionFeedCmd)
131
132         BytomcliCmd.AddCommand(versionCmd)
133 }