Skip to content

Commit a350bfa

Browse files
authored
Format projects under :distribution:tools (#51226)
Opt-in the sub-projects of :distribution:tools for automatic formatting.
1 parent ff22445 commit a350bfa

File tree

26 files changed

+840
-608
lines changed

26 files changed

+840
-608
lines changed

build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ subprojects {
107107
// switched to an exclude list, and eventualy removed completely.
108108
def projectPathsToFormat = [
109109
':build-tools',
110+
':distribution:tools:java-version-checker',
111+
':distribution:tools:keystore-cli',
112+
':distribution:tools:launchers',
113+
':distribution:tools:plugin-cli',
110114
':x-pack:plugin:autoscaling',
111115
':x-pack:plugin:enrich'
112116
]

distribution/tools/java-version-checker/src/main/java/org/elasticsearch/tools/java_version_checker/JavaVersion.java

-1
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,4 @@ static int compare(final List<Integer> left, final List<Integer> right) {
6666
return 0;
6767
}
6868

69-
7069
}

distribution/tools/java-version-checker/src/main/java/org/elasticsearch/tools/java_version_checker/JavaVersionChecker.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
*/
2828
final class JavaVersionChecker {
2929

30-
private JavaVersionChecker() {
31-
}
30+
private JavaVersionChecker() {}
3231

3332
/**
3433
* The main entry point. The exit code is 0 if the Java version is at least 1.8, otherwise the exit code is 1.
@@ -42,9 +41,10 @@ public static void main(final String[] args) {
4241
}
4342
if (JavaVersion.compare(JavaVersion.CURRENT, JavaVersion.JAVA_11) < 0) {
4443
final String message = String.format(
45-
Locale.ROOT,
46-
"the minimum required Java version is 11; your Java version from [%s] does not meet this requirement",
47-
System.getProperty("java.home"));
44+
Locale.ROOT,
45+
"the minimum required Java version is 11; your Java version from [%s] does not meet this requirement",
46+
System.getProperty("java.home")
47+
);
4848
errPrintln(message);
4949
exit(1);
5050
}

distribution/tools/keystore-cli/src/main/java/org/elasticsearch/common/settings/AddFileKeyStoreCommand.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class AddFileKeyStoreCommand extends EnvironmentAwareCommand {
5555
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
5656
KeyStoreWrapper keystore = KeyStoreWrapper.load(env.configFile());
5757
if (keystore == null) {
58-
if (options.has(forceOption) == false &&
59-
terminal.promptYesNo("The elasticsearch keystore does not exist. Do you want to create it?", false) == false) {
58+
if (options.has(forceOption) == false
59+
&& terminal.promptYesNo("The elasticsearch keystore does not exist. Do you want to create it?", false) == false) {
6060
terminal.println("Exiting without creating keystore.");
6161
return;
6262
}
@@ -87,14 +87,16 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th
8787
throw new UserException(ExitCodes.IO_ERROR, "File [" + file.toString() + "] does not exist");
8888
}
8989
if (argumentValues.size() > 2) {
90-
throw new UserException(ExitCodes.USAGE, "Unrecognized extra arguments [" +
91-
String.join(", ", argumentValues.subList(2, argumentValues.size())) + "] after filepath");
90+
throw new UserException(
91+
ExitCodes.USAGE,
92+
"Unrecognized extra arguments [" + String.join(", ", argumentValues.subList(2, argumentValues.size())) + "] after filepath"
93+
);
9294
}
9395
keystore.setFile(setting, Files.readAllBytes(file));
9496
keystore.save(env.configFile(), new char[0]);
9597
}
9698

97-
@SuppressForbidden(reason="file arg for cli")
99+
@SuppressForbidden(reason = "file arg for cli")
98100
private Path getPath(String file) {
99101
return PathUtils.get(file);
100102
}

distribution/tools/keystore-cli/src/main/java/org/elasticsearch/common/settings/AddStringKeyStoreCommand.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ InputStream getStdin() {
5959
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
6060
KeyStoreWrapper keystore = KeyStoreWrapper.load(env.configFile());
6161
if (keystore == null) {
62-
if (options.has(forceOption) == false &&
63-
terminal.promptYesNo("The elasticsearch keystore does not exist. Do you want to create it?", false) == false) {
62+
if (options.has(forceOption) == false
63+
&& terminal.promptYesNo("The elasticsearch keystore does not exist. Do you want to create it?", false) == false) {
6464
terminal.println("Exiting without creating keystore.");
6565
return;
6666
}
@@ -84,8 +84,10 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th
8484

8585
final char[] value;
8686
if (options.has(stdinOption)) {
87-
try (BufferedReader stdinReader = new BufferedReader(new InputStreamReader(getStdin(), StandardCharsets.UTF_8));
88-
CharArrayWriter writer = new CharArrayWriter()) {
87+
try (
88+
BufferedReader stdinReader = new BufferedReader(new InputStreamReader(getStdin(), StandardCharsets.UTF_8));
89+
CharArrayWriter writer = new CharArrayWriter()
90+
) {
8991
int charInt;
9092
while ((charInt = stdinReader.read()) != -1) {
9193
if ((char) charInt == '\r' || (char) charInt == '\n') {

distribution/tools/keystore-cli/src/main/java/org/elasticsearch/common/settings/CreateKeyStoreCommand.java

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th
4646
}
4747
}
4848

49-
5049
char[] password = new char[0];// terminal.readSecret("Enter passphrase (empty for no passphrase): ");
5150
/* TODO: uncomment when entering passwords on startup is supported
5251
char[] passwordRepeat = terminal.readSecret("Enter same passphrase again: ");

distribution/tools/keystore-cli/src/main/java/org/elasticsearch/common/settings/ListKeyStoreCommand.java

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.common.settings;
2121

22-
2322
import java.util.ArrayList;
2423
import java.util.Collections;
2524
import java.util.List;

distribution/tools/keystore-cli/src/main/java/org/elasticsearch/common/settings/UpgradeKeyStoreCommand.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ protected void execute(final Terminal terminal, final OptionSet options, final E
4040
final KeyStoreWrapper wrapper = KeyStoreWrapper.load(env.configFile());
4141
if (wrapper == null) {
4242
throw new UserException(
43-
ExitCodes.CONFIG,
44-
"keystore does not exist at [" + KeyStoreWrapper.keystorePath(env.configFile()) + "]");
43+
ExitCodes.CONFIG,
44+
"keystore does not exist at [" + KeyStoreWrapper.keystorePath(env.configFile()) + "]"
45+
);
4546
}
4647
wrapper.decrypt(new char[0]);
4748
KeyStoreWrapper.upgrade(wrapper, env.configFile(), new char[0]);

distribution/tools/keystore-cli/src/test/java/org/elasticsearch/common/settings/AddStringKeyStoreCommandTests.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ protected Command newCommand() {
4343
protected Environment createEnv(Map<String, String> settings) throws UserException {
4444
return env;
4545
}
46+
4647
@Override
4748
InputStream getStdin() {
4849
return input;
@@ -180,10 +181,8 @@ public void testSpecialCharacterInName() throws Exception {
180181
terminal.addSecretInput("value");
181182
final String key = randomAlphaOfLength(4) + '@' + randomAlphaOfLength(4);
182183
final UserException e = expectThrows(UserException.class, () -> execute(key));
183-
final String exceptionString= "Setting name [" + key + "] does not match the allowed setting name pattern [[A-Za-z0-9_\\-.]+]";
184-
assertThat(
185-
e,
186-
hasToString(containsString(exceptionString)));
184+
final String exceptionString = "Setting name [" + key + "] does not match the allowed setting name pattern [[A-Za-z0-9_\\-.]+]";
185+
assertThat(e, hasToString(containsString(exceptionString)));
187186
}
188187

189188
void setInput(String inputStr) {

distribution/tools/keystore-cli/src/test/java/org/elasticsearch/common/settings/KeyStoreCommandTestCase.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ void assertSecureFile(KeyStoreWrapper keystore, String setting, Path file) throw
114114
}
115115
int eof = input.read();
116116
if (eof != -1) {
117-
fail("Found extra bytes in file stream from keystore, expected " + expectedBytes.length +
118-
" bytes but found 0x" + Integer.toHexString(eof));
117+
fail(
118+
"Found extra bytes in file stream from keystore, expected "
119+
+ expectedBytes.length
120+
+ " bytes but found 0x"
121+
+ Integer.toHexString(eof)
122+
);
119123
}
120124
}
121125

distribution/tools/keystore-cli/src/test/java/org/elasticsearch/common/settings/KeyStoreWrapperTests.java

+21-11
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void testFileSettingExhaustiveBytes() throws Exception {
8383
KeyStoreWrapper keystore = KeyStoreWrapper.create();
8484
byte[] bytes = new byte[256];
8585
for (int i = 0; i < 256; ++i) {
86-
bytes[i] = (byte)i;
86+
bytes[i] = (byte) i;
8787
}
8888
keystore.setFile("foo", bytes);
8989
keystore.save(env.configFile(), new char[0]);
@@ -110,8 +110,10 @@ public void testDecryptKeyStoreWithWrongPassword() throws Exception {
110110
KeyStoreWrapper keystore = KeyStoreWrapper.create();
111111
keystore.save(env.configFile(), new char[0]);
112112
final KeyStoreWrapper loadedkeystore = KeyStoreWrapper.load(env.configFile());
113-
final SecurityException exception = expectThrows(SecurityException.class,
114-
() -> loadedkeystore.decrypt(new char[]{'i', 'n', 'v', 'a', 'l', 'i', 'd'}));
113+
final SecurityException exception = expectThrows(
114+
SecurityException.class,
115+
() -> loadedkeystore.decrypt(new char[] { 'i', 'n', 'v', 'a', 'l', 'i', 'd' })
116+
);
115117
assertThat(exception.getMessage(), containsString("Keystore has been corrupted or tampered with"));
116118
}
117119

@@ -123,8 +125,10 @@ public void testCannotReadStringFromClosedKeystore() throws Exception {
123125
keystore.close();
124126

125127
assertThat(keystore.getSettingNames(), Matchers.hasItem(KeyStoreWrapper.SEED_SETTING.getKey()));
126-
final IllegalStateException exception = expectThrows(IllegalStateException.class,
127-
() -> keystore.getString(KeyStoreWrapper.SEED_SETTING.getKey()));
128+
final IllegalStateException exception = expectThrows(
129+
IllegalStateException.class,
130+
() -> keystore.getString(KeyStoreWrapper.SEED_SETTING.getKey())
131+
);
128132
assertThat(exception.getMessage(), containsString("closed"));
129133
}
130134

@@ -292,9 +296,13 @@ private void possiblyAlterSecretString(DataOutputStream output, int truncLength)
292296
output.write(secret_value);
293297
}
294298

295-
private void possiblyAlterEncryptedBytes(IndexOutput indexOutput, byte[] salt, byte[] iv, byte[] encryptedBytes, int
296-
truncEncryptedDataLength)
297-
throws Exception {
299+
private void possiblyAlterEncryptedBytes(
300+
IndexOutput indexOutput,
301+
byte[] salt,
302+
byte[] iv,
303+
byte[] encryptedBytes,
304+
int truncEncryptedDataLength
305+
) throws Exception {
298306
indexOutput.writeInt(4 + salt.length + 4 + iv.length + 4 + encryptedBytes.length);
299307
indexOutput.writeInt(salt.length);
300308
indexOutput.writeBytes(salt, salt.length);
@@ -387,7 +395,7 @@ public void testBackcompatV2() throws Exception {
387395
byte[] base64Bytes = Base64.getEncoder().encode(fileBytes);
388396
char[] chars = new char[base64Bytes.length];
389397
for (int i = 0; i < chars.length; ++i) {
390-
chars[i] = (char)base64Bytes[i]; // PBE only stores the lower 8 bits, so this narrowing is ok
398+
chars[i] = (char) base64Bytes[i]; // PBE only stores the lower 8 bits, so this narrowing is ok
391399
}
392400
secretKey = secretFactory.generateSecret(new PBEKeySpec(chars));
393401
keystore.setEntry("file_setting", new KeyStore.SecretKeyEntry(secretKey), protectionParameter);
@@ -437,8 +445,10 @@ public void testStringAndFileDistinction() throws Exception {
437445
public void testLegacyV3() throws GeneralSecurityException, IOException {
438446
final Path configDir = createTempDir();
439447
final Path keystore = configDir.resolve("elasticsearch.keystore");
440-
try (InputStream is = KeyStoreWrapperTests.class.getResourceAsStream("/format-v3-elasticsearch.keystore");
441-
OutputStream os = Files.newOutputStream(keystore)) {
448+
try (
449+
InputStream is = KeyStoreWrapperTests.class.getResourceAsStream("/format-v3-elasticsearch.keystore");
450+
OutputStream os = Files.newOutputStream(keystore)
451+
) {
442452
final byte[] buffer = new byte[4096];
443453
int readBytes;
444454
while ((readBytes = is.read(buffer)) > 0) {

distribution/tools/keystore-cli/src/test/java/org/elasticsearch/common/settings/UpgradeKeyStoreCommandTests.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ protected Environment createEnv(final Map<String, String> settings) {
5050

5151
public void testKeystoreUpgrade() throws Exception {
5252
final Path keystore = KeyStoreWrapper.keystorePath(env.configFile());
53-
try (InputStream is = KeyStoreWrapperTests.class.getResourceAsStream("/format-v3-elasticsearch.keystore");
54-
OutputStream os = Files.newOutputStream(keystore)) {
53+
try (
54+
InputStream is = KeyStoreWrapperTests.class.getResourceAsStream("/format-v3-elasticsearch.keystore");
55+
OutputStream os = Files.newOutputStream(keystore)
56+
) {
5557
is.transferTo(os);
5658
}
5759
try (KeyStoreWrapper beforeUpgrade = KeyStoreWrapper.load(env.configFile())) {

distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/JvmErgonomics.java

+21-18
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ static List<String> choose(final List<String> userDefinedJvmOptions) throws Inte
6262
return ergonomicChoices;
6363
}
6464

65-
private static final Pattern OPTION =
66-
Pattern.compile("^\\s*\\S+\\s+(?<flag>\\S+)\\s+:?=\\s+(?<value>\\S+)?\\s+\\{[^}]+?\\}\\s+\\{[^}]+}");
65+
private static final Pattern OPTION = Pattern.compile(
66+
"^\\s*\\S+\\s+(?<flag>\\S+)\\s+:?=\\s+(?<value>\\S+)?\\s+\\{[^}]+?\\}\\s+\\{[^}]+}"
67+
);
6768

68-
static Map<String, Optional<String>> finalJvmOptions(
69-
final List<String> userDefinedJvmOptions) throws InterruptedException, IOException {
69+
static Map<String, Optional<String>> finalJvmOptions(final List<String> userDefinedJvmOptions) throws InterruptedException,
70+
IOException {
7071
return flagsFinal(userDefinedJvmOptions).stream()
71-
.map(OPTION::matcher).filter(Matcher::matches)
72-
.collect(Collectors.toUnmodifiableMap(m -> m.group("flag"), m -> Optional.ofNullable(m.group("value"))));
72+
.map(OPTION::matcher)
73+
.filter(Matcher::matches)
74+
.collect(Collectors.toUnmodifiableMap(m -> m.group("flag"), m -> Optional.ofNullable(m.group("value"))));
7375
}
7476

7577
private static List<String> flagsFinal(final List<String> userDefinedJvmOptions) throws InterruptedException, IOException {
@@ -82,31 +84,32 @@ private static List<String> flagsFinal(final List<String> userDefinedJvmOptions)
8284
* without having to implement our own JVM option parsing logic.
8385
*/
8486
final String java = Path.of(System.getProperty("java.home"), "bin", "java").toString();
85-
final List<String> command =
86-
Stream.of(Stream.of(java), userDefinedJvmOptions.stream(), Stream.of("-XX:+PrintFlagsFinal"), Stream.of("-version"))
87-
.reduce(Stream::concat)
88-
.get()
89-
.collect(Collectors.toUnmodifiableList());
87+
final List<String> command = Stream.of(
88+
Stream.of(java),
89+
userDefinedJvmOptions.stream(),
90+
Stream.of("-XX:+PrintFlagsFinal"),
91+
Stream.of("-version")
92+
).reduce(Stream::concat).get().collect(Collectors.toUnmodifiableList());
9093
final Process process = new ProcessBuilder().command(command).start();
9194
final List<String> output = readLinesFromInputStream(process.getInputStream());
9295
final List<String> error = readLinesFromInputStream(process.getErrorStream());
9396
final int status = process.waitFor();
9497
if (status != 0) {
9598
final String message = String.format(
96-
Locale.ROOT,
97-
"starting java failed with [%d]\noutput:\n%s\nerror:\n%s",
98-
status,
99-
String.join("\n", output),
100-
String.join("\n", error));
99+
Locale.ROOT,
100+
"starting java failed with [%d]\noutput:\n%s\nerror:\n%s",
101+
status,
102+
String.join("\n", output),
103+
String.join("\n", error)
104+
);
101105
throw new RuntimeException(message);
102106
} else {
103107
return output;
104108
}
105109
}
106110

107111
private static List<String> readLinesFromInputStream(final InputStream is) throws IOException {
108-
try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
109-
BufferedReader br = new BufferedReader(isr)) {
112+
try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr)) {
110113
return br.lines().collect(Collectors.toUnmodifiableList());
111114
}
112115
}

0 commit comments

Comments
 (0)