From 77ec7641fb05c45fa86fe04a603891c8630190b3 Mon Sep 17 00:00:00 2001 From: praveenkrishna Date: Thu, 13 Jun 2019 23:07:58 +0530 Subject: [PATCH] Acquire password in cli when its stdout is a pipe --- .../main/java/io/prestosql/cli/Console.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/presto-cli/src/main/java/io/prestosql/cli/Console.java b/presto-cli/src/main/java/io/prestosql/cli/Console.java index 2088a0e61e70c..c591a47df7a31 100644 --- a/presto-cli/src/main/java/io/prestosql/cli/Console.java +++ b/presto-cli/src/main/java/io/prestosql/cli/Console.java @@ -26,6 +26,7 @@ import io.prestosql.client.ClientSelectedRole; import io.prestosql.client.ClientSession; import io.prestosql.sql.parser.StatementSplitter; +import jline.console.ConsoleReader; import jline.console.history.FileHistory; import jline.console.history.History; import jline.console.history.MemoryHistory; @@ -169,14 +170,20 @@ private String getPassword() } java.io.Console console = System.console(); - if (console == null) { - throw new RuntimeException("No console from which to read password"); + if (console != null) { + char[] password = console.readPassword("Password: "); + if (password != null) { + return new String(password); + } + return ""; + } + try { + ConsoleReader consoleReader = new ConsoleReader(System.in, System.err); + return consoleReader.readLine("Password: ", (char) 0); } - char[] password = console.readPassword("Password: "); - if (password != null) { - return new String(password); + catch (IOException exception) { + throw new UncheckedIOException("Failed to read password from console", exception); } - return ""; } private static void runConsole(QueryRunner queryRunner, AtomicBoolean exiting)