From b1e3ebacc9bb0d72cc2de41c4d85faab9b7671dc Mon Sep 17 00:00:00 2001 From: jruaux Date: Wed, 9 Oct 2024 00:00:10 -0700 Subject: [PATCH] fix: Execution strategy --- .../java/com/redis/riot/core/MainCommand.java | 40 +++++++++---------- .../src/main/java/com/redis/riot/Riot.java | 26 +++++++++--- .../com/redis/riot/RiotExecutionStrategy.java | 39 ------------------ .../riot/AbstractRiotApplicationTestBase.java | 18 ++++----- 4 files changed, 48 insertions(+), 75 deletions(-) delete mode 100644 plugins/riot/src/main/java/com/redis/riot/RiotExecutionStrategy.java diff --git a/core/riot-core/src/main/java/com/redis/riot/core/MainCommand.java b/core/riot-core/src/main/java/com/redis/riot/core/MainCommand.java index c855aec52..bcf008798 100644 --- a/core/riot-core/src/main/java/com/redis/riot/core/MainCommand.java +++ b/core/riot-core/src/main/java/com/redis/riot/core/MainCommand.java @@ -12,26 +12,6 @@ public class MainCommand extends BaseCommand implements Callable, 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); @@ -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; + } + } diff --git a/plugins/riot/src/main/java/com/redis/riot/Riot.java b/plugins/riot/src/main/java/com/redis/riot/Riot.java index 3908a4a04..2c3239dfa 100644 --- a/plugins/riot/src/main/java/com/redis/riot/Riot.java +++ b/plugins/riot/src/main/java/com/redis/riot/Riot.java @@ -1,6 +1,7 @@ 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; @@ -8,6 +9,8 @@ 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 = { @@ -15,8 +18,6 @@ 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)); } @@ -24,7 +25,8 @@ public static void main(String[] 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; } @@ -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 } } diff --git a/plugins/riot/src/main/java/com/redis/riot/RiotExecutionStrategy.java b/plugins/riot/src/main/java/com/redis/riot/RiotExecutionStrategy.java deleted file mode 100644 index 99455fc13..000000000 --- a/plugins/riot/src/main/java/com/redis/riot/RiotExecutionStrategy.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.redis.riot; - -import com.redis.riot.operation.OperationCommand; - -import picocli.CommandLine.ExecutionException; -import picocli.CommandLine.IExecutionStrategy; -import picocli.CommandLine.ParameterException; -import picocli.CommandLine.ParseResult; -import picocli.CommandLine.RunFirst; -import picocli.CommandLine.RunLast; - -public class RiotExecutionStrategy implements IExecutionStrategy { - - private IExecutionStrategy defaultStrategy = new RunLast(); - - public void setDefaultStrategy(IExecutionStrategy defaultStrategy) { - this.defaultStrategy = defaultStrategy; - } - - @Override - public int execute(ParseResult parseResult) throws ExecutionException, ParameterException { - 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 new RunLast().execute(redisCommand); - } - importCommand.getImportOperationCommands() - .add((OperationCommand) redisCommand.commandSpec().userObject()); - } - return new RunFirst().execute(subcommand); - } - } - return defaultStrategy.execute(parseResult); // default execution strategy - } - -} diff --git a/plugins/riot/src/test/java/com/redis/riot/AbstractRiotApplicationTestBase.java b/plugins/riot/src/test/java/com/redis/riot/AbstractRiotApplicationTestBase.java index cc1230335..2dd06ac8e 100644 --- a/plugins/riot/src/test/java/com/redis/riot/AbstractRiotApplicationTestBase.java +++ b/plugins/riot/src/test/java/com/redis/riot/AbstractRiotApplicationTestBase.java @@ -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) { @@ -99,7 +96,6 @@ private int execute(ParseResult parseResult) { replicateCommand.setCompareMode(CompareMode.NONE); } } - return 0; } }