Skip to content

Commit

Permalink
ninja: upgrade-objects, fixed handling of non-existing verification file
Browse files Browse the repository at this point in the history
(cherry picked from commit d5c62ff)
  • Loading branch information
1azyman committed Jan 15, 2024
1 parent 910c0dd commit e5d0e80
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void setOut(@NotNull PrintStream out) {
this.out = out;
}

@SuppressWarnings("unused")
public PrintStream getErr() {
return err;
}
Expand Down Expand Up @@ -163,8 +164,7 @@ public void setErr(@NotNull PrintStream err) {
}

Object result = action.execute();
if (result instanceof ActionResult) {
ActionResult<?> actionResult = (ActionResult<?>) result;
if (result instanceof ActionResult<?> actionResult) {
return new MainResult<>(actionResult.result(), actionResult.exitCode(), actionResult.exitMessage());
}

Expand All @@ -175,7 +175,8 @@ public void setErr(@NotNull PrintStream err) {
} catch (InputParameterException ex) {
err.println(ConsoleFormat.formatLogMessage(LogLevel.ERROR, ex.getMessage()));

return MainResult.EMPTY_ERROR;
int exitCode = ex.getExitCode() != null ? ex.getExitCode() : MainResult.DEFAULT_EXIT_CODE_ERROR;
return new MainResult<>(ex.getMessage(), exitCode);
} catch (Exception ex) {
handleException(base, ex);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
*/
public final class MainResult<T> {

public static final int DEFAULT_EXIT_CODE_ERROR = 1;

public static final int DEFAULT_EXIT_CODE_SUCCESS = 0;

private final T result;

private final int exitCode;

private final String exitMessage;

public static final MainResult<?> EMPTY_ERROR = new MainResult<>(null, 1);
public static final MainResult<?> EMPTY_ERROR = new MainResult<>(null, DEFAULT_EXIT_CODE_ERROR);

public static final MainResult<?> EMPTY_SUCCESS = new MainResult<>(null, 0);
public static final MainResult<?> EMPTY_SUCCESS = new MainResult<>(null, DEFAULT_EXIT_CODE_SUCCESS);

public MainResult(T result) {
this(result, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;

import com.evolveum.midpoint.ninja.util.InputParameterException;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
Expand All @@ -28,6 +30,8 @@

public class UpgradeObjectsAction extends AbstractRepositorySearchAction<UpgradeObjectsOptions, ActionResult<UpgradeObjectsItemsSummary>> {

public static final int ERROR_CODE_VERIFICATION_FILE_NOT_FOUND = 101;

public UpgradeObjectsAction() {
}

Expand Down Expand Up @@ -157,14 +161,20 @@ public String getOperationName() {

private Map<UUID, Set<SkipUpgradeItem>> loadVerificationFile() throws IOException {
File verification = options.getVerification();
if (verification == null || !verification.exists() || !verification.isFile()) {
if (verification == null) {
// FIXME: Add log explanation, what is happening
if (context.isUserMode()) {
log.warn("Upgrade objects started without verification report, all necessary non-manual changes will be accepted.");
}
return Collections.emptyMap();
}

if (!verification.exists() || !verification.isFile()) {
throw new InputParameterException(
"Verification file '" + verification.getAbsolutePath() + "' does not exist or is not a file",
ERROR_CODE_VERIFICATION_FILE_NOT_FOUND);
}

log.info("Loading verification file");

Map<UUID, Set<SkipUpgradeItem>> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@
*/
public class InputParameterException extends RuntimeException {

private Integer exitCode;

public InputParameterException(String message) {
super(message);
}

public InputParameterException(String message, Integer exitCode) {
super(message);
this.exitCode = exitCode;
}

public Integer getExitCode() {
return exitCode;
}
}

0 comments on commit e5d0e80

Please sign in to comment.