Skip to content

Commit

Permalink
fix: Execution strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Oct 9, 2024
1 parent d92251d commit b1e3eba
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 75 deletions.
40 changes: 20 additions & 20 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 @@ -12,26 +12,6 @@ public class MainCommand extends BaseCommand implements Callable<Integer>, IO {
private PrintWriter out;
private PrintWriter err;

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

@Override
public void setOut(PrintWriter out) {
this.out = out;
}

@Override
public PrintWriter getErr() {
return err;
}

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

@Override
public Integer call() throws Exception {
commandSpec.commandLine().usage(out);
Expand All @@ -56,4 +36,24 @@ public int run(String... args) {
return commandLine.execute(args);
}

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

@Override
public void setOut(PrintWriter out) {
this.out = out;
}

@Override
public PrintWriter getErr() {
return err;
}

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

}
26 changes: 21 additions & 5 deletions plugins/riot/src/main/java/com/redis/riot/Riot.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package com.redis.riot;

import com.redis.riot.core.MainCommand;
import com.redis.riot.operation.OperationCommand;
import com.redis.spring.batch.item.redis.common.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 software.amazon.awssdk.regions.Region;

@Command(name = "riot", versionProvider = Versions.class, headerHeading = "A data import/export tool for Redis.%n%n", footerHeading = "%nRun 'riot COMMAND --help' for more information on a command.%n%nFor more help on how to use RIOT, head to http://redis.github.io/riot%n", subcommands = {
DatabaseExport.class, DatabaseImport.class, FakerImport.class, FileExport.class, FileImport.class,
Generate.class, Ping.class, Replicate.class, Compare.class, GenerateCompletion.class })
public class Riot extends MainCommand {

public static final IExecutionStrategy DEFAULT_EXECUTION_STRATEGY = new RiotExecutionStrategy();

public static void main(String[] args) {
System.exit(new Riot().run(args));
}

@Override
protected CommandLine commandLine() {
CommandLine commandLine = super.commandLine();
commandLine.setExecutionStrategy(executionStrategy());
IExecutionStrategy defaultStrategy = commandLine.getExecutionStrategy();
commandLine.setExecutionStrategy(r -> execute(r, defaultStrategy));
configureCommandLine(commandLine);
return commandLine;
}
Expand All @@ -35,8 +37,22 @@ public static void configureCommandLine(CommandLine commandLine) {
commandLine.registerConverter(Range.class, new RangeConverter());
}

protected IExecutionStrategy executionStrategy() {
return DEFAULT_EXECUTION_STRATEGY;
protected int execute(ParseResult parseResult, IExecutionStrategy defaultStrategy) {
for (ParseResult subcommand : parseResult.subcommands()) {
Object command = subcommand.commandSpec().userObject();
if (AbstractImportCommand.class.isAssignableFrom(command.getClass())) {
AbstractImportCommand importCommand = (AbstractImportCommand) command;
for (ParseResult redisCommand : subcommand.subcommands()) {
if (redisCommand.isUsageHelpRequested()) {
return defaultStrategy.execute(redisCommand);
}
OperationCommand operationCommand = (OperationCommand) redisCommand.commandSpec().userObject();
importCommand.getImportOperationCommands().add(operationCommand);
}
return new RunFirst().execute(subcommand);
}
}
return defaultStrategy.execute(parseResult); // default execution strategy
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,15 @@ private void configure(RedisReaderArgs redisReaderArgs) {
}

@Override
protected IExecutionStrategy executionStrategy() {
IExecutionStrategy strategy = super.executionStrategy();
return r -> {
execute(r);
for (IExecutionStrategy config : configs) {
config.execute(r);
}
return strategy.execute(r);
};
protected int execute(ParseResult parseResult, IExecutionStrategy defaultStrategy) {
configure(parseResult);
for (IExecutionStrategy config : configs) {
config.execute(parseResult);
}
return defaultStrategy.execute(parseResult);
}

private int execute(ParseResult parseResult) {
private void configure(ParseResult parseResult) {
for (ParseResult subParseResult : parseResult.subcommands()) {
Object command = subParseResult.commandSpec().commandLine().getCommand();
if (command instanceof OperationCommand) {
Expand Down Expand Up @@ -99,7 +96,6 @@ private int execute(ParseResult parseResult) {
replicateCommand.setCompareMode(CompareMode.NONE);
}
}
return 0;
}
}

Expand Down

0 comments on commit b1e3eba

Please sign in to comment.