From d25c23c3b43c66b3b91d7f7c1096831b5d1fbdc1 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Thu, 6 Jul 2017 10:54:41 -0400 Subject: [PATCH] Fix some forbiddens in cli Original commit: elastic/x-pack-elasticsearch@3dc96c7279512bcd32bc6ca5a2f5224323717f19 --- .../org/elasticsearch/xpack/sql/cli/Cli.java | 16 +++++++++++----- .../xpack/sql/cli/CliIntegrationTestCase.java | 6 ++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/sql/cli/src/main/java/org/elasticsearch/xpack/sql/cli/Cli.java b/sql/cli/src/main/java/org/elasticsearch/xpack/sql/cli/Cli.java index c8a68b1a09f..db77b1d482c 100644 --- a/sql/cli/src/main/java/org/elasticsearch/xpack/sql/cli/Cli.java +++ b/sql/cli/src/main/java/org/elasticsearch/xpack/sql/cli/Cli.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.sql.cli; import org.elasticsearch.xpack.sql.cli.net.client.CliHttpClient; +import org.elasticsearch.xpack.sql.net.client.SuppressForbidden; import org.elasticsearch.xpack.sql.net.client.util.IOUtils; import org.jline.keymap.BindingReader; import org.jline.reader.EndOfFileException; @@ -37,12 +38,17 @@ public class Cli { Cli console = new Cli(new CliConfiguration("localhost:9200/_cli", new Properties()), term); console.run(); } catch (FatalException e) { - new PrintWriter(term.output()).println(e.getMessage()); - System.exit(1); + term.writer().println(e.getMessage()); + } } } + @SuppressForbidden(reason = "CLI application") + private static void terminateWithError() { + System.exit(1); + } + private final Terminal term; private final BindingReader bindingReader; private final Keys keys; @@ -134,7 +140,7 @@ public class Cli { executeCommand(line, out); } } catch (RuntimeException ex) { - ex.printStackTrace(); + // NOCOMMIT we should probably be able to throw these exceptions, if just for testing AttributedStringBuilder asb = new AttributedStringBuilder(); asb.append("Communication error [", BOLD.foreground(RED)); asb.append(ex.getMessage(), DEFAULT.boldOff().italic().foreground(YELLOW)); @@ -194,11 +200,11 @@ public class Cli { } static class FatalException extends RuntimeException { - public FatalException(String message, Throwable cause) { + FatalException(String message, Throwable cause) { super(message, cause); } - public FatalException(String message) { + FatalException(String message) { super(message); } } diff --git a/sql/cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliIntegrationTestCase.java b/sql/cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliIntegrationTestCase.java index 76d26542e36..fc0de370e0f 100644 --- a/sql/cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliIntegrationTestCase.java +++ b/sql/cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliIntegrationTestCase.java @@ -39,10 +39,12 @@ import org.junit.BeforeClass; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintWriter; import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; @@ -134,9 +136,9 @@ public class CliIntegrationTestCase extends ESRestTestCase { @Before public void startCli() throws IOException { PipedInputStream terminalIn = new PipedInputStream(); - out = new PrintWriter(new PipedOutputStream(terminalIn), true); + out = new PrintWriter(new OutputStreamWriter(new PipedOutputStream(terminalIn), StandardCharsets.UTF_8), true); PipedOutputStream terminalOut = new PipedOutputStream(); - in = new BufferedReader(new InputStreamReader(new PipedInputStream(terminalOut))); + in = new BufferedReader(new InputStreamReader(new PipedInputStream(terminalOut), StandardCharsets.UTF_8)); Terminal terminal = TerminalBuilder.builder() .system(false) .jansi(false)