Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import org.elasticsearch.core.CheckedFunction;
import org.elasticsearch.env.Environment;

import java.io.CharArrayWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -53,42 +50,26 @@ protected void executeCommand(Terminal terminal, OptionSet options, Environment

final KeyStoreWrapper keyStore = getKeyStore();

final Closeable closeable;
final CheckedFunction<String, char[], IOException> valueSupplier;
if (options.has(stdinOption)) {
final Reader stdinReader = terminal.getReader();
valueSupplier = s -> {
try (CharArrayWriter writer = new CharArrayWriter()) {
int c;
while ((c = stdinReader.read()) != -1) {
if ((char) c == '\r' || (char) c == '\n') {
break;
}
writer.write((char) c);
}
return writer.toCharArray();
}
};
closeable = stdinReader;
} else {
valueSupplier = s -> terminal.readSecret("Enter value for " + s + ": ");
closeable = () -> {};
}
final CheckedFunction<String, char[], IOException> valueSupplier = s -> {
String prompt = "";
if (options.has(stdinOption) == false) {
prompt = "Enter value for " + s + ": ";
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This style of supplying a default that is sometimes overridden later defeats Java's error detection for paths where the value is not set.

Suggested change
String prompt = "";
if (options.has(stdinOption) == false) {
prompt = "Enter value for " + s + ": ";
}
String prompt;
if (options.has(stdinOption)) {
prompt = "";
} else {
prompt = "Enter value for " + s + ": ";
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied your suggestion.

return terminal.readSecret(prompt);
};

try (closeable) {
for (final String setting : settings) {
if (keyStore.getSettingNames().contains(setting) && options.has(forceOption) == false) {
if (terminal.promptYesNo("Setting " + setting + " already exists. Overwrite?", false) == false) {
terminal.println("Exiting without modifying keystore.");
return;
}
for (final String setting : settings) {
if (keyStore.getSettingNames().contains(setting) && options.has(forceOption) == false) {
if (terminal.promptYesNo("Setting " + setting + " already exists. Overwrite?", false) == false) {
terminal.println("Exiting without modifying keystore.");
return;
}
}

try {
keyStore.setString(setting, valueSupplier.apply(setting));
} catch (final IllegalArgumentException e) {
throw new UserException(ExitCodes.DATA_ERROR, e.getMessage());
}
try {
keyStore.setString(setting, valueSupplier.apply(setting));
} catch (final IllegalArgumentException e) {
throw new UserException(ExitCodes.DATA_ERROR, e.getMessage());
}
}

Expand Down