mirror of https://github.com/apache/kafka.git
MINOR: Various cleanups in shell (#15712)
Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
This commit is contained in:
parent
c034cf2953
commit
0b4e9afee2
|
@ -27,7 +27,6 @@ import org.jline.reader.History;
|
|||
import org.jline.reader.LineReader;
|
||||
import org.jline.reader.LineReaderBuilder;
|
||||
import org.jline.reader.ParsedLine;
|
||||
import org.jline.reader.Parser;
|
||||
import org.jline.reader.UserInterruptException;
|
||||
import org.jline.reader.impl.DefaultParser;
|
||||
import org.jline.reader.impl.history.DefaultHistory;
|
||||
|
@ -56,7 +55,7 @@ public final class InteractiveShell implements AutoCloseable {
|
|||
|
||||
@Override
|
||||
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
|
||||
if (line.words().size() == 0) {
|
||||
if (line.words().isEmpty()) {
|
||||
CommandUtils.completeCommand("", candidates);
|
||||
} else if (line.words().size() == 1) {
|
||||
CommandUtils.completeCommand(line.words().get(0), candidates);
|
||||
|
@ -82,9 +81,7 @@ public final class InteractiveShell implements AutoCloseable {
|
|||
|
||||
private final MetadataShellState state;
|
||||
private final Terminal terminal;
|
||||
private final Parser parser;
|
||||
private final History history;
|
||||
private final MetadataShellCompleter completer;
|
||||
private final LineReader reader;
|
||||
|
||||
public InteractiveShell(MetadataShellState state) throws IOException {
|
||||
|
@ -93,14 +90,12 @@ public final class InteractiveShell implements AutoCloseable {
|
|||
system(true).
|
||||
nativeSignals(true);
|
||||
this.terminal = builder.build();
|
||||
this.parser = new DefaultParser();
|
||||
this.history = new DefaultHistory();
|
||||
this.completer = new MetadataShellCompleter(state);
|
||||
this.reader = LineReaderBuilder.builder().
|
||||
terminal(terminal).
|
||||
parser(parser).
|
||||
parser(new DefaultParser()).
|
||||
history(history).
|
||||
completer(completer).
|
||||
completer(new MetadataShellCompleter(state)).
|
||||
option(LineReader.Option.AUTO_FRESH_LINE, false).
|
||||
build();
|
||||
}
|
||||
|
|
|
@ -46,7 +46,6 @@ import java.nio.file.Path;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
|
@ -217,7 +216,7 @@ public final class MetadataShell {
|
|||
}
|
||||
}
|
||||
|
||||
public void close() throws Exception {
|
||||
public void close() {
|
||||
Utils.closeQuietly(loader, "loader");
|
||||
if (raftManager != null) {
|
||||
try {
|
||||
|
@ -238,7 +237,7 @@ public final class MetadataShell {
|
|||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
public static void main(String[] args) {
|
||||
ArgumentParser parser = ArgumentParsers
|
||||
.newArgumentParser("kafka-metadata-shell")
|
||||
.defaultHelp(true)
|
||||
|
@ -281,13 +280,12 @@ public final class MetadataShell {
|
|||
}
|
||||
}
|
||||
|
||||
void waitUntilCaughtUp() throws ExecutionException, InterruptedException {
|
||||
void waitUntilCaughtUp() throws InterruptedException {
|
||||
while (true) {
|
||||
if (loader.lastAppliedOffset() > 0) {
|
||||
return;
|
||||
}
|
||||
Thread.sleep(10);
|
||||
}
|
||||
//snapshotFileReader.caughtUpFuture().get();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,9 +73,9 @@ public final class CommandUtils {
|
|||
public static List<String> splitPath(String path) {
|
||||
List<String> results = new ArrayList<>();
|
||||
String[] components = path.split("/");
|
||||
for (int i = 0; i < components.length; i++) {
|
||||
if (!components[i].isEmpty()) {
|
||||
results.add(components[i]);
|
||||
for (String component : components) {
|
||||
if (!component.isEmpty()) {
|
||||
results.add(component);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
|
@ -85,7 +85,7 @@ public final class CommandUtils {
|
|||
List<String> output = new ArrayList<>();
|
||||
for (String string : input) {
|
||||
if (string.equals("..")) {
|
||||
if (output.size() > 0) {
|
||||
if (!output.isEmpty()) {
|
||||
output.remove(output.size() - 1);
|
||||
}
|
||||
} else if (!string.equals(".")) {
|
||||
|
@ -106,7 +106,7 @@ public final class CommandUtils {
|
|||
MetadataShellState state,
|
||||
String pathPrefix,
|
||||
List<Candidate> candidates
|
||||
) throws Exception {
|
||||
) {
|
||||
state.visit(data -> {
|
||||
String absolutePath = pathPrefix.startsWith("/") ?
|
||||
pathPrefix : data.workingDirectory() + "/" + pathPrefix;
|
||||
|
|
|
@ -140,7 +140,7 @@ public final class LsCommandHandler implements Commands.Handler {
|
|||
List<String> targetFiles,
|
||||
List<TargetDirectory> targetDirectories) {
|
||||
printEntries(writer, "", screenWidth, targetFiles);
|
||||
boolean needIntro = targetFiles.size() > 0 || targetDirectories.size() > 1;
|
||||
boolean needIntro = !targetFiles.isEmpty() || targetDirectories.size() > 1;
|
||||
boolean firstIntro = targetFiles.isEmpty();
|
||||
for (TargetDirectory targetDirectory : targetDirectories) {
|
||||
String intro = "";
|
||||
|
@ -205,8 +205,7 @@ public final class LsCommandHandler implements Commands.Handler {
|
|||
}
|
||||
for (int i = 0; i < entries.size(); i++) {
|
||||
String entry = entries.get(i);
|
||||
for (int s = 0; s < schemas.length; s++) {
|
||||
ColumnSchema schema = schemas[s];
|
||||
for (ColumnSchema schema : schemas) {
|
||||
schema.process(i, entry);
|
||||
}
|
||||
}
|
||||
|
@ -244,8 +243,8 @@ public final class LsCommandHandler implements Commands.Handler {
|
|||
|
||||
int totalWidth() {
|
||||
int total = 0;
|
||||
for (int i = 0; i < columnWidths.length; i++) {
|
||||
total += columnWidths[i];
|
||||
for (int columnWidth : columnWidths) {
|
||||
total += columnWidth;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
@ -264,7 +263,7 @@ public final class LsCommandHandler implements Commands.Handler {
|
|||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(columnWidths, entriesPerColumn);
|
||||
return Objects.hash(Arrays.hashCode(columnWidths), entriesPerColumn);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -280,9 +279,9 @@ public final class LsCommandHandler implements Commands.Handler {
|
|||
public String toString() {
|
||||
StringBuilder bld = new StringBuilder("ColumnSchema(columnWidths=[");
|
||||
String prefix = "";
|
||||
for (int i = 0; i < columnWidths.length; i++) {
|
||||
for (int columnWidth : columnWidths) {
|
||||
bld.append(prefix);
|
||||
bld.append(columnWidths[i]);
|
||||
bld.append(columnWidth);
|
||||
prefix = ", ";
|
||||
}
|
||||
bld.append("], entriesPerColumn=").append(entriesPerColumn).append(")");
|
||||
|
|
|
@ -50,10 +50,6 @@ public final class GlobVisitor implements Consumer<MetadataShellState> {
|
|||
this.node = node;
|
||||
}
|
||||
|
||||
public String[] path() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public MetadataNode node() {
|
||||
return node;
|
||||
}
|
||||
|
@ -72,7 +68,7 @@ public final class GlobVisitor implements Consumer<MetadataShellState> {
|
|||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(path, node);
|
||||
return Objects.hash(Arrays.hashCode(path), node);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -87,9 +83,9 @@ public final class GlobVisitor implements Consumer<MetadataShellState> {
|
|||
@Override
|
||||
public String toString() {
|
||||
StringBuilder bld = new StringBuilder("MetadataNodeInfo(path=");
|
||||
for (int i = 0; i < path.length; i++) {
|
||||
for (String s : path) {
|
||||
bld.append("/");
|
||||
bld.append(path[i]);
|
||||
bld.append(s);
|
||||
}
|
||||
bld.append(", node=").append(node).append(")");
|
||||
return bld.toString();
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
package org.apache.kafka.shell.command;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -27,7 +26,7 @@ import java.util.Arrays;
|
|||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
|
||||
@Timeout(value = 120000, unit = MILLISECONDS)
|
||||
@Timeout(value = 120)
|
||||
public class CommandTest {
|
||||
@Test
|
||||
public void testParseCommands() {
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
package org.apache.kafka.shell.command;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.apache.kafka.shell.command.LsCommandHandler.ColumnSchema;
|
||||
|
@ -34,7 +33,7 @@ import java.util.Arrays;
|
|||
import java.util.Collections;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
@Timeout(value = 120000, unit = MILLISECONDS)
|
||||
@Timeout(value = 120)
|
||||
public class LsCommandHandlerTest {
|
||||
@Test
|
||||
public void testCalculateColumnSchema() {
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
package org.apache.kafka.shell.glob;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
@ -26,7 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
|
||||
@Timeout(value = 120000, unit = MILLISECONDS)
|
||||
@Timeout(value = 120)
|
||||
public class GlobComponentTest {
|
||||
private void verifyIsLiteral(GlobComponent globComponent, String component) {
|
||||
assertTrue(globComponent.literal());
|
||||
|
|
Loading…
Reference in New Issue