This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 361
/
bytomcli.go
229 lines (181 loc) · 6.25 KB
/
bytomcli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package commands
import (
"fmt"
"os"
"regexp"
"github.com/spf13/cobra"
"github.com/bytom/util"
)
// bytomcli usage template
var usageTemplate = `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:
{{range .Commands}}{{if (and .IsAvailableCommand (.Name | WalletDisable))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}
available with wallet enable:
{{range .Commands}}{{if (and .IsAvailableCommand (.Name | WalletEnable))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
// commandError is an error used to signal different error situations in command handling.
type commandError struct {
s string
userError bool
}
func (c commandError) Error() string {
return c.s
}
func (c commandError) isUserError() bool {
return c.userError
}
func newUserError(a ...interface{}) commandError {
return commandError{s: fmt.Sprintln(a...), userError: true}
}
func newSystemError(a ...interface{}) commandError {
return commandError{s: fmt.Sprintln(a...), userError: false}
}
func newSystemErrorF(format string, a ...interface{}) commandError {
return commandError{s: fmt.Sprintf(format, a...), userError: false}
}
// Catch some of the obvious user errors from Cobra.
// We don't want to show the usage message for every error.
// The below may be to generic. Time will show.
var userErrorRegexp = regexp.MustCompile("argument|flag|shorthand")
func isUserError(err error) bool {
if cErr, ok := err.(commandError); ok && cErr.isUserError() {
return true
}
return userErrorRegexp.MatchString(err.Error())
}
// BytomcliCmd is Bytomcli's root command.
// Every other command attached to BytomcliCmd is a child command to it.
var BytomcliCmd = &cobra.Command{
Use: "bytomcli",
Short: "Bytomcli is a commond line client for bytom core (a.k.a. bytomd)",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.SetUsageTemplate(usageTemplate)
cmd.Usage()
}
},
}
// Execute adds all child commands to the root command BytomcliCmd and sets flags appropriately.
func Execute() {
AddCommands()
AddTemplateFunc()
if _, err := BytomcliCmd.ExecuteC(); err != nil {
os.Exit(util.ErrLocalExe)
}
}
// AddCommands adds child commands to the root command BytomcliCmd.
func AddCommands() {
BytomcliCmd.AddCommand(createAccessTokenCmd)
BytomcliCmd.AddCommand(listAccessTokenCmd)
BytomcliCmd.AddCommand(deleteAccessTokenCmd)
BytomcliCmd.AddCommand(checkAccessTokenCmd)
BytomcliCmd.AddCommand(createAccountCmd)
BytomcliCmd.AddCommand(deleteAccountCmd)
BytomcliCmd.AddCommand(listAccountsCmd)
BytomcliCmd.AddCommand(createAccountReceiverCmd)
BytomcliCmd.AddCommand(listAddressesCmd)
BytomcliCmd.AddCommand(validateAddressCmd)
BytomcliCmd.AddCommand(listPubKeysCmd)
BytomcliCmd.AddCommand(createAssetCmd)
BytomcliCmd.AddCommand(getAssetCmd)
BytomcliCmd.AddCommand(listAssetsCmd)
BytomcliCmd.AddCommand(updateAssetAliasCmd)
BytomcliCmd.AddCommand(getTransactionCmd)
BytomcliCmd.AddCommand(listTransactionsCmd)
BytomcliCmd.AddCommand(getUnconfirmedTransactionCmd)
BytomcliCmd.AddCommand(listUnconfirmedTransactionsCmd)
BytomcliCmd.AddCommand(decodeRawTransactionCmd)
BytomcliCmd.AddCommand(listUnspentOutputsCmd)
BytomcliCmd.AddCommand(listBalancesCmd)
BytomcliCmd.AddCommand(rescanWalletCmd)
BytomcliCmd.AddCommand(walletInfoCmd)
BytomcliCmd.AddCommand(buildTransactionCmd)
BytomcliCmd.AddCommand(signTransactionCmd)
BytomcliCmd.AddCommand(submitTransactionCmd)
BytomcliCmd.AddCommand(estimateTransactionGasCmd)
BytomcliCmd.AddCommand(getBlockCountCmd)
BytomcliCmd.AddCommand(getBlockHashCmd)
BytomcliCmd.AddCommand(getBlockCmd)
BytomcliCmd.AddCommand(getBlockHeaderCmd)
BytomcliCmd.AddCommand(getDifficultyCmd)
BytomcliCmd.AddCommand(getHashRateCmd)
BytomcliCmd.AddCommand(createKeyCmd)
BytomcliCmd.AddCommand(deleteKeyCmd)
BytomcliCmd.AddCommand(listKeysCmd)
BytomcliCmd.AddCommand(resetKeyPwdCmd)
BytomcliCmd.AddCommand(checkKeyPwdCmd)
BytomcliCmd.AddCommand(signMsgCmd)
BytomcliCmd.AddCommand(verifyMsgCmd)
BytomcliCmd.AddCommand(decodeProgCmd)
BytomcliCmd.AddCommand(createTransactionFeedCmd)
BytomcliCmd.AddCommand(listTransactionFeedsCmd)
BytomcliCmd.AddCommand(deleteTransactionFeedCmd)
BytomcliCmd.AddCommand(getTransactionFeedCmd)
BytomcliCmd.AddCommand(updateTransactionFeedCmd)
BytomcliCmd.AddCommand(isMiningCmd)
BytomcliCmd.AddCommand(setMiningCmd)
BytomcliCmd.AddCommand(netInfoCmd)
BytomcliCmd.AddCommand(gasRateCmd)
BytomcliCmd.AddCommand(versionCmd)
}
// AddTemplateFunc adds usage template to the root command BytomcliCmd.
func AddTemplateFunc() {
walletEnableCmd := []string{
createAccountCmd.Name(),
listAccountsCmd.Name(),
deleteAccountCmd.Name(),
createAccountReceiverCmd.Name(),
listAddressesCmd.Name(),
validateAddressCmd.Name(),
listPubKeysCmd.Name(),
createAssetCmd.Name(),
getAssetCmd.Name(),
listAssetsCmd.Name(),
updateAssetAliasCmd.Name(),
createKeyCmd.Name(),
deleteKeyCmd.Name(),
listKeysCmd.Name(),
resetKeyPwdCmd.Name(),
checkKeyPwdCmd.Name(),
signMsgCmd.Name(),
buildTransactionCmd.Name(),
signTransactionCmd.Name(),
getTransactionCmd.Name(),
listTransactionsCmd.Name(),
listUnspentOutputsCmd.Name(),
listBalancesCmd.Name(),
rescanWalletCmd.Name(),
walletInfoCmd.Name(),
}
cobra.AddTemplateFunc("WalletEnable", func(cmdName string) bool {
for _, name := range walletEnableCmd {
if name == cmdName {
return true
}
}
return false
})
cobra.AddTemplateFunc("WalletDisable", func(cmdName string) bool {
for _, name := range walletEnableCmd {
if name == cmdName {
return false
}
}
return true
})
}