From 2a32f9048ee35d0262214cd528d5bc8418f8c643 Mon Sep 17 00:00:00 2001 From: nickfelt Date: Mon, 6 Feb 2017 09:48:33 -0800 Subject: [PATCH] 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 --- java/google/registry/tools/CommandUtilities.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/java/google/registry/tools/CommandUtilities.java b/java/google/registry/tools/CommandUtilities.java index 46aa0d5b7..409c46565 100644 --- a/java/google/registry/tools/CommandUtilities.java +++ b/java/google/registry/tools/CommandUtilities.java @@ -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"); } }