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