Skip to content

Commit

Permalink
Merge pull request #1977 from rsksmart/named-cli-args-revert
Browse files Browse the repository at this point in the history
Revert named cli args feature
  • Loading branch information
Vovchyk authored Feb 20, 2023
2 parents 04ddf65 + 48a3e92 commit b5ee19e
Show file tree
Hide file tree
Showing 20 changed files with 166 additions and 467 deletions.
8 changes: 0 additions & 8 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,6 @@
<sha256 value="28ebb2998bc7d7acb25078526971640892000f3413586ff42d611f1043bfec30" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="info.picocli" name="picocli" version="4.6.3">
<artifact name="picocli-4.6.3.jar">
<sha256 value="b0a5159e926de8084ff066025142270443533656bc599b8bb31d14d11fd138a4" origin="Generated by Gradle"/>
</artifact>
<artifact name="picocli-4.6.3.pom">
<sha256 value="24150c7dbdc8a5bb987cbeb4898ee1515d2893d3f6224acfc563da1a7fd5c283" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="io.netty" name="netty-buffer" version="4.0.56.Final">
<artifact name="netty-buffer-4.0.56.Final.jar">
<sha256 value="055f5ccfd7f9683c5d961fbf4466778d5b91ebf8b1f2ddd2eed539a82352b695" origin="Generated by Gradle"/>
Expand Down
3 changes: 0 additions & 3 deletions rskj-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ ext {
mapdbVer : '2.0-beta13',
jsonrpc4jVer : '1.6',
jaxwsRtVer : '2.3.5',
picocliVer : '4.6.3',

bitcoinjThinVer: '0.14.4-rsk-14',
rskjNativeVer: '1.3.0',
Expand All @@ -95,7 +94,6 @@ ext {
mapdbLib : "org.mapdb:mapdb:${libVersions.mapdbVer}",
jsonrpc4jLib : "com.github.briandilley.jsonrpc4j:jsonrpc4j:${libVersions.jsonrpc4jVer}",
jaxwsRtLib : "com.sun.xml.ws:jaxws-rt:${libVersions.jaxwsRtVer}",
picocliLib : "info.picocli:picocli:${libVersions.picocliVer}",

bitcoinjThinLib : "co.rsk.bitcoinj:bitcoinj-thin:${libVersions.bitcoinjThinVer}",
rskjNativeLib : "co.rsk:native:${libVersions.rskjNativeVer}",
Expand Down Expand Up @@ -143,7 +141,6 @@ dependencies {
implementation "${libs.mapdbLib}"
implementation "${libs.jsonrpc4jLib}"
implementation "${libs.jaxwsRtLib}"
implementation "${libs.picocliLib}"

implementation "${libs.bitcoinjThinLib}"
implementation "${libs.rskjNativeLib}"
Expand Down
7 changes: 1 addition & 6 deletions rskj-core/src/main/java/co/rsk/RskContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,9 @@ public class RskContext implements NodeContext, NodeBootstrapper {
/***** Constructors ***********************************************************************************************/

public RskContext(String[] args) {
this(args, false);
}

public RskContext(String[] args, boolean ignoreUnmatchedArgs) {
this(new CliArgs.Parser<>(
NodeCliOptions.class,
NodeCliFlags.class,
ignoreUnmatchedArgs
NodeCliFlags.class
).parse(args));
}

Expand Down
87 changes: 29 additions & 58 deletions rskj-core/src/main/java/co/rsk/cli/CliArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,10 @@ public static class Parser<O extends Enum<O> & OptionalizableCliArg, F extends E

private final EnumSet<O> options;
private final EnumSet<F> flags;
private final boolean ignoreUnmatchedArgs;

public Parser(Class<O> optionsClass, Class<F> flagsClass) {
this(optionsClass, flagsClass, false);
}

public Parser(Class<O> optionsClass, Class<F> flagsClass, boolean ignoreUnmatchedArgs) {
this.options = EnumSet.allOf(optionsClass);
this.flags = EnumSet.allOf(flagsClass);
this.ignoreUnmatchedArgs = ignoreUnmatchedArgs;
}

public CliArgs<O, F> parse(String[] args) {
Expand All @@ -98,7 +92,27 @@ public CliArgs<O, F> parse(String[] args) {
for (int i = 0; i < args.length; i++) {
switch (args[i].charAt(0)) {
case '-':
this.parseArguments(args, i, options, flags, paramValueMap);
if (args[i].length() < 2) {
throw new IllegalArgumentException("You must provide an option name, e.g. -d");
}
char currentChar = Character.toLowerCase(args[i].charAt(1));
if (currentChar == '-') {
if (args[i].length() < 3) {
throw new IllegalArgumentException("You must provide a flag name, e.g. --quiet");
}
flags.add(getFlagByName(args[i].substring(2, args[i].length())));
} else if (currentChar == 'x') {
String arg = args[i].substring(2);
paramValueMap.putAll(parseArgToMap(arg));
} else {
if (args.length - 1 == i) {
throw new IllegalArgumentException(
String.format("A value must be provided after the option -%s", args[i])
);
}
options.put(getOptionByName(args[i].substring(1, args[i].length())), args[i + 1]);
i++;
}
break;
default:
arguments.add(args[i]);
Expand All @@ -119,65 +133,22 @@ public CliArgs<O, F> parse(String[] args) {
return new CliArgs<>(arguments, options, flags, paramValueMap);
}

private void parseArguments(String[] args, int i, Map<O, String> options, Set<F> flags, Map<String, String> paramValueMap) {
if (args[i].length() < 2) {
throw new IllegalArgumentException("You must provide an option name, e.g. -d");
}
char currentChar = Character.toLowerCase(args[i].charAt(1));
if (currentChar == '-') {
if (args[i].length() < 3) {
throw new IllegalArgumentException("You must provide a flag name, e.g. --quiet");
}

F flag = getFlagByName(args[i].substring(2, args[i].length()));

if (flag != null) {
flags.add(flag);
}
} else if (currentChar == 'x') {
String arg = args[i].substring(2);
paramValueMap.putAll(parseArgToMap(arg));
} else {
if (args.length - 1 == i) {
throw new IllegalArgumentException(
String.format("A value must be provided after the option -%s", args[i])
);
}

O option = getOptionByName(args[i].substring(1, args[i].length()));

if (option != null) {
options.put(option, args[i + 1]);
}

i++;
}
}

private F getFlagByName(String flagName) {
F flagFound = flags.stream()
return flags.stream()
.filter(flag -> flag.getName().equals(flagName))
.findFirst()
.orElse(null);

if (flagFound == null && !this.ignoreUnmatchedArgs) {
throw new NoSuchElementException(String.format("--%s is not a valid flag", flagName));
}

return flagFound;
.orElseThrow(
() -> new NoSuchElementException(String.format("--%s is not a valid flag", flagName))
);
}

private O getOptionByName(String optionName) {
O option = options.stream()
return options.stream()
.filter(opt -> opt.getName().equals(optionName))
.findFirst()
.orElse(null);

if (option == null && !this.ignoreUnmatchedArgs) {
throw new NoSuchElementException(String.format("-%s is not a valid option", optionName));
}

return option;
.orElseThrow(
() -> new NoSuchElementException(String.format("-%s is not a valid option", optionName))
);
}

/**
Expand Down
25 changes: 7 additions & 18 deletions rskj-core/src/main/java/co/rsk/cli/CliToolRskContextAware.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@
/**
* An abstract class for cli tools that need {@link RskContext}. Lifecycle of the {@link RskContext} instance
* is being managed by {@link CliToolRskContextAware}.
* <p>
*
* Also {@link CliToolRskContextAware} provides a logger instance for derived classes.
*/
public abstract class CliToolRskContextAware {

private static final Logger logger = LoggerFactory.getLogger("clitool");
protected RskContext ctx;
protected NodeStopper stopper;

protected static CliToolRskContextAware create(@Nonnull Class<?> cliToolClass) {
Objects.requireNonNull(cliToolClass, "cliToolClass should not be null");
Expand All @@ -59,27 +57,18 @@ public void execute(@Nonnull String[] args) {
execute(args, () -> new RskContext(args), System::exit);
}

public void execute(@Nonnull String[] args, @Nonnull Factory<RskContext> contextFactory, @Nonnull NodeStopper nodeStopper) {
public void execute(@Nonnull String[] args, @Nonnull Factory<RskContext> contextFactory, @Nonnull NodeStopper stopper) {
Objects.requireNonNull(args, "args should not be null");
Objects.requireNonNull(contextFactory, "contextFactory should not be null");
Objects.requireNonNull(nodeStopper, "stopper should not be null");
Objects.requireNonNull(stopper, "stopper should not be null");

String cliToolName = getClass().getSimpleName();

// Ignore contextFactory if ctx was set previously
// in order to allow compatibility between picocli
// and old cli tool version
if (this.ctx == null) {
this.ctx = contextFactory.create();
}
// Ignore nodeStopper if stopper was set previously
// in order to allow compatibility between picocli
// and old cli tool version
if (this.stopper == null) {
this.stopper = nodeStopper;
}

RskContext ctx = null;
// not using try-with-resources because System::exit (from stopper) prevents autocloseable closing
try {
ctx = contextFactory.create();

printInfo("{} started", cliToolName);

RskSystemProperties rskSystemProperties = ctx.getRskSystemProperties();
Expand Down
39 changes: 0 additions & 39 deletions rskj-core/src/main/java/co/rsk/cli/PicoCliToolRskContextAware.java

This file was deleted.

This file was deleted.

20 changes: 8 additions & 12 deletions rskj-core/src/main/java/co/rsk/cli/tools/ConnectBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
*/
package co.rsk.cli.tools;

import co.rsk.cli.PicoCliToolRskContextAware;
import co.rsk.RskContext;
import co.rsk.cli.CliToolRskContextAware;
import co.rsk.trie.TrieStore;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.core.Block;
import org.ethereum.core.BlockFactory;
import org.ethereum.core.Blockchain;
import org.ethereum.db.BlockStore;
import org.ethereum.db.ReceiptStore;
import picocli.CommandLine;

import javax.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -39,36 +40,31 @@
* Required cli args:
* - args[0] - file path
*/
@CommandLine.Command(name = "connect-blocks", mixinStandardHelpOptions = true, version = "connect-blocks 1.0",
description = "Connects blocks to a chain from external source file")
public class ConnectBlocks extends PicoCliToolRskContextAware {

@CommandLine.Option(names = {"-f", "--file"}, description = "Path to a file with blocks to connect", required = true)
private String filePath;
public class ConnectBlocks extends CliToolRskContextAware {

public static void main(String[] args) throws IOException {
create(MethodHandles.lookup().lookupClass()).execute(args);
}

@Override
public Integer call() throws IOException {
protected void onExecute(@Nonnull String[] args, @Nonnull RskContext ctx) throws Exception {
BlockFactory blockFactory = ctx.getBlockFactory();
Blockchain blockchain = ctx.getBlockchain();
TrieStore trieStore = ctx.getTrieStore();
BlockStore blockStore = ctx.getBlockStore();
ReceiptStore receiptStore = ctx.getReceiptStore();

String filename = args[0];

long startTime = System.currentTimeMillis();

try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
connectBlocks(blockFactory, blockchain, trieStore, blockStore, receiptStore, reader);
}

long endTime = System.currentTimeMillis();

printInfo("Duration: " + (endTime - startTime) + " millis");

return 0;
}

private void connectBlocks(BlockFactory blockFactory, Blockchain blockchain, TrieStore trieStore, BlockStore blockStore, ReceiptStore receiptStore, BufferedReader reader) throws IOException {
Expand Down
Loading

0 comments on commit b5ee19e

Please sign in to comment.