mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 07:57:13 +02:00
Fix CommandUtilities to fail gracefully if stdin is unavailable
Right now, it just NPEs, which is harder to debug. Also make it handle end-of-input more cleanly by assuming that means a negative response. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=146674937
This commit is contained in:
parent
6fd3592a54
commit
2a32f9048e
1 changed files with 7 additions and 1 deletions
|
@ -14,6 +14,8 @@
|
||||||
|
|
||||||
package google.registry.tools;
|
package google.registry.tools;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
|
||||||
import com.google.common.base.Ascii;
|
import com.google.common.base.Ascii;
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
@ -55,6 +57,10 @@ class CommandUtilities {
|
||||||
|
|
||||||
/** Prompts for yes/no input using promptText, defaulting to no. */
|
/** Prompts for yes/no input using promptText, defaulting to no. */
|
||||||
static boolean promptForYes(String promptText) {
|
static boolean promptForYes(String promptText) {
|
||||||
return Ascii.toUpperCase(System.console().readLine(promptText + " (y/N): ")).startsWith("Y");
|
checkState(
|
||||||
|
System.console() != null, "Unable to access stdin (are you running with bazel run?)");
|
||||||
|
String input = System.console().readLine(promptText + " (y/N): ");
|
||||||
|
// Null represents end-of-file (e.g. ^-D) so interpret that as a negative response.
|
||||||
|
return input != null && Ascii.toUpperCase(input).startsWith("Y");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue