Skip to content

Commit

Permalink
Added --version flag to subcommands [hyperledger#2762] (hyperledger#3577
Browse files Browse the repository at this point in the history
)

* added --version flag to subcommands (hyperledger#2762)

Signed-off-by: Davide Savazzi <davide@davidesavazzi.net>

Co-authored-by: Sally MacFarlane <sally.macfarlane@consensys.net>
  • Loading branch information
2 people authored and garyschulte committed May 2, 2022
1 parent a7ef9d4 commit 1f33803
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.hyperledger.besu.cli.BesuCommand;
import org.hyperledger.besu.cli.subcommands.PasswordSubCommand.HashSubCommand;
import org.hyperledger.besu.cli.util.VersionProvider;

import java.io.PrintStream;

Expand All @@ -33,6 +34,7 @@
name = COMMAND_NAME,
description = "This command provides password related actions.",
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class,
subcommands = {HashSubCommand.class})
public class PasswordSubCommand implements Runnable {

Expand Down Expand Up @@ -60,7 +62,8 @@ public void run() {
@Command(
name = "hash",
description = "This command generates the hash of a given password.",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
static class HashSubCommand implements Runnable {

@SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"}) // PicoCLI requires non-final Strings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.hyperledger.besu.cli.options.stable.NodePrivateKeyFileOption;
import org.hyperledger.besu.cli.subcommands.PublicKeySubCommand.AddressSubCommand;
import org.hyperledger.besu.cli.subcommands.PublicKeySubCommand.ExportSubCommand;
import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.crypto.SignatureAlgorithmFactory;
import org.hyperledger.besu.crypto.SignatureAlgorithmType;
Expand Down Expand Up @@ -51,6 +52,7 @@
name = COMMAND_NAME,
description = "This command provides node public key related actions.",
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class,
subcommands = {ExportSubCommand.class, AddressSubCommand.class})
public class PublicKeySubCommand implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(PublicKeySubCommand.class);
Expand Down Expand Up @@ -87,7 +89,8 @@ public void run() {
@Command(
name = "export",
description = "This command outputs the node public key. Default output is standard output.",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
static class ExportSubCommand extends KeyPairSubcommand implements Runnable {

@Option(
Expand Down Expand Up @@ -117,7 +120,8 @@ public void run() {
description =
"This command outputs the node's account address. "
+ "Default output is standard output.",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
static class AddressSubCommand extends KeyPairSubcommand implements Runnable {

@Option(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hyperledger.besu.cli.DefaultCommandValues;
import org.hyperledger.besu.cli.custom.JsonRPCAllowlistHostsProperty;
import org.hyperledger.besu.cli.options.stable.LoggingLevelOption;
import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration;
import org.hyperledger.besu.ethereum.retesteth.RetestethConfiguration;
import org.hyperledger.besu.ethereum.retesteth.RetestethService;
Expand All @@ -38,7 +39,8 @@
@Command(
name = COMMAND_NAME,
description = "Run a Retesteth compatible server for reference tests.",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
@SuppressWarnings("unused")
public class RetestethSubCommand implements Runnable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hyperledger.besu.cli.BesuCommand;
import org.hyperledger.besu.cli.DefaultCommandValues;
import org.hyperledger.besu.cli.util.TomlConfigFileDefaultProvider;
import org.hyperledger.besu.cli.util.VersionProvider;

import java.io.PrintStream;
import java.nio.file.Path;
Expand All @@ -32,7 +33,8 @@
@Command(
name = COMMAND_NAME,
description = "This command provides basic Besu config validation (syntax only).",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
public class ValidateConfigSubCommand implements Runnable {

public static final String COMMAND_NAME = "validate-config";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.hyperledger.besu.cli.DefaultCommandValues;
import org.hyperledger.besu.cli.subcommands.blocks.BlocksSubCommand.ExportSubCommand;
import org.hyperledger.besu.cli.subcommands.blocks.BlocksSubCommand.ImportSubCommand;
import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.controller.BesuController;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Wei;
Expand Down Expand Up @@ -73,6 +74,7 @@
name = COMMAND_NAME,
description = "This command provides blocks related actions.",
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class,
subcommands = {ImportSubCommand.class, ExportSubCommand.class})
public class BlocksSubCommand implements Runnable {

Expand Down Expand Up @@ -118,7 +120,8 @@ public void run() {
@Command(
name = "import",
description = "This command imports blocks from a file into the database.",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
static class ImportSubCommand implements Runnable {
@SuppressWarnings("unused")
@ParentCommand
Expand Down Expand Up @@ -302,7 +305,8 @@ private void importRlpBlocks(final BesuController controller, final Path path)
@Command(
name = "export",
description = "This command exports a specific block, or list of blocks from storage.",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
static class ExportSubCommand implements Runnable {
@SuppressWarnings("unused")
@ParentCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.hyperledger.besu.cli.DefaultCommandValues.MANDATORY_LONG_FORMAT_HELP;

import org.hyperledger.besu.BesuInfo;
import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.controller.BesuController;
import org.hyperledger.besu.ethereum.api.query.StateBackupService;
import org.hyperledger.besu.ethereum.api.query.StateBackupService.BackupStatus;
Expand All @@ -41,7 +42,8 @@
@Command(
name = "x-backup-state",
description = "Backs up the state and accounts at a specified block.",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
public class BackupState implements Runnable {

@Option(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static java.nio.charset.StandardCharsets.UTF_8;

import org.hyperledger.besu.cli.DefaultCommandValues;
import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.config.JsonGenesisConfigOptions;
Expand Down Expand Up @@ -61,7 +62,8 @@
@Command(
name = "generate-blockchain-config",
description = "Generates node keypairs and genesis file with RLP encoded extra data.",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
class GenerateBlockchainConfig implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(GenerateBlockchainConfig.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.hyperledger.besu.cli.DefaultCommandValues.MANDATORY_LONG_FORMAT_HELP;
import static org.hyperledger.besu.ethereum.api.query.cache.TransactionLogBloomCacher.BLOCKS_PER_BLOOM_CACHE;

import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.controller.BesuController;
import org.hyperledger.besu.ethereum.api.query.cache.TransactionLogBloomCacher;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
Expand All @@ -36,7 +37,8 @@
@Command(
name = "generate-log-bloom-cache",
description = "Generate cached values of block log bloom filters.",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
public class GenerateLogBloomCache implements Runnable {

@Option(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.hyperledger.besu.cli.subcommands.operator.OperatorSubCommand.COMMAND_NAME;

import org.hyperledger.besu.cli.BesuCommand;
import org.hyperledger.besu.cli.util.VersionProvider;

import java.io.PrintStream;

Expand All @@ -30,6 +31,7 @@
name = COMMAND_NAME,
description = "Operator related actions such as generating configuration and caches.",
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class,
subcommands = {
GenerateBlockchainConfig.class,
GenerateLogBloomCache.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.hyperledger.besu.cli.DefaultCommandValues.MANDATORY_LONG_FORMAT_HELP;
import static org.hyperledger.besu.ethereum.trie.CompactEncoding.bytesToPath;

import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.config.JsonUtil;
import org.hyperledger.besu.controller.BesuController;
import org.hyperledger.besu.datatypes.Hash;
Expand Down Expand Up @@ -58,7 +59,8 @@
@Command(
name = "x-restore-state",
description = "Restores the chain from a previously generated backup-state.",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
public class RestoreState implements Runnable {

private static final Logger LOG = LoggerFactory.getLogger(RestoreState.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hyperledger.besu.cli.BesuCommand;
import org.hyperledger.besu.cli.DefaultCommandValues;
import org.hyperledger.besu.cli.subcommands.rlp.RLPSubCommand.EncodeSubCommand;
import org.hyperledger.besu.cli.util.VersionProvider;

import java.io.BufferedReader;
import java.io.BufferedWriter;
Expand All @@ -45,6 +46,7 @@
name = RLPSubCommand.COMMAND_NAME,
description = "This command provides RLP data related actions.",
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class,
subcommands = {EncodeSubCommand.class})
public class RLPSubCommand implements Runnable {

Expand Down Expand Up @@ -79,7 +81,8 @@ public void run() {
@Command(
name = "encode",
description = "This command encodes a JSON typed data into an RLP hex string.",
mixinStandardHelpOptions = true)
mixinStandardHelpOptions = true,
versionProvider = VersionProvider.class)
static class EncodeSubCommand implements Runnable {

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.BesuInfo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
Expand Down Expand Up @@ -62,6 +64,20 @@ public void passwordHashSubCommandHelpDisplaysHelp() {
.contains("This command generates the hash of a given password");
}

@Test
public void passwordSubCommandVersionDisplaysVersion() {
parseCommand("password", "--version");
assertThat(commandOutput.toString(UTF_8)).isEqualToIgnoringWhitespace(BesuInfo.version());
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void passwordHashSubCommandVersionDisplaysVersion() {
parseCommand("password", "hash", "--version");
assertThat(commandOutput.toString(UTF_8)).isEqualToIgnoringWhitespace(BesuInfo.version());
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void passwordHashSubCommandHashesPassword() {
parseCommand("password", "hash", "--password", "foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;

import org.hyperledger.besu.BesuInfo;
import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.crypto.SECPPrivateKey;
Expand Down Expand Up @@ -162,6 +163,13 @@ public void callingPublicKeySubCommandHelpMustDisplayUsage() {
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void callingPublicKeySubCommandVersionMustDisplayVersion() {
parseCommand(PUBLIC_KEY_SUBCOMMAND_NAME, "--version");
assertThat(commandOutput.toString(UTF_8)).isEqualToIgnoringWhitespace(BesuInfo.version());
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

// Export public key sub-sub-command
@Test
public void callingPublicKeyExportSubCommandHelpMustDisplayUsage() {
Expand All @@ -170,6 +178,13 @@ public void callingPublicKeyExportSubCommandHelpMustDisplayUsage() {
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void callingPublicKeyExportSubCommandVersionMustDisplayVersion() {
parseCommand(PUBLIC_KEY_SUBCOMMAND_NAME, PUBLIC_KEY_EXPORT_SUBCOMMAND_NAME, "--version");
assertThat(commandOutput.toString(UTF_8)).isEqualToIgnoringWhitespace(BesuInfo.version());
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void callingPublicKeyExportSubCommandWithoutPathMustWriteKeyToStandardOutput() {
final NodeKey nodeKey = getNodeKey();
Expand Down Expand Up @@ -258,6 +273,14 @@ public void callingPublicKeyExportAddressSubCommandHelpMustDisplayUsage() {
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void callingPublicKeyExportAddressSubCommandVersionMustDisplayVersion() {
parseCommand(
PUBLIC_KEY_SUBCOMMAND_NAME, PUBLIC_KEY_EXPORT_ADDRESS_SUBCOMMAND_NAME, "--version");
assertThat(commandOutput.toString(UTF_8)).isEqualToIgnoringWhitespace(BesuInfo.version());
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void callingPublicKeyExportAddressSubCommandWithoutPathMustWriteAddressToStandardOutput() {
final NodeKey nodeKey = getNodeKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.BesuInfo;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
Expand Down Expand Up @@ -60,6 +62,13 @@ public void callingValidateConfigSubCommandHelpMustDisplayUsage() {
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void callingValidateConfigSubCommandVersionMustDisplayVersion() {
parseCommand(VALIDATE_CONFIG_SUBCOMMAND_NAME, "--version");
assertThat(commandOutput.toString(UTF_8)).isEqualToIgnoringWhitespace(BesuInfo.version());
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void callingValidateConfigSubCommandWithNonExistentMustDisplayError() {
parseCommand(VALIDATE_CONFIG_SUBCOMMAND_NAME, "--config-file", "/non/existent/file");
Expand Down
Loading

0 comments on commit 1f33803

Please sign in to comment.