Skip to content

Commit 966c706

Browse files
committed
Fix piping for commands needin wallet unlock
1 parent f1b6275 commit 966c706

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

cmd/common/prompts.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ package common
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/AlecAivazis/survey/v2"
78
"github.com/spf13/cobra"
89
)
910

11+
// StdioOpts returns survey options for using stderr for prompts.
12+
// This ensures prompts are visible even when stdout is piped.
13+
var StdioOpts = survey.WithStdio(os.Stdin, os.Stderr, os.Stderr)
14+
1015
var (
1116
// PromptPassphrase is the standard passphrase prompt.
1217
PromptPassphrase = &survey.Password{
@@ -27,12 +32,12 @@ var (
2732
// Confirm asks the user for confirmation and aborts when rejected.
2833
func Confirm(msg, abortMsg string) {
2934
if answerYes {
30-
fmt.Printf("? %s Yes\n", msg)
35+
fmt.Fprintf(os.Stderr, "? %s Yes\n", msg)
3136
return
3237
}
3338

3439
var proceed bool
35-
err := survey.AskOne(&survey.Confirm{Message: msg}, &proceed)
40+
err := survey.AskOne(&survey.Confirm{Message: msg}, &proceed, StdioOpts)
3641
cobra.CheckErr(err)
3742
if !proceed {
3843
cobra.CheckErr(abortMsg)
@@ -61,7 +66,7 @@ func AskNewPassphrase() string {
6166
},
6267
},
6368
}
64-
err := survey.Ask(questions, &answers)
69+
err := survey.Ask(questions, &answers, StdioOpts)
6570
cobra.CheckErr(err)
6671

6772
return answers.Passphrase

cmd/common/wallet.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package common
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67

78
"github.com/AlecAivazis/survey/v2"
@@ -61,9 +62,10 @@ func LoadAccount(cfg *config.Config, name string) wallet.Account {
6162
var passphrase string
6263
if af.RequiresPassphrase() && !answerYes {
6364
// Ask for passphrase to decrypt the account.
64-
fmt.Printf("Unlock your account.\n")
65+
// Use stderr for prompts so they work when stdout is piped.
66+
fmt.Fprintln(os.Stderr, "Unlock your account.")
6567

66-
err = survey.AskOne(PromptPassphrase, &passphrase)
68+
err = survey.AskOne(PromptPassphrase, &passphrase, StdioOpts)
6769
cobra.CheckErr(err)
6870
}
6971

0 commit comments

Comments
 (0)