Skip to content

Commit

Permalink
refactor: Removed programmatic command declaration for manpage docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Feb 7, 2025
1 parent 1fe91fe commit f317a87
Show file tree
Hide file tree
Showing 27 changed files with 342 additions and 377 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public Integer call() throws Exception {
return 0;
}

protected void initialize() throws RiotInitializationException {
protected void initialize() {
if (log == null) {
log = LoggerFactory.getLogger(getClass());
}
}

protected abstract void execute() throws RiotExecutionException;
protected abstract void execute();

protected void teardown() {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public abstract class AbstractJobCommand extends AbstractCallableCommand {
protected Runnable onJobSuccessCallback;

@Override
protected void initialize() throws RiotInitializationException {
protected void initialize() {
super.initialize();
if (jobName == null) {
jobName = jobName();
Expand All @@ -90,7 +90,7 @@ protected void initialize() throws RiotInitializationException {
try {
jobRepository = JobUtils.jobRepositoryFactoryBean(jobRepositoryName).getObject();
} catch (Exception e) {
throw new RiotInitializationException("Could not create job repository", e);
throw new RiotException("Could not create job repository", e);
}
}
if (transactionManager == null) {
Expand All @@ -100,15 +100,15 @@ protected void initialize() throws RiotInitializationException {
try {
jobLauncher = jobLauncher();
} catch (Exception e) {
throw new RiotInitializationException("Could not create job launcher", e);
throw new RiotException("Could not create job launcher", e);
}
}
if (jobExplorer == null) {
try {
jobExplorer = JobUtils.jobExplorerFactoryBean(jobRepositoryName).getObject();
} catch (Exception e) {
log.warn("Error getting jobExplorer", e);
throw new RiotInitializationException("Could not create job explorer", e);
throw new RiotException("Could not create job explorer", e);
}
}
}
Expand All @@ -130,20 +130,20 @@ private JobBuilder jobBuilder() {
}

@Override
protected void execute() throws RiotExecutionException {
protected void execute() {
Job job = job();
JobExecution jobExecution;
try {
jobExecution = jobLauncher.run(job, new JobParameters());
} catch (JobExecutionException e) {
throw new RiotExecutionException("Could not run job " + job.getName(), e);
throw new RiotException(e);
}
if (JobUtils.isFailed(jobExecution.getExitStatus())) {
for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
ExitStatus stepExitStatus = stepExecution.getExitStatus();
if (JobUtils.isFailed(stepExitStatus)) {
if (CollectionUtils.isEmpty(stepExecution.getFailureExceptions())) {
throw new RiotExecutionException(stepExitStatus.getExitDescription());
throw new RiotException(stepExitStatus.getExitDescription());
}
throw wrapException(stepExecution.getFailureExceptions());
}
Expand All @@ -159,11 +159,11 @@ private String jobName() {
return commandSpec.name();
}

private RiotExecutionException wrapException(List<Throwable> throwables) {
private RiotException wrapException(List<Throwable> throwables) {
if (throwables.isEmpty()) {
return new RiotExecutionException("Job failed");
return new RiotException("Job failed");
}
return new RiotExecutionException("Job failed", throwables.get(0));
return new RiotException(throwables.get(0));
}

protected Job job(Step<?, ?>... steps) {
Expand Down Expand Up @@ -224,7 +224,7 @@ public void afterJob(JobExecution jobExecution) {
lastJob = nextJob;
} catch (InterruptedException | JobExecutionAlreadyRunningException | JobRestartException
| JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {
throw new RuntimeException(e);
throw new RiotException(e);
}
}
JobExecutionListener.super.afterJob(jobExecution);
Expand All @@ -235,7 +235,7 @@ protected boolean shouldShowProgress() {
return stepArgs.getProgressArgs().getStyle() != ProgressStyle.NONE;
}

protected abstract Job job() throws RiotExecutionException;
protected abstract Job job();

private <I, O> TaskletStep step(Step<I, O> step) {
log.info("Creating {}", step);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.redis.riot.core;

import picocli.CommandLine.ExecutionException;
import picocli.CommandLine.IExecutionStrategy;
import picocli.CommandLine.ParameterException;
import picocli.CommandLine.ParseResult;

public class CompositeExecutionStrategy implements IExecutionStrategy {

private final IExecutionStrategy inactive;
private final IExecutionStrategy active;

public CompositeExecutionStrategy(IExecutionStrategy inactive, IExecutionStrategy active) {
this.inactive = inactive;
this.active = active;
}

@Override
public int execute(ParseResult parseResult) throws ExecutionException, ParameterException {
inactive.execute(parseResult);
return active.execute(parseResult);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.slf4j.event.Level;
import org.slf4j.simple.SimpleLogger;

import picocli.CommandLine.IExecutionStrategy;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParseResult;
import picocli.CommandLine.Spec;

public class LoggingMixin {
Expand Down Expand Up @@ -107,11 +107,9 @@ public void setLevel(Level level) {
getTopLevelCommandLoggingMixin(mixee).level = level;
}

public static IExecutionStrategy executionStrategy(IExecutionStrategy delegate) {
return r -> {
getTopLevelCommandLoggingMixin(r.commandSpec()).configureLoggers();
return delegate.execute(r);
};
public static int executionStrategy(ParseResult parseResult) {
getTopLevelCommandLoggingMixin(parseResult.commandSpec()).configureLoggers();
return 0;
}

public void configureLoggers() {
Expand Down
15 changes: 13 additions & 2 deletions core/riot-core/src/main/java/com/redis/riot/core/MainCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.springframework.util.unit.DataSize;

import picocli.CommandLine;
import picocli.CommandLine.ParseResult;
import picocli.CommandLine.RunLast;

public class MainCommand extends BaseCommand implements Callable<Integer>, IO {

Expand All @@ -29,12 +31,21 @@ public int run(String... args) {
commandLine.setCaseInsensitiveEnumValuesAllowed(true);
commandLine.setUnmatchedOptionsAllowedAsOptionParameters(false);
commandLine.setExecutionExceptionHandler(new PrintExceptionMessageHandler());
registerConverters(commandLine);
commandLine.setExecutionStrategy(
new CompositeExecutionStrategy(LoggingMixin::executionStrategy, this::executionStrategy));
return commandLine.execute(args);
}

protected int executionStrategy(ParseResult parseResult) {
return new RunLast().execute(parseResult);
}

protected void registerConverters(CommandLine commandLine) {
commandLine.registerConverter(DataSize.class, DataSize::parse);
commandLine.registerConverter(Expression.class, Expression::parse);
commandLine.registerConverter(Duration.class, Duration::parse);
commandLine.registerConverter(TemplateExpression.class, Expression::parseTemplate);
commandLine.setExecutionStrategy(LoggingMixin.executionStrategy(commandLine.getExecutionStrategy()));
return commandLine.execute(args);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,32 @@

public class PrintExceptionMessageHandler implements IExecutionExceptionHandler {

public int handleExecutionException(Exception ex, CommandLine cmd, ParseResult parseResult) {
public int handleExecutionException(Exception exception, CommandLine cmd, ParseResult parseResult) {

Throwable finalException = unwrapException(exception);

if (cmd.getCommand() instanceof BaseCommand) {
if (((BaseCommand) cmd.getCommand()).loggingMixin.isStacktrace()) {
ex.printStackTrace(cmd.getErr());
finalException.printStackTrace(cmd.getErr());
}
}

// bold red error message
cmd.getErr().println(cmd.getColorScheme().errorText(ex.getMessage()));
cmd.getErr().println(cmd.getColorScheme().errorText(finalException.getMessage()));

return cmd.getExitCodeExceptionMapper() != null ? cmd.getExitCodeExceptionMapper().getExitCode(ex)
return cmd.getExitCodeExceptionMapper() != null ? cmd.getExitCodeExceptionMapper().getExitCode(finalException)
: cmd.getCommandSpec().exitCodeOnExecutionException();
}

private Throwable unwrapException(Exception exception) {
if (exception instanceof RiotException) {
RiotException riotException = (RiotException) exception;
if (riotException.getCause() == null) {
return riotException;
}
return riotException.getCause();
}
return exception;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.redis.riot.core;

@SuppressWarnings("serial")
public class RiotException extends RuntimeException {

public RiotException(String message, Throwable cause) {
super(message, cause);
}

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

public RiotException(Throwable cause) {
super(cause);
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.redis.lettucemod.RedisModulesUtils;
import com.redis.lettucemod.api.StatefulRedisModulesConnection;
import com.redis.riot.core.AbstractJobCommand;
import com.redis.riot.core.RiotInitializationException;
import com.redis.riot.core.Step;
import com.redis.spring.batch.item.redis.RedisItemReader;
import com.redis.spring.batch.item.redis.RedisItemReader.ReaderMode;
Expand All @@ -36,7 +35,7 @@ public abstract class AbstractExportCommand extends AbstractJobCommand {
private RedisContext sourceRedisContext;

@Override
protected void initialize() throws RiotInitializationException {
protected void initialize() {
super.initialize();
sourceRedisContext = sourceRedisContext();
sourceRedisContext.afterPropertiesSet();
Expand Down
Loading

0 comments on commit f317a87

Please sign in to comment.