OSDN Git Service

Test (#272)
[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(updateAccountTagsCmd)
85         BytomcliCmd.AddCommand(createAccountReceiverCmd)
86         BytomcliCmd.AddCommand(createAccountAddressCmd)
87
88         BytomcliCmd.AddCommand(createAssetCmd)
89         BytomcliCmd.AddCommand(listAssetsCmd)
90         BytomcliCmd.AddCommand(updateAssetTagsCmd)
91
92         BytomcliCmd.AddCommand(listTransactionsCmd)
93         BytomcliCmd.AddCommand(listUnspentOutputsCmd)
94         BytomcliCmd.AddCommand(listBalancesCmd)
95
96         BytomcliCmd.AddCommand(buildTransactionCmd)
97         BytomcliCmd.AddCommand(signTransactionCmd)
98         BytomcliCmd.AddCommand(submitTransactionCmd)
99         BytomcliCmd.AddCommand(signSubTransactionCmd)
100
101         BytomcliCmd.AddCommand(blockHeightCmd)
102         BytomcliCmd.AddCommand(blockHashCmd)
103         BytomcliCmd.AddCommand(getBlockByHashCmd)
104         BytomcliCmd.AddCommand(getBlockHeaderByHashCmd)
105         BytomcliCmd.AddCommand(getBlockTransactionsCountByHashCmd)
106         BytomcliCmd.AddCommand(getBlockByHeightCmd)
107         BytomcliCmd.AddCommand(getBlockTransactionsCountByHeightCmd)
108
109         BytomcliCmd.AddCommand(createKeyCmd)
110         BytomcliCmd.AddCommand(deleteKeyCmd)
111         BytomcliCmd.AddCommand(listKeysCmd)
112         BytomcliCmd.AddCommand(exportPrivateCmd)
113         BytomcliCmd.AddCommand(importPrivateCmd)
114
115         BytomcliCmd.AddCommand(isMiningCmd)
116
117         BytomcliCmd.AddCommand(netInfoCmd)
118         BytomcliCmd.AddCommand(netListeningCmd)
119         BytomcliCmd.AddCommand(peerCountCmd)
120         BytomcliCmd.AddCommand(netSyncingCmd)
121
122         BytomcliCmd.AddCommand(gasRateCmd)
123
124         BytomcliCmd.AddCommand(createTransactionFeedCmd)
125         BytomcliCmd.AddCommand(listTransactionFeedsCmd)
126         BytomcliCmd.AddCommand(deleteTransactionFeedCmd)
127         BytomcliCmd.AddCommand(getTransactionFeedCmd)
128         BytomcliCmd.AddCommand(updateTransactionFeedCmd)
129
130         BytomcliCmd.AddCommand(versionCmd)
131 }
132
133