Remove format args from readText and readSecret

This commit is contained in:
Ryan Ernst 2016-02-04 02:03:17 -08:00
parent b80200a363
commit e97345984d
7 changed files with 21 additions and 41 deletions

View File

@ -52,6 +52,6 @@ public class HelpPrinter {
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
terminal.println("");
terminal.println(Terminal.Verbosity.SILENT, "");
}
}

View File

@ -22,9 +22,7 @@ package org.elasticsearch.common.cli;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.Charset;
import org.elasticsearch.common.SuppressForbidden;
@ -60,10 +58,10 @@ public abstract class Terminal {
}
/** Reads clear text from the terminal input. See {@link Console#readLine()}. */
public abstract String readText(String text, Object... args);
public abstract String readText(String prompt);
/** Reads password text from the terminal input. See {@link Console#readPassword()}}. */
public abstract char[] readSecret(String text, Object... args);
public abstract char[] readSecret(String prompt);
/** Print a message directly to the terminal. */
protected abstract void doPrint(String msg);
@ -75,7 +73,7 @@ public abstract class Terminal {
/** Prints a line to the terminal at {@code verbosity} level. */
public final void println(Verbosity verbosity, String msg) {
if (verbosity.ordinal() >= this.verbosity.ordinal()) {
if (this.verbosity.ordinal() >= verbosity.ordinal()) {
doPrint(msg + System.lineSeparator());
}
}
@ -95,13 +93,13 @@ public abstract class Terminal {
}
@Override
public String readText(String text, Object... args) {
return console.readLine(text, args);
public String readText(String prompt) {
return console.readLine("%s", prompt);
}
@Override
public char[] readSecret(String text, Object... args) {
return console.readPassword(text, args);
public char[] readSecret(String prompt) {
return console.readPassword("%s", prompt);
}
}
@ -115,7 +113,7 @@ public abstract class Terminal {
}
@Override
public String readText(String text, Object... args) {
public String readText(String text) {
doPrint(text);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
try {
@ -126,8 +124,8 @@ public abstract class Terminal {
}
@Override
public char[] readSecret(String text, Object... args) {
return readText(text, args).toCharArray();
public char[] readSecret(String text) {
return readText(text).toCharArray();
}
}
}

View File

@ -237,8 +237,8 @@ public class InternalSettingsPreparer {
}
if (secret) {
return new String(terminal.readSecret("Enter value for [" + key + "]: ", key));
return new String(terminal.readSecret("Enter value for [" + key + "]: "));
}
return terminal.readText("Enter value for [" + key + "]: ", key);
return terminal.readText("Enter value for [" + key + "]: ");
}
}

View File

@ -22,9 +22,6 @@ package org.elasticsearch.common.cli;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
/**
*
*/
public class TerminalTests extends CliToolTestCase {
public void testVerbosity() throws Exception {
CaptureOutputTerminal terminal = new CaptureOutputTerminal(Terminal.Verbosity.SILENT);
@ -50,8 +47,8 @@ public class TerminalTests extends CliToolTestCase {
private void assertPrinted(CaptureOutputTerminal logTerminal, Terminal.Verbosity verbosity, String text) {
logTerminal.println(verbosity, text);
assertThat(logTerminal.getTerminalOutput(), hasSize(1));
assertThat(logTerminal.getTerminalOutput(), hasItem(text));
assertEquals(1, logTerminal.getTerminalOutput().size());
assertTrue(logTerminal.getTerminalOutput().get(0).contains(text));
logTerminal.terminalOutput.clear();
}

View File

@ -81,22 +81,14 @@ public class InternalSettingsPreparerTests extends ESTestCase {
}
public void testReplacePromptPlaceholders() {
final List<String> replacedSecretProperties = new ArrayList<>();
final List<String> replacedTextProperties = new ArrayList<>();
final Terminal terminal = new CliToolTestCase.MockTerminal() {
@Override
public char[] readSecret(String message, Object... args) {
for (Object arg : args) {
replacedSecretProperties.add((String) arg);
}
public char[] readSecret(String message) {
return "replaced".toCharArray();
}
@Override
public String readText(String message, Object... args) {
for (Object arg : args) {
replacedTextProperties.add((String) arg);
}
public String readText(String message) {
return "text";
}
};
@ -112,8 +104,6 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put("replace_me", InternalSettingsPreparer.TEXT_PROMPT_VALUE);
Settings settings = InternalSettingsPreparer.prepareEnvironment(builder.build(), terminal).settings();
assertThat(replacedSecretProperties.size(), is(1));
assertThat(replacedTextProperties.size(), is(1));
assertThat(settings.get("password.replace"), equalTo("replaced"));
assertThat(settings.get("replace_me"), equalTo("text"));

View File

@ -45,9 +45,6 @@ import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
/**
*
*/
@SuppressForbidden(reason = "modifies system properties intentionally")
public class CliToolTests extends CliToolTestCase {
public void testOK() throws Exception {
@ -233,16 +230,14 @@ public class CliToolTests extends CliToolTestCase {
final AtomicReference<String> promptedTextValue = new AtomicReference<>(null);
final Terminal terminal = new MockTerminal() {
@Override
public char[] readSecret(String text, Object... args) {
public char[] readSecret(String text) {
counter.incrementAndGet();
assertThat(args, arrayContaining((Object) "foo.password"));
return "changeit".toCharArray();
}
@Override
public String readText(String text, Object... args) {
public String readText(String text) {
counter.incrementAndGet();
assertThat(args, arrayContaining((Object) "replace"));
return "replaced";
}
};

View File

@ -65,12 +65,12 @@ public abstract class CliToolTestCase extends ESTestCase {
protected void doPrint(String msg) {}
@Override
public String readText(String text, Object... args) {
public String readText(String prompt) {
return null;
}
@Override
public char[] readSecret(String text, Object... args) {
public char[] readSecret(String prompt) {
return new char[0];
}
}