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 0c34c14
Show file tree
Hide file tree
Showing 4 changed files with 58 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,39 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 0c34c14

Please sign in to comment.