Skip to content

Commit

Permalink
refactor: Exposed Main application API
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed May 28, 2024
1 parent f445131 commit 86b1f5a
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
import picocli.CommandLine.ParentCommand;

@Command
public abstract class AbstractCommand extends BaseCommand implements Callable<Integer> {
public abstract class AbstractCommand<C extends IO> extends BaseCommand implements Callable<Integer> {

@ParentCommand
protected AbstractMain parent;
private C parent;

protected C parent() {
return parent;
}

@Option(names = "--help", usageHelp = true, description = "Show this help message and exit.")
private boolean helpRequested;
Expand All @@ -27,11 +31,18 @@ public abstract class AbstractCommand extends BaseCommand implements Callable<In

protected Logger log;

public void copyTo(AbstractCommand target) {
public void copyTo(AbstractCommand<C> target) {
target.helpRequested = helpRequested;
target.loggingArgs = loggingArgs;
target.log = log;
}

@Override
public Integer call() throws Exception {
setup();
execute();
return 0;
}

protected void setup() {
setupLogging();
Expand Down Expand Up @@ -69,12 +80,7 @@ private static void setBoolean(String property, boolean value) {
System.setProperty(property, String.valueOf(value));
}

@Override
public Integer call() throws Exception {
setup();
execute();
return 0;
}


protected abstract void execute() throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import picocli.CommandLine.Command;

@Command
public abstract class AbstractJobCommand extends AbstractCommand {
public abstract class AbstractJobCommand<C extends IO> extends AbstractCommand<C> {

public static final String DEFAULT_JOB_REPOSITORY_NAME = "riot";

Expand All @@ -52,7 +52,7 @@ public abstract class AbstractJobCommand extends AbstractCommand {
protected PlatformTransactionManager transactionManager;
protected JobLauncher jobLauncher;

public void copyTo(AbstractJobCommand target) {
public void copyTo(AbstractJobCommand<C> target) {
super.copyTo(target);
target.jobArgs = jobArgs;
target.jobRepository = jobRepository;
Expand Down
88 changes: 0 additions & 88 deletions core/riot-core/src/main/java/com/redis/riot/core/AbstractMain.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import picocli.CommandLine.Spec;

@Command(usageHelpAutoWidth = true)
abstract class BaseCommand {
public abstract class BaseCommand {

static {
if (System.getenv().containsKey("RIOT_NO_COLOR")) {
Expand All @@ -17,7 +17,7 @@ abstract class BaseCommand {
}

@Spec
CommandSpec commandSpec;
protected CommandSpec commandSpec;

@Option(names = "-D", paramLabel = "<key=value>", description = "Sets a System property.", mapFallbackValue = "")
void setProperty(Map<String, String> props) {
Expand Down

This file was deleted.

15 changes: 15 additions & 0 deletions core/riot-core/src/main/java/com/redis/riot/core/IO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.redis.riot.core;

import java.io.PrintWriter;

public interface IO {

PrintWriter getOut();

void setOut(PrintWriter out);

PrintWriter getErr();

void setErr(PrintWriter err);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import picocli.CommandLine.IExecutionExceptionHandler;
import picocli.CommandLine.ParseResult;

class PrintExceptionMessageHandler implements IExecutionExceptionHandler {
public class PrintExceptionMessageHandler implements IExecutionExceptionHandler {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.lettuce.core.RedisCommandTimeoutException;
import picocli.CommandLine.ArgGroup;

abstract class AbstractRedisCommand extends AbstractJobCommand {
abstract class AbstractRedisCommand extends AbstractJobCommand<Main> {

private static final String CONTEXT_VAR_REDIS = "redis";

Expand Down
78 changes: 59 additions & 19 deletions plugins/riot/src/main/java/com/redis/riot/Main.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,85 @@
package com.redis.riot;

import java.util.ArrayList;
import java.util.List;
import java.io.PrintWriter;

import com.redis.riot.core.AbstractMain;
import org.springframework.expression.Expression;
import org.springframework.util.unit.DataSize;

import com.redis.riot.core.BaseCommand;
import com.redis.riot.core.IO;
import com.redis.riot.core.PrintExceptionMessageHandler;
import com.redis.riot.core.RiotUtils;
import com.redis.riot.core.TemplateExpression;
import com.redis.riot.operation.OperationCommand;
import com.redis.spring.batch.Range;

import io.lettuce.core.RedisURI;
import picocli.AutoComplete.GenerateCompletion;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.IExecutionStrategy;
import picocli.CommandLine.ParseResult;
import picocli.CommandLine.RunFirst;
import picocli.CommandLine.RunLast;

@Command(name = "riot", versionProvider = Versions.class, headerHeading = "RIOT is a data import/export tool for Redis.%n%n", footerHeading = "%nDocumentation found at http://redis.github.io/riot%n", subcommands = {
@Command(name = "riot", versionProvider = Versions.class, headerHeading = "RIOT is a data import/export tool for Redis.%n%n", footerHeading = "%nDocumentation found at http://redis.github.io/riot%n", mixinStandardHelpOptions = true, subcommands = {
DatabaseImport.class, DatabaseExport.class, FileImport.class, FileExport.class, FakerImport.class,
Generate.class, Replicate.class, Compare.class, Ping.class, GenerateCompletion.class })
public class Main extends AbstractMain {
public class Main extends BaseCommand implements Runnable, IO {

public static void main(String[] args) {
System.exit(run(new Main(), args));
private PrintWriter out;
private PrintWriter err;

@Override
public PrintWriter getOut() {
return out;
}

@Override
protected CommandLine commandLine() {
CommandLine commandLine = super.commandLine();
commandLine.registerConverter(RedisURI.class, RedisURI::create);
return commandLine;
public void setOut(PrintWriter out) {
this.out = out;
}

@Override
protected List<IExecutionStrategy> executionStrategies() {
List<IExecutionStrategy> strategies = new ArrayList<>();
strategies.addAll(super.executionStrategies());
strategies.add(Main::executionStrategy);
return strategies;
public PrintWriter getErr() {
return err;
}

@Override
public void setErr(PrintWriter err) {
this.err = err;
}

@Override
public void run() {
commandSpec.commandLine().usage(out);
}

public static CommandLine commandLine(IO main) {
CommandLine commandLine = new CommandLine(main);
main.setOut(commandLine.getOut());
main.setErr(commandLine.getErr());
return commandLine;
}

public static int run(CommandLine commandLine, String... args) {
commandLine.setCaseInsensitiveEnumValuesAllowed(true);
commandLine.setUnmatchedOptionsAllowedAsOptionParameters(false);
commandLine.setExecutionExceptionHandler(new PrintExceptionMessageHandler());
commandLine.setUsageHelpWidth(110);
commandLine.setUsageHelpLongOptionsMaxWidth(42);
commandLine.registerConverter(DataSize.class, DataSize::parse);
commandLine.registerConverter(Range.class, Range::parse);
commandLine.registerConverter(Expression.class, RiotUtils::parse);
commandLine.registerConverter(TemplateExpression.class, RiotUtils::parseTemplate);
return commandLine.execute(args);
}

public static void main(String[] args) {
CommandLine commandLine = commandLine(new Main());
commandLine.setExecutionStrategy(Main::executionStrategy);
System.exit(run(commandLine, args));
}

private static int executionStrategy(ParseResult parseResult) {
public static int executionStrategy(ParseResult parseResult) {
for (ParseResult subcommand : parseResult.subcommands()) {
Object command = subcommand.commandSpec().userObject();
if (AbstractImport.class.isAssignableFrom(command.getClass())) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/riot/src/main/java/com/redis/riot/Ping.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void copyTo(Ping target) {
protected Job job() {
PingExecutionItemReader reader = new PingExecutionItemReader(redisCommands);
reader.setMaxItemCount(pingArgs.getCount());
PingLatencyItemWriter writer = new PingLatencyItemWriter(parent.getOut());
PingLatencyItemWriter writer = new PingLatencyItemWriter(parent().getOut());
writer.setLatencyArgs(pingArgs.getLatencyArgs());
Step<PingExecution, PingExecution> step = new Step<>(reader, writer);
step.taskName("Pinging");
Expand Down
Loading

0 comments on commit 86b1f5a

Please sign in to comment.