Skip to content

Commit

Permalink
Merge pull request #3143 from oasisprotocol/yawning/fix/isatty
Browse files Browse the repository at this point in the history
go/oasis-node: Fix the interactive prompt to be correct
  • Loading branch information
Yawning authored Jul 30, 2020
2 parents 8162e5f + 54fadb0 commit a4be9b5
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 27 deletions.
1 change: 1 addition & 0 deletions .changelog/3143.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/oasis-node: Fix the interactive prompt to be correct
42 changes: 26 additions & 16 deletions go/oasis-node/cmd/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,24 +278,34 @@ func ExportEntity(signerBackend, entityDir string) error {
return err
}

// GetUserConfirmation scans the input for user's confirmation.
// GetUserConfirmation displays the prompt, and scans the input for
// the user's confirmation, until the user either explicitly confirms
// or rejects the prompt.
//
// If the user's response is not recognized, it prompts the user again.
func GetUserConfirmation() bool {
var response string

_, err := fmt.Scanln(&response)
if err != nil && err.Error() != "unexpected newline" {
rootLog.Error("Error reading from line", "err", err)
// Note: If standard input is not a tty, this will omit displaying
// the prompt, and assume the user entered yes.
func GetUserConfirmation(prompt string) bool {
if !Isatty(os.Stdin.Fd()) {
return true
}

switch strings.ToLower(response) {
case "y", "yes":
return true
case "n", "no":
return false
default:
fmt.Printf("Unrecognized response: '%s'. Please, type (y)es or (n)o: ", response)
return GetUserConfirmation()
fmt.Printf("%s", prompt)

var response string
for {
_, err := fmt.Scanln(&response)
if err != nil && err.Error() != "unexpected newline" {
rootLog.Error("Error reading from line", "err", err)
continue
}

switch strings.ToLower(response) {
case "y", "yes":
return true
case "n", "no":
return false
default:
fmt.Printf("Unrecognized response: '%s'. Please, type (y)es or (n)o: ", response)
}
}
}
3 changes: 1 addition & 2 deletions go/oasis-node/cmd/common/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ func SignAndSaveTx(ctx context.Context, tx *transaction.Transaction) {
tx.PrettyPrint(ctx, " ", os.Stdout)

if !cmdFlags.AssumeYes() && cmdSigner.Backend() == signerFile.SignerName {
fmt.Printf("\nAre you sure you want to continue? (y)es/(n)o ")
if !cmdCommon.GetUserConfirmation() {
if !cmdCommon.GetUserConfirmation("\nAre you sure you want to continue? (y)es/(n)o: ") {
os.Exit(1)
}
}
Expand Down
31 changes: 31 additions & 0 deletions go/oasis-node/cmd/common/isatty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package common

import (
"syscall"
"unsafe"
)

// Isatty returns true iff the provided file descriptor is a terminal.
func Isatty(fd uintptr) bool {
var attrs syscall.Termios

// This could examine the error more specifically to see if
// something really strange is going on since we expect it
// to return 0 or ENOTTY all the time, but, "the messed up
// thing the user passed in that makes it complain" also
// is not a tty.
//
// And yes, this is the standard way of doing this, see your
// libc implementation of choice.
_, _, errno := syscall.Syscall6(
syscall.SYS_IOCTL,
fd,
syscall.TCGETS,
uintptr(unsafe.Pointer(&attrs)),
0,
0,
0,
)

return errno == 0
}
1 change: 0 additions & 1 deletion go/oasis-test-runner/oasis/cli/keymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func (k *KeymanagerHelpers) GenUpdate(nonce uint64, polPath string, polSigPaths
"--" + cmdConsensus.CfgTxFeeAmount, strconv.Itoa(0), // TODO: Make fee configurable.
"--" + cmdConsensus.CfgTxFeeGas, strconv.Itoa(10000), // TODO: Make fee configurable.
"--" + cmdKM.CfgPolicyFile, polPath,
"--" + flags.CfgAssumeYes,
"--" + flags.CfgDebugDontBlameOasis,
"--" + cmdCommon.CfgDebugAllowTestKeys,
"--" + flags.CfgDebugTestEntity,
Expand Down
1 change: 0 additions & 1 deletion go/oasis-test-runner/oasis/cli/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ func (r *RegistryHelpers) GenerateRegisterRuntimeTx(
"--"+consensus.CfgTxFile, txPath,
"--"+consensus.CfgTxFeeAmount, strconv.Itoa(0), // TODO: Make fee configurable.
"--"+consensus.CfgTxFeeGas, strconv.Itoa(10000), // TODO: Make fee configurable.
"--"+flags.CfgAssumeYes,
"--"+flags.CfgDebugDontBlameOasis,
"--"+cmdCommon.CfgDebugAllowTestKeys,
"--"+flags.CfgDebugTestEntity,
Expand Down
2 changes: 0 additions & 2 deletions go/oasis-test-runner/scenario/e2e/registry_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,6 @@ func (sc *registryCLIImpl) genRegisterEntityTx(childEnv *env.Env, nonce int, txP
"--" + consensus.CfgTxFile, txPath,
"--" + consensus.CfgTxFeeAmount, strconv.Itoa(0),
"--" + consensus.CfgTxFeeGas, strconv.Itoa(feeGas),
"--" + flags.CfgAssumeYes,
"--" + flags.CfgDebugDontBlameOasis,
"--" + cmdCommon.CfgDebugAllowTestKeys,
"--" + cmdSigner.CfgSigner, fileSigner.SignerName,
Expand All @@ -578,7 +577,6 @@ func (sc *registryCLIImpl) genDeregisterEntityTx(childEnv *env.Env, nonce int, t
"--" + consensus.CfgTxFile, txPath,
"--" + consensus.CfgTxFeeAmount, strconv.Itoa(0),
"--" + consensus.CfgTxFeeGas, strconv.Itoa(feeGas),
"--" + flags.CfgAssumeYes,
"--" + flags.CfgDebugDontBlameOasis,
"--" + cmdCommon.CfgDebugAllowTestKeys,
"--" + cmdSigner.CfgSigner, fileSigner.SignerName,
Expand Down
5 changes: 0 additions & 5 deletions go/oasis-test-runner/scenario/e2e/stake_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,6 @@ func (sc *stakeCLIImpl) genTransferTx(childEnv *env.Env, amount int, nonce uint6
"--" + stake.CfgTransferDestination, dst.String(),
"--" + consensus.CfgTxFeeAmount, strconv.Itoa(feeAmount),
"--" + consensus.CfgTxFeeGas, strconv.Itoa(feeGas),
"--" + flags.CfgAssumeYes,
"--" + flags.CfgDebugDontBlameOasis,
"--" + flags.CfgDebugTestEntity,
"--" + common.CfgDebugAllowTestKeys,
Expand All @@ -782,7 +781,6 @@ func (sc *stakeCLIImpl) genBurnTx(childEnv *env.Env, amount int, nonce uint64, t
"--" + consensus.CfgTxFile, txPath,
"--" + consensus.CfgTxFeeAmount, strconv.Itoa(feeAmount),
"--" + consensus.CfgTxFeeGas, strconv.Itoa(feeGas),
"--" + flags.CfgAssumeYes,
"--" + flags.CfgDebugDontBlameOasis,
"--" + flags.CfgDebugTestEntity,
"--" + common.CfgDebugAllowTestKeys,
Expand All @@ -805,7 +803,6 @@ func (sc *stakeCLIImpl) genEscrowTx(childEnv *env.Env, amount int, nonce uint64,
"--" + stake.CfgEscrowAccount, escrow.String(),
"--" + consensus.CfgTxFeeAmount, strconv.Itoa(feeAmount),
"--" + consensus.CfgTxFeeGas, strconv.Itoa(feeGas),
"--" + flags.CfgAssumeYes,
"--" + flags.CfgDebugDontBlameOasis,
"--" + flags.CfgDebugTestEntity,
"--" + common.CfgDebugAllowTestKeys,
Expand All @@ -828,7 +825,6 @@ func (sc *stakeCLIImpl) genReclaimEscrowTx(childEnv *env.Env, shares int, nonce
"--" + stake.CfgEscrowAccount, escrow.String(),
"--" + consensus.CfgTxFeeAmount, strconv.Itoa(feeAmount),
"--" + consensus.CfgTxFeeGas, strconv.Itoa(feeGas),
"--" + flags.CfgAssumeYes,
"--" + flags.CfgDebugDontBlameOasis,
"--" + flags.CfgDebugTestEntity,
"--" + common.CfgDebugAllowTestKeys,
Expand All @@ -849,7 +845,6 @@ func (sc *stakeCLIImpl) genAmendCommissionScheduleTx(childEnv *env.Env, nonce in
"--" + consensus.CfgTxFile, txPath,
"--" + consensus.CfgTxFeeAmount, strconv.Itoa(feeAmount),
"--" + consensus.CfgTxFeeGas, strconv.Itoa(feeGas),
"--" + flags.CfgAssumeYes,
"--" + flags.CfgDebugDontBlameOasis,
"--" + flags.CfgDebugTestEntity,
"--" + common.CfgDebugAllowTestKeys,
Expand Down

0 comments on commit a4be9b5

Please sign in to comment.