Skip to content

Commit 384b13d

Browse files
committed
Fix piping for commands needin wallet unlock
1 parent f1b6275 commit 384b13d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+92
-43
lines changed

cmd/common/helpers.go

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

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

67
"github.com/spf13/cobra"
78

@@ -11,6 +12,16 @@ import (
1112
"github.com/oasisprotocol/cli/config"
1213
)
1314

15+
// Warnf prints a message to stderr with formatting.
16+
func Warnf(format string, args ...interface{}) {
17+
fmt.Fprintf(os.Stderr, format+"\n", args...)
18+
}
19+
20+
// Warn prints a message to stderr.
21+
func Warn(msg string) {
22+
fmt.Fprintln(os.Stderr, msg)
23+
}
24+
1425
// CheckForceErr treats error as warning, if --force is provided.
1526
func CheckForceErr(err interface{}) {
1627
// No error.
@@ -20,7 +31,7 @@ func CheckForceErr(err interface{}) {
2031

2132
// --force is provided.
2233
if IsForce() {
23-
fmt.Printf("Warning: %s\nProceeding by force as requested\n", err)
34+
Warnf("Warning: %s\nProceeding by force as requested", err)
2435
return
2536
}
2637

cmd/common/prompts.go

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

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

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

11+
// SurveyStdio is the standard survey option to direct prompts to stderr.
12+
var SurveyStdio = survey.WithStdio(os.Stdin, os.Stderr, os.Stderr)
13+
14+
// Ask wraps survey.AskOne while forcing prompts to stderr.
15+
func Ask(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error {
16+
return survey.AskOne(p, response, append([]survey.AskOpt{SurveyStdio}, opts...)...)
17+
}
18+
19+
// AskMulti wraps survey.Ask while forcing prompts to stderr.
20+
func AskMulti(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error {
21+
return survey.Ask(qs, response, append([]survey.AskOpt{SurveyStdio}, opts...)...)
22+
}
23+
1024
var (
1125
// PromptPassphrase is the standard passphrase prompt.
1226
PromptPassphrase = &survey.Password{
@@ -27,12 +41,12 @@ var (
2741
// Confirm asks the user for confirmation and aborts when rejected.
2842
func Confirm(msg, abortMsg string) {
2943
if answerYes {
30-
fmt.Printf("? %s Yes\n", msg)
44+
fmt.Fprintf(os.Stderr, "? %s Yes\n", msg)
3145
return
3246
}
3347

3448
var proceed bool
35-
err := survey.AskOne(&survey.Confirm{Message: msg}, &proceed)
49+
err := Ask(&survey.Confirm{Message: msg}, &proceed)
3650
cobra.CheckErr(err)
3751
if !proceed {
3852
cobra.CheckErr(abortMsg)
@@ -61,7 +75,7 @@ func AskNewPassphrase() string {
6175
},
6276
},
6377
}
64-
err := survey.Ask(questions, &answers)
78+
err := AskMulti(questions, &answers)
6579
cobra.CheckErr(err)
6680

6781
return answers.Passphrase

cmd/common/wallet.go

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

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

7-
"github.com/AlecAivazis/survey/v2"
88
ethCommon "github.com/ethereum/go-ethereum/common"
99
"github.com/spf13/cobra"
1010

@@ -61,9 +61,10 @@ func LoadAccount(cfg *config.Config, name string) wallet.Account {
6161
var passphrase string
6262
if af.RequiresPassphrase() && !answerYes {
6363
// Ask for passphrase to decrypt the account.
64-
fmt.Printf("Unlock your account.\n")
64+
// Use stderr for prompts so they work when stdout is piped.
65+
fmt.Fprintln(os.Stderr, "Unlock your account.")
6566

66-
err = survey.AskOne(PromptPassphrase, &passphrase)
67+
err = Ask(PromptPassphrase, &passphrase)
6768
cobra.CheckErr(err)
6869
}
6970

cmd/network/governance/show.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,11 @@ var (
362362
// Try to figure out the human readable names for all the entities.
363363
fromRegistry, err := metadata.EntitiesFromRegistry(ctx)
364364
if err != nil {
365-
fmt.Println()
366-
fmt.Printf("Warning: failed to query metadata registry: %v", err)
367-
fmt.Println()
365+
common.Warnf("\nWarning: failed to query metadata registry: %v\n", err)
368366
}
369367
fromOasisscan, err := metadata.EntitiesFromOasisscan(ctx)
370368
if err != nil {
371-
fmt.Println()
372-
fmt.Printf("Warning: failed to query oasisscan: %v", err)
373-
fmt.Println()
369+
common.Warnf("\nWarning: failed to query oasisscan: %v\n", err)
374370
}
375371

376372
getName := func(addr staking.Address) string {

cmd/network/network.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
88

9+
"github.com/oasisprotocol/cli/cmd/common"
910
"github.com/oasisprotocol/cli/cmd/network/governance"
1011
)
1112

@@ -41,7 +42,7 @@ func networkDetailsFromSurvey(net *config.Network) {
4142
Symbol string
4243
Decimals uint8
4344
}{}
44-
err := survey.Ask(questions, &answers)
45+
err := common.AskMulti(questions, &answers)
4546
cobra.CheckErr(err)
4647

4748
net.Description = answers.Description

cmd/network/remove.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var rmCmd = &cobra.Command{
2424
}
2525

2626
if !common.GetAnswerYes() && len(net.ParaTimes.All) > 0 {
27-
fmt.Printf("WARNING: Network '%s' contains %d ParaTimes.\n", name, len(net.ParaTimes.All))
27+
common.Warnf("WARNING: Network '%s' contains %d ParaTimes.", name, len(net.ParaTimes.All))
2828
common.Confirm("Are you sure you want to remove the network?", "not removing network")
2929
}
3030

cmd/network/trust.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ WARNING:
6666
fmt.Println("Trust height: ", trust.Height)
6767
fmt.Println("Trust hash: ", trust.Hash)
6868
fmt.Println()
69-
fmt.Println("WARNING: Cannot be trusted unless the CLI is connected to the RPC endpoint you control.")
69+
common.Warn("WARNING: Cannot be trusted unless the CLI is connected to the RPC endpoint you control.")
7070
}
7171

7272
return nil

cmd/paratime/add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var (
8686
},
8787
}
8888

89-
err := survey.Ask(questions, &paratimeInfo)
89+
err := common.AskMulti(questions, &paratimeInfo)
9090
cobra.CheckErr(err)
9191
}
9292

cmd/paratime/statistics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ var statsCmd = &cobra.Command{
384384
if err != nil {
385385
// Non-fatal, this is informative and gathering stats is time
386386
// consuming.
387-
fmt.Printf("\nWarning: failed to query metadata registry: %v\n", err)
387+
common.Warnf("\nWarning: failed to query metadata registry: %v", err)
388388
}
389389

390390
stats.prepareEntitiesOutput(entityMetadataLookup)

cmd/rofl/build/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func setupBuildEnv(deployment *buildRofl.Deployment, npa *common.NPASelection) {
312312
trustRoot, err := fetchTrustRoot(npa, deployment.TrustRoot)
313313
if deployment.Debug && err != nil {
314314
// Trust root is not mandatory for debug builds.
315-
fmt.Printf("WARNING: no trust root will be provided during compilation: %v\n", err)
315+
common.Warnf("WARNING: no trust root will be provided during compilation: %v", err)
316316
return
317317
}
318318
cobra.CheckErr(err)

0 commit comments

Comments
 (0)