Skip to content

Commit

Permalink
Merge pull request #32139 from gsmet/improve-dev-console
Browse files Browse the repository at this point in the history
Improve the dev mode console output
  • Loading branch information
gsmet committed Mar 28, 2023
2 parents 1b34b7c + 7eaad48 commit 06bd24c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void accept(Throwable throwable, StackTraceElement stackTraceElement) {
}

context.reset(
new ConsoleCommand('x', "Opens last exception in IDE", new ConsoleCommand.HelpState(new Supplier<String>() {
new ConsoleCommand('x', "Open last exception in IDE", new ConsoleCommand.HelpState(new Supplier<String>() {
@Override
public String get() {
return MessageFormat.RED;
Expand All @@ -115,7 +115,7 @@ public String get() {
public String get() {
StackTraceElement throwable = lastUserCode.get();
if (throwable == null) {
return "None";
return "none";
}
return throwable.getFileName() + ":" + throwable.getLineNumber();
}
Expand Down Expand Up @@ -179,7 +179,7 @@ ConsoleCommandBuildItem quitCommand() {
return new ConsoleCommandBuildItem(new QuitCommand());
}

@CommandDefinition(name = "quit", description = "Quits the console", aliases = { "q" })
@CommandDefinition(name = "quit", description = "Quit the console", aliases = { "q" })
public static class QuitCommand implements Command {

@Override
Expand All @@ -194,7 +194,7 @@ ConsoleCommandBuildItem helpCommand() {
return new ConsoleCommandBuildItem(new HelpCommand());
}

@CommandDefinition(name = "help", description = "Displays the command list", aliases = { "h" })
@CommandDefinition(name = "help", description = "Display the command list", aliases = { "h" })
public static class HelpCommand implements Command {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ void installBuiltins(DevModeType devModeType) {
ConsoleStateManager.this::toggleLogLevel));
commands.add(new ConsoleCommand((char) 12, null, null, 10002, null, this::clearScreen));
commands.add(new ConsoleCommand((char) 13, null, null, 10001, null, this::printBlankLine));
commands.add(new ConsoleCommand('h', "Shows this help", "for more options", 10000, null, this::printHelp));
commands.add(new ConsoleCommand('h', "Show this help", "for more options", 10000, null, this::printHelp));
if (QuarkusConsole.INSTANCE instanceof AeshConsole) {
commands.add(new ConsoleCommand(':', "Enters terminal mode", "for the terminal", 9000, null, () -> {
commands.add(new ConsoleCommand(':', "Enter terminal mode", "for the terminal", 9000, null, () -> {
}));
}
commands.add(new ConsoleCommand('q', "Quits the application", null, this::exitQuarkus));
commands.add(new ConsoleCommand('q', "Quit the application", null, this::exitQuarkus));
context.reset(commands.toArray(new ConsoleCommand[0]));
}

Expand Down Expand Up @@ -198,7 +198,7 @@ private void printBlankLine() {
}

private void printHelp() {
System.out.println("\nThe following commands are currently available:");
System.out.println("\nThe following commands are available:");
Set<ConsoleContext> contexts = new HashSet<>();
for (Holder i : commands.values()) {
contexts.add(i.context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.quarkus.deployment.dev.devservices;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.stream.Collectors;

import io.quarkus.builder.item.MultiBuildItem;
Expand All @@ -16,7 +18,7 @@ public DevServiceDescriptionBuildItem() {
public DevServiceDescriptionBuildItem(String name, ContainerInfo containerInfo, Map<String, String> configs) {
this.name = name;
this.containerInfo = containerInfo;
this.configs = configs;
this.configs = configs instanceof SortedMap ? configs : new TreeMap<>(configs);
}

public boolean hasContainerInfo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public CommandResult execute(CommandInvocation commandInvocation) {
}
return CommandResult.SUCCESS;
} else {
commandInvocation.println("Could not find dev service with name " + this.devService);
commandInvocation.println("Could not find Dev Service with name " + this.devService);
return CommandResult.FAILURE;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@
import static io.quarkus.deployment.dev.testing.MessageFormat.RESET;
import static io.quarkus.deployment.dev.testing.MessageFormat.UNDERLINE;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -31,10 +43,12 @@
import io.quarkus.deployment.dev.devservices.ContainerInfo;
import io.quarkus.deployment.dev.devservices.DevServiceDescriptionBuildItem;
import io.quarkus.dev.spi.DevModeType;
import io.quarkus.runtime.util.ContainerRuntimeUtil;
import io.quarkus.runtime.util.ContainerRuntimeUtil.ContainerRuntime;

public class DevServicesProcessor {

private static final String EXEC_FORMAT = "docker exec -it %s /bin/bash";
private static final String EXEC_FORMAT = "%s exec -it %s /bin/bash";

static volatile ConsoleStateManager.ConsoleContext context;
static volatile boolean logForwardEnabled = false;
Expand Down Expand Up @@ -70,7 +84,7 @@ public List<DevServiceDescriptionBuildItem> config(
context = ConsoleStateManager.INSTANCE.createContext("Dev Services");
}
context.reset(
new ConsoleCommand('c', "Show dev services containers", null, () -> {
new ConsoleCommand('c', "Show Dev Services containers", null, () -> {
List<DevServiceDescriptionBuildItem> descriptions = buildServiceDescriptions(
dockerStatusBuildItem, devServicesResults, devServicesLauncherConfig);
StringBuilder builder = new StringBuilder();
Expand All @@ -83,7 +97,7 @@ public List<DevServiceDescriptionBuildItem> config(
}
System.out.println(builder);
}),
new ConsoleCommand('g', "Follow dev services logs to the console",
new ConsoleCommand('g', "Follow Dev Services logs in the console",
new ConsoleCommand.HelpState(() -> logForwardEnabled ? GREEN : RED,
() -> logForwardEnabled ? "enabled" : "disabled"),
this::toggleLogForwarders));
Expand Down Expand Up @@ -111,12 +125,12 @@ private List<DevServiceDescriptionBuildItem> buildServiceDescriptions(
descriptions.sort(Comparator.comparing(DevServiceDescriptionBuildItem::getName));
// Add description from other dev service configs as last
if (devServicesLauncherConfig.isPresent()) {
Map<String, String> config = new HashMap<>(devServicesLauncherConfig.get().getConfig());
Map<String, String> config = new TreeMap<>(devServicesLauncherConfig.get().getConfig());
for (String key : configKeysFromDevServices) {
config.remove(key);
}
if (!config.isEmpty()) {
descriptions.add(new DevServiceDescriptionBuildItem("Other Dev Services", null, config));
descriptions.add(new DevServiceDescriptionBuildItem("Additional Dev Services config", null, config));
}
}
return descriptions;
Expand Down Expand Up @@ -191,12 +205,10 @@ private synchronized void toggleLogForwarders() {
}

public static void printDevService(StringBuilder builder, DevServiceDescriptionBuildItem devService, boolean withStatus) {
builder.append(BOLD).append(devService.getName()).append(NO_BOLD);
builder.append("\n");

if (devService.hasContainerInfo()) {
builder.append(BOLD).append(devService.getName()).append(NO_BOLD);
if (withStatus) {
builder.append(" - ").append(devService.getContainerInfo().getStatus());
}
builder.append("\n");
builder.append(String.format(" %-18s", "Container: "))
.append(devService.getContainerInfo().getId(), 0, 12)
.append(devService.getContainerInfo().formatNames())
Expand All @@ -208,13 +220,30 @@ public static void printDevService(StringBuilder builder, DevServiceDescriptionB
.append(" - ")
.append(devService.getContainerInfo().formatPorts())
.append("\n");
builder.append(String.format(" %-18s", "Exec command: "))
.append(String.format(EXEC_FORMAT, devService.getContainerInfo().getShortId()))
.append("\n");

ContainerRuntime containerRuntime = ContainerRuntimeUtil.detectContainerRuntime(false);
if (containerRuntime != null) {
builder.append(String.format(" %-18s", "Exec command: "))
.append(String.format(EXEC_FORMAT,
containerRuntime.getExecutableName(),
devService.getContainerInfo().getShortId()))
.append("\n");
}
}

if (!devService.getConfigs().isEmpty()) {
builder.append(String.format(" %-18s", "Injected config: "));
boolean indent = false;
for (Entry<String, String> devServiceConfigEntry : devService.getConfigs().entrySet()) {
if (indent) {
builder.append(String.format(" %-18s", " "));
}
builder.append(
String.format("- %s=%s\n",
devServiceConfigEntry.getKey(), devServiceConfigEntry.getValue()));
indent = true;
}
}
builder.append(String.format(" %-18s", "Injected Config: "))
.append(devService.formatConfigs())
.append("\n");
}

}

0 comments on commit 06bd24c

Please sign in to comment.