Skip to content

Commit

Permalink
[Issue_515] Provide a CliMain variant that doesn't call System.exit
Browse files Browse the repository at this point in the history
  • Loading branch information
bstansberry committed Dec 5, 2023
1 parent 6086f3c commit 49324bd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
20 changes: 14 additions & 6 deletions prospero-cli/src/main/java/org/wildfly/prospero/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,14 @@ private static void enableJBossLogManager() {
}
}

private static final Logger logger = Logger.getLogger(CliMain.class);
static final Logger logger = Logger.getLogger(CliMain.class);

public static void main(String[] args) {
try {
CliConsole console = new CliConsole();
CommandLine commandLine = createCommandLine(console, args);
int exitCode = commandLine.execute(args);
int exitCode = execute(args);
System.exit(exitCode);
} catch (Exception e) {
System.err.println(CliMessages.MESSAGES.errorWhenProcessingCommand() + e.getMessage());
logger.error(CliMessages.MESSAGES.errorWhenProcessingCommand(), e);
logException(e);
System.exit(ReturnCodes.PROCESSING_ERROR);
}
}
Expand Down Expand Up @@ -111,4 +108,15 @@ public static CommandLine createCommandLine(CliConsole console, String[] args, A
return commandLine;
}

static int execute(String[] args) {
CliConsole console = new CliConsole();
CommandLine commandLine = createCommandLine(console, args);
return commandLine.execute(args);
}

static void logException(Exception e) {
System.err.println(CliMessages.MESSAGES.errorWhenProcessingCommand() + e.getMessage());
logger.error(CliMessages.MESSAGES.errorWhenProcessingCommand(), e);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.wildfly.prospero.cli;

import static org.wildfly.prospero.cli.CliMain.execute;
import static org.wildfly.prospero.cli.CliMain.logException;

/**
* Variant of {@link CliMain} that doesn't call {@link System#exit(int)}.
* Meant for embedded use cases, for example Maven build executions.
*/
public class CliMainNoExit {

public static void main(String[] args) {
try {
int exitCode = execute(args);
if (exitCode != 0) {
System.err.println(CliMessages.MESSAGES.unexpectedExitWhenProcessingCommand(exitCode));
CliMain.logger.error(CliMessages.MESSAGES.unexpectedExitWhenProcessingCommand(exitCode));
}
} catch (Exception e) {
logException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ default String errorWhenProcessingCommand() {
return bundle.getString("prospero.general.processing_error") + " ";
}

default String unexpectedExitWhenProcessingCommand(int exitCode) {
return format(bundle.getString("prospero.general.processing_exit error"), exitCode);
}

default String possibleDowngrade() {
return bundle.getString("prospero.updates.downgrade.warning");
}
Expand Down
1 change: 1 addition & 0 deletions prospero-cli/src/main/resources/UsageMessages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ prospero.general.prompt.reminder=Choose [y/N]:
prospero.general.prompt.yes=y
prospero.general.prompt.no=n
prospero.general.processing_error=Error when processing command:
prospero.general.processing_exit_error=Error when processing command: Exit code %.2f"
prospero.general.operation.completed.time=Operation completed in %.2f seconds.
prospero.general.error.header=ERROR: %s
prospero.general.error.ssl=SSL error, maybe you forgot to configure the certificates
Expand Down

0 comments on commit 49324bd

Please sign in to comment.