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:
nickfelt 2017-02-06 09:48:33 -08:00 committed by Ben McIlwain
parent 6fd3592a54
commit 2a32f9048e

View file

@ -14,6 +14,8 @@
package google.registry.tools;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Ascii;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
@ -55,6 +57,10 @@ class CommandUtilities {
/** Prompts for yes/no input using promptText, defaulting to no. */
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");
}
}