OSDN Git Service

rename (#465)
[bytom/vapor.git] / cmd / vaporcli / commands / vaporcli.go
1 package commands
2
3 import (
4         "fmt"
5         "os"
6         "regexp"
7
8         "github.com/spf13/cobra"
9
10         "github.com/bytom/vapor/util"
11 )
12
13 // vaporcli 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 // VaporCmd is Vaporcli's root command.
84 // Every other command attached to VaporcliCmd is a child command to it.
85 var VaporcliCmd = &cobra.Command{
86         Use:   "vaporcli",
87         Short: "Vaporcli is a commond line client for vapor (a.k.a. vapord)",
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 VaporcliCmd and sets flags appropriately.
97 func Execute() {
98
99         AddCommands()
100         AddTemplateFunc()
101
102         if _, err := VaporcliCmd.ExecuteC(); err != nil {
103                 os.Exit(util.ErrLocalExe)
104         }
105 }
106
107 // AddCommands adds child commands to the root command VaporcliCmd.
108 func AddCommands() {
109         VaporcliCmd.AddCommand(createAccessTokenCmd)
110         VaporcliCmd.AddCommand(listAccessTokenCmd)
111         VaporcliCmd.AddCommand(deleteAccessTokenCmd)
112         VaporcliCmd.AddCommand(checkAccessTokenCmd)
113
114         VaporcliCmd.AddCommand(createAccountCmd)
115         VaporcliCmd.AddCommand(deleteAccountCmd)
116         VaporcliCmd.AddCommand(listAccountsCmd)
117         VaporcliCmd.AddCommand(updateAccountAliasCmd)
118         VaporcliCmd.AddCommand(createAccountReceiverCmd)
119         VaporcliCmd.AddCommand(listAddressesCmd)
120         VaporcliCmd.AddCommand(validateAddressCmd)
121         VaporcliCmd.AddCommand(listPubKeysCmd)
122
123         VaporcliCmd.AddCommand(createAssetCmd)
124         VaporcliCmd.AddCommand(getAssetCmd)
125         VaporcliCmd.AddCommand(listAssetsCmd)
126         VaporcliCmd.AddCommand(updateAssetAliasCmd)
127
128         VaporcliCmd.AddCommand(getTransactionCmd)
129         VaporcliCmd.AddCommand(listTransactionsCmd)
130
131         VaporcliCmd.AddCommand(getUnconfirmedTransactionCmd)
132         VaporcliCmd.AddCommand(listUnconfirmedTransactionsCmd)
133         VaporcliCmd.AddCommand(decodeRawTransactionCmd)
134
135         VaporcliCmd.AddCommand(listUnspentOutputsCmd)
136         VaporcliCmd.AddCommand(listBalancesCmd)
137
138         VaporcliCmd.AddCommand(rescanWalletCmd)
139         VaporcliCmd.AddCommand(walletInfoCmd)
140
141         VaporcliCmd.AddCommand(buildTransactionCmd)
142         VaporcliCmd.AddCommand(signTransactionCmd)
143         VaporcliCmd.AddCommand(submitTransactionCmd)
144         VaporcliCmd.AddCommand(estimateTransactionGasCmd)
145
146         VaporcliCmd.AddCommand(getBlockCountCmd)
147         VaporcliCmd.AddCommand(getBlockHashCmd)
148         VaporcliCmd.AddCommand(getBlockCmd)
149         VaporcliCmd.AddCommand(getBlockHeaderCmd)
150         VaporcliCmd.AddCommand(getDifficultyCmd)
151         VaporcliCmd.AddCommand(getHashRateCmd)
152
153         VaporcliCmd.AddCommand(createKeyCmd)
154         VaporcliCmd.AddCommand(deleteKeyCmd)
155         VaporcliCmd.AddCommand(listKeysCmd)
156         VaporcliCmd.AddCommand(updateKeyAliasCmd)
157         VaporcliCmd.AddCommand(resetKeyPwdCmd)
158         VaporcliCmd.AddCommand(checkKeyPwdCmd)
159
160         VaporcliCmd.AddCommand(signMsgCmd)
161         VaporcliCmd.AddCommand(verifyMsgCmd)
162         VaporcliCmd.AddCommand(decodeProgCmd)
163
164         VaporcliCmd.AddCommand(createTransactionFeedCmd)
165         VaporcliCmd.AddCommand(listTransactionFeedsCmd)
166         VaporcliCmd.AddCommand(deleteTransactionFeedCmd)
167         VaporcliCmd.AddCommand(getTransactionFeedCmd)
168         VaporcliCmd.AddCommand(updateTransactionFeedCmd)
169
170         VaporcliCmd.AddCommand(isMiningCmd)
171         VaporcliCmd.AddCommand(setMiningCmd)
172
173         VaporcliCmd.AddCommand(netInfoCmd)
174         VaporcliCmd.AddCommand(gasRateCmd)
175
176         VaporcliCmd.AddCommand(versionCmd)
177 }
178
179 // AddTemplateFunc adds usage template to the root command VaporcliCmd.
180 func AddTemplateFunc() {
181         walletEnableCmd := []string{
182                 createAccountCmd.Name(),
183                 listAccountsCmd.Name(),
184                 deleteAccountCmd.Name(),
185                 updateAccountAliasCmd.Name(),
186                 createAccountReceiverCmd.Name(),
187                 listAddressesCmd.Name(),
188                 validateAddressCmd.Name(),
189                 listPubKeysCmd.Name(),
190
191                 createAssetCmd.Name(),
192                 getAssetCmd.Name(),
193                 listAssetsCmd.Name(),
194                 updateAssetAliasCmd.Name(),
195
196                 createKeyCmd.Name(),
197                 deleteKeyCmd.Name(),
198                 listKeysCmd.Name(),
199                 resetKeyPwdCmd.Name(),
200                 checkKeyPwdCmd.Name(),
201                 signMsgCmd.Name(),
202
203                 buildTransactionCmd.Name(),
204                 signTransactionCmd.Name(),
205
206                 getTransactionCmd.Name(),
207                 listTransactionsCmd.Name(),
208                 listUnspentOutputsCmd.Name(),
209                 listBalancesCmd.Name(),
210
211                 rescanWalletCmd.Name(),
212                 walletInfoCmd.Name(),
213         }
214
215         cobra.AddTemplateFunc("WalletEnable", func(cmdName string) bool {
216                 for _, name := range walletEnableCmd {
217                         if name == cmdName {
218                                 return true
219                         }
220                 }
221                 return false
222         })
223
224         cobra.AddTemplateFunc("WalletDisable", func(cmdName string) bool {
225                 for _, name := range walletEnableCmd {
226                         if name == cmdName {
227                                 return false
228                         }
229                 }
230                 return true
231         })
232 }