Skip to content

Commit

Permalink
[MNG-8342] Add command line and terminal information when verbose (#1840
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gnodet authored Oct 24, 2024
1 parent 3425b4d commit 69c6a3f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@
import org.apache.maven.cling.invoker.mvn.ProtoSession;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.internal.impl.SettingsUtilsV4;
import org.apache.maven.jline.FastTerminal;
import org.apache.maven.jline.MessageUtils;
import org.apache.maven.logging.LoggingOutputStream;
import org.apache.maven.logging.api.LogLevelRecorder;
import org.apache.maven.slf4j.MavenSimpleLogger;
import org.eclipse.aether.transfer.TransferListener;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.terminal.impl.AbstractPosixTerminal;
import org.jline.terminal.spi.TerminalExt;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LocationAwareLogger;
Expand Down Expand Up @@ -409,26 +412,56 @@ protected void activateLogging(C context) throws Exception {
}

protected void helpOrVersionAndMayExit(C context) throws Exception {
Consumer<String> writer = determineWriter(context);
R invokerRequest = context.invokerRequest;
if (invokerRequest.options().help().isPresent()) {
Consumer<String> writer = determineWriter(context);
invokerRequest.options().displayHelp(context.invokerRequest.parserRequest(), writer);
throw new ExitException(0);
}
if (invokerRequest.options().showVersionAndExit().isPresent()) {
if (invokerRequest.options().quiet().orElse(false)) {
writer.accept(CLIReportingUtils.showVersionMinimal());
} else {
writer.accept(CLIReportingUtils.showVersion());
}
showVersion(context);
throw new ExitException(0);
}
}

protected void showVersion(C context) {
Consumer<String> writer = determineWriter(context);
R invokerRequest = context.invokerRequest;
if (invokerRequest.options().quiet().orElse(false)) {
writer.accept(CLIReportingUtils.showVersionMinimal());
} else if (invokerRequest.options().verbose().orElse(false)) {
writer.accept(CLIReportingUtils.showVersion(
ProcessHandle.current().info().commandLine().orElse(null), describe(context.terminal)));

} else {
writer.accept(CLIReportingUtils.showVersion());
}
}

protected String describe(Terminal terminal) {
if (terminal == null) {
return null;
}
if (terminal instanceof FastTerminal ft) {
terminal = ft.getTerminal();
}
List<String> subs = new ArrayList<>();
subs.add("type=" + terminal.getType());
if (terminal instanceof TerminalExt te) {
subs.add("provider=" + te.getProvider().name());
}
if (terminal instanceof AbstractPosixTerminal pt) {
subs.add("pty=" + pt.getPty().getClass().getName());
}
return terminal.getClass().getSimpleName() + " (" + String.join(", ", subs) + ")";
}

protected void preCommands(C context) throws Exception {
Options mavenOptions = context.invokerRequest.options();
if (mavenOptions.verbose().orElse(false) || mavenOptions.showVersion().orElse(false)) {
determineWriter(context).accept(CLIReportingUtils.showVersion());
boolean verbose = mavenOptions.verbose().orElse(false);
boolean version = mavenOptions.showVersion().orElse(false);
if (verbose || version) {
showVersion(context);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public final class CLIReportingUtils {
public static final String BUILD_VERSION_PROPERTY = "version";

public static String showVersion() {
return showVersion(null, null);
}

public static String showVersion(String commandLine, String terminal) {
final String ls = System.lineSeparator();
Properties properties = getBuildProperties();
StringBuilder version = new StringBuilder(256);
Expand Down Expand Up @@ -78,6 +82,13 @@ public static String showVersion() {
.append("\", family: \"")
.append(Os.OS_FAMILY)
.append('\"');
// Add process information using modern Java API
if (commandLine != null) {
version.append(ls).append("Command line: ").append(commandLine);
}
if (terminal != null) {
version.append(ls).append("Terminal: ").append(terminal);
}
return version.toString();
}

Expand Down

0 comments on commit 69c6a3f

Please sign in to comment.