Skip to content

Commit 8e2f509

Browse files
authored
Add passwordFlag(-P) and yesFlag(-y) for ioctl (iotexproject#1369)
1 parent 45591bc commit 8e2f509

11 files changed

+58
-17
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ require (
5454
go.uber.org/zap v1.10.0
5555
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734
5656
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c
57+
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421 // indirect
5758
golang.org/x/sync v0.0.0-20190423024810-112230192c58
5859
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
5960
golang.org/x/tools v0.0.0-20190430194229-2d28432af7a5 // indirect

ioctl/cmd/action/action.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ var (
4141
nonceFlag = flag.NewUint64VarP("nonce", "n", 0, "set nonce (default using pending nonce)")
4242
signerFlag = flag.NewStringVarP("signer", "s", "", "choose a signing account")
4343
bytecodeFlag = flag.NewStringVarP("bytecode", "b", "", "set the byte code")
44+
yesFlag = flag.BoolVarP("assume-yes", "y", false, " answer yes for all confirmations")
45+
passwordFlag = flag.NewStringVarP("password", "P", "", "input password for account")
4446
)
4547

4648
// ActionCmd represents the action command
@@ -89,6 +91,8 @@ func registerWriteCommand(cmd *cobra.Command) {
8991
gasPriceFlag.RegisterCommand(cmd)
9092
signerFlag.RegisterCommand(cmd)
9193
nonceFlag.RegisterCommand(cmd)
94+
yesFlag.RegisterCommand(cmd)
95+
passwordFlag.RegisterCommand(cmd)
9296
}
9397

9498
// gasPriceInRau returns the suggest gas price
@@ -187,15 +191,18 @@ func sendAction(elp action.Envelope, signer string) error {
187191
return err
188192
}
189193
prvKey, err = crypto.HexStringToPrivateKey(prvKeyOrPassword)
190-
} else {
194+
} else if passwordFlag.Value() == "" {
191195
fmt.Printf("Enter password #%s:\n", signer)
192196
prvKeyOrPassword, err = util.ReadSecretFromStdin()
193197
if err != nil {
194198
log.L().Error("failed to get password", zap.Error(err))
195199
return err
196200
}
197-
prvKey, err = account.KsAccountToPrivateKey(signer, prvKeyOrPassword)
201+
} else {
202+
prvKeyOrPassword = passwordFlag.Value().(string)
198203
}
204+
prvKey, err = account.KsAccountToPrivateKey(signer, prvKeyOrPassword)
205+
199206
if err != nil {
200207
return err
201208
}
@@ -215,13 +222,15 @@ func sendAction(elp action.Envelope, signer string) error {
215222
if err != nil {
216223
return err
217224
}
218-
var confirm string
219-
fmt.Println("\n" + actionInfo + "\n" +
220-
"Please confirm your action.\n" +
221-
"Type 'YES' to continue, quit for anything else.")
222-
fmt.Scanf("%s", &confirm)
223-
if confirm != "YES" && confirm != "yes" {
224-
return nil
225+
if yesFlag.Value() == false {
226+
var confirm string
227+
fmt.Println("\n" + actionInfo + "\n" +
228+
"Please confirm your action.\n" +
229+
"Type 'YES' to continue, quit for anything else.")
230+
fmt.Scanf("%s", &confirm)
231+
if confirm != "YES" && confirm != "yes" {
232+
return nil
233+
}
225234
}
226235
fmt.Println()
227236
return sendRaw(selp)

ioctl/cmd/action/actionclaim.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
// actionClaimCmd represents the action claim command
1717
var actionClaimCmd = &cobra.Command{
18-
Use: "claim AMOUNT_IOTX [DATA] [-s SIGNER] [-l GAS_LIMIT] [-p GASPRICE]",
18+
Use: "claim AMOUNT_IOTX [DATA] [-s SIGNER] [-l GAS_LIMIT] [-p GASPRICE] [-P PASSWORD] [-y]",
1919
Short: "Claim rewards from rewarding fund",
2020
Args: cobra.RangeArgs(1, 2),
2121
RunE: func(cmd *cobra.Command, args []string) error {

ioctl/cmd/action/actiondeploy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
// actionDeployCmd represents the action deploy command
1818
var actionDeployCmd = &cobra.Command{
19-
Use: "deploy [AMOUNT_IOTX] [-s SIGNER] -b BYTE_CODE [-l GAS_LIMIT] [-p GAS_PRICE]",
19+
Use: "deploy [AMOUNT_IOTX] [-s SIGNER] -b BYTE_CODE [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
2020
Short: "Deploy smart contract on IoTeX blockchain",
2121
Args: cobra.MaximumNArgs(0),
2222
RunE: func(cmd *cobra.Command, args []string) error {

ioctl/cmd/action/actiondeposit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
// actionDepositCmd represents the action deposit command
1717
var actionDepositCmd = &cobra.Command{
18-
Use: "deposit AMOUNT_IOTX [DATA] [-s SIGNER] [-l GAS_LIMIT] [-p GASPRICE]",
18+
Use: "deposit AMOUNT_IOTX [DATA] [-s SIGNER] [-l GAS_LIMIT] [-p GASPRICE] [-P PASSWORD] [-y]",
1919
Short: "Deposit rewards from rewarding fund",
2020
Args: cobra.RangeArgs(1, 2),
2121
RunE: func(cmd *cobra.Command, args []string) error {

ioctl/cmd/action/actioninvoke.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// actionInvokeCmd represents the action invoke command
1818
var actionInvokeCmd = &cobra.Command{
1919
Use: "invoke (ALIAS|CONTRACT_ADDRESS) [AMOUNT_IOTX]" +
20-
" [-s SIGNER] -b BYTE_CODE [-l GAS_LIMIT] [-p GAS_PRICE]",
20+
" [-s SIGNER] -b BYTE_CODE [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
2121
Short: "Invoke smart contract on IoTeX blockchain",
2222
Args: cobra.RangeArgs(1, 2),
2323
RunE: func(cmd *cobra.Command, args []string) error {

ioctl/cmd/action/actiontransfer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
// actionTransferCmd represents the action transfer command
2121
var actionTransferCmd = &cobra.Command{
2222
Use: "transfer (ALIAS|RECIPIENT_ADDRESS) AMOUNT_IOTX [DATA]" +
23-
" [-s SIGNER] [-l GAS_LIMIT] [-p GAS_PRICE]",
23+
" [-s SIGNER] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
2424
Short: "Transfer tokens on IoTeX blokchain",
2525
Args: cobra.RangeArgs(2, 3),
2626
RunE: func(cmd *cobra.Command, args []string) error {

ioctl/cmd/action/xrc20approve.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// xrc20ApproveCmd could config target address limited amount
1818
var xrc20ApproveCmd = &cobra.Command{
1919
Use: "approve (ALIAS|SPENDER_ADDRESS) (XRC20_AMOUNT)" +
20-
" -c ALIAS|CONTRACT_ADDRESS [-s SIGNER] [-l GAS_LIMIT]",
20+
" -c ALIAS|CONTRACT_ADDRESS [-s SIGNER] [-l GAS_LIMIT] [-P PASSWORD] [-y]",
2121
Short: "Allow spender to withdraw from your account, multiple times, up to the amount",
2222
Args: cobra.ExactArgs(2),
2323
RunE: func(cmd *cobra.Command, args []string) error {

ioctl/cmd/action/xrc20transfer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// xrc20TransferCmd could do transfer action
1818
var xrc20TransferCmd = &cobra.Command{
1919
Use: "transfer (ALIAS|TARGET_ADDRESS) AMOUNT" +
20-
" -c ALIAS|CONTRACT_ADDRESS [-l GAS_LIMIT] [-s SIGNER] [-p GAS_PRICE]",
20+
" -c ALIAS|CONTRACT_ADDRESS [-l GAS_LIMIT] [-s SIGNER] [-p GAS_PRICE] [-P PASSWORD] [-y]",
2121
Short: "Transfer token to the target address",
2222
Args: cobra.ExactArgs(2),
2323
RunE: func(cmd *cobra.Command, args []string) error {

ioctl/cmd/action/xrc20transferfrom.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
// xrc20TransferFromCmd could transfer from owner address to target address
1616
var xrc20TransferFromCmd = &cobra.Command{
1717
Use: "transferFrom (ALIAS|OWNER_ADDRESS) (ALIAS|RECIPIENT_ADDRESS) AMOUNT" +
18-
" -c (ALIAS|CONTRACT_ADDRESS) [-s SIGNER] [-l GAS_LIMIT] [-p GAS_PRICE]",
18+
" -c (ALIAS|CONTRACT_ADDRESS) [-s SIGNER] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
1919
Short: "Send amount of tokens from owner address to target address",
2020
Args: cobra.ExactArgs(3),
2121
RunE: func(cmd *cobra.Command, args []string) error {

ioctl/flag/flag.go

+31
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ type (
3434
value uint64
3535
defaultValue uint64
3636
}
37+
38+
boolVarP struct {
39+
flagBase
40+
value bool
41+
defaultValue bool
42+
}
3743
)
3844

3945
func (f *flagBase) MarkFlagRequired(cmd *cobra.Command) {
@@ -93,3 +99,28 @@ func (f *uint64VarP) RegisterCommand(cmd *cobra.Command) {
9399
func (f *uint64VarP) Value() interface{} {
94100
return f.value
95101
}
102+
103+
// BoolVarP creates a new stringVarP flag
104+
func BoolVarP(
105+
label string,
106+
shortLabel string,
107+
defaultValue bool,
108+
description string,
109+
) Flag {
110+
return &boolVarP{
111+
flagBase: flagBase{
112+
label: label,
113+
shortLabel: shortLabel,
114+
description: description,
115+
},
116+
defaultValue: defaultValue,
117+
}
118+
}
119+
120+
func (f *boolVarP) Value() interface{} {
121+
return f.value
122+
}
123+
124+
func (f *boolVarP) RegisterCommand(cmd *cobra.Command) {
125+
cmd.Flags().BoolVarP(&f.value, f.label, f.shortLabel, f.defaultValue, f.description)
126+
}

0 commit comments

Comments
 (0)