Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
[PIE-1741] Refactor CLI (#1627)
Browse files Browse the repository at this point in the history
- refactor packages
- remove `PantheonExceptionHandler` inner class and create a separate class
- introduce subcommand package
  • Loading branch information
AbdelStark committed Jul 2, 2019
1 parent d144a20 commit 94f39d1
Show file tree
Hide file tree
Showing 30 changed files with 152 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
*/
package tech.pegasys.pantheon.tests.acceptance.dsl.node;

import static tech.pegasys.pantheon.cli.NetworkName.DEV;
import static tech.pegasys.pantheon.cli.config.NetworkName.DEV;

import tech.pegasys.pantheon.Runner;
import tech.pegasys.pantheon.RunnerBuilder;
import tech.pegasys.pantheon.cli.EthNetworkConfig;
import tech.pegasys.pantheon.cli.config.EthNetworkConfig;
import tech.pegasys.pantheon.controller.KeyPairUtil;
import tech.pegasys.pantheon.controller.PantheonController;
import tech.pegasys.pantheon.controller.PantheonControllerBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package tech.pegasys.pantheon;

import tech.pegasys.pantheon.cli.EthNetworkConfig;
import tech.pegasys.pantheon.cli.config.EthNetworkConfig;
import tech.pegasys.pantheon.controller.PantheonController;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.ethereum.ProtocolContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static tech.pegasys.pantheon.cli.CommandLineUtils.checkOptionDependencies;
import static tech.pegasys.pantheon.cli.DefaultCommandValues.getDefaultPantheonDataPath;
import static tech.pegasys.pantheon.cli.NetworkName.MAINNET;
import static tech.pegasys.pantheon.cli.config.NetworkName.MAINNET;
import static tech.pegasys.pantheon.cli.util.CommandLineUtils.checkOptionDependencies;
import static tech.pegasys.pantheon.controller.PantheonController.DATABASE_PATH;
import static tech.pegasys.pantheon.ethereum.graphql.GraphQLConfiguration.DEFAULT_GRAPHQL_HTTP_PORT;
import static tech.pegasys.pantheon.ethereum.jsonrpc.JsonRpcConfiguration.DEFAULT_JSON_RPC_PORT;
Expand All @@ -31,14 +31,22 @@
import tech.pegasys.pantheon.PantheonInfo;
import tech.pegasys.pantheon.Runner;
import tech.pegasys.pantheon.RunnerBuilder;
import tech.pegasys.pantheon.cli.PublicKeySubCommand.KeyLoader;
import tech.pegasys.pantheon.cli.config.EthNetworkConfig;
import tech.pegasys.pantheon.cli.config.NetworkName;
import tech.pegasys.pantheon.cli.converter.MetricCategoryConverter;
import tech.pegasys.pantheon.cli.converter.RpcApisConverter;
import tech.pegasys.pantheon.cli.custom.CorsAllowedOriginsProperty;
import tech.pegasys.pantheon.cli.custom.JsonRPCWhitelistHostsProperty;
import tech.pegasys.pantheon.cli.custom.RpcAuthFileValidator;
import tech.pegasys.pantheon.cli.operator.OperatorSubCommand;
import tech.pegasys.pantheon.cli.rlp.RLPSubCommand;
import tech.pegasys.pantheon.cli.error.PantheonExceptionHandler;
import tech.pegasys.pantheon.cli.subcommands.PasswordSubCommand;
import tech.pegasys.pantheon.cli.subcommands.PublicKeySubCommand;
import tech.pegasys.pantheon.cli.subcommands.PublicKeySubCommand.KeyLoader;
import tech.pegasys.pantheon.cli.subcommands.blocks.BlocksSubCommand;
import tech.pegasys.pantheon.cli.subcommands.operator.OperatorSubCommand;
import tech.pegasys.pantheon.cli.subcommands.rlp.RLPSubCommand;
import tech.pegasys.pantheon.cli.util.ConfigOptionSearchAndRunHandler;
import tech.pegasys.pantheon.cli.util.VersionProvider;
import tech.pegasys.pantheon.config.GenesisConfigFile;
import tech.pegasys.pantheon.controller.KeyPairUtil;
import tech.pegasys.pantheon.controller.PantheonController;
Expand Down Expand Up @@ -470,7 +478,7 @@ void setBannedNodeIds(final List<String> values) {
paramLabel = "<LOG VERBOSITY LEVEL>",
description =
"Logging verbosity levels: OFF, FATAL, WARN, INFO, DEBUG, TRACE, ALL (default: INFO)")
private final Level logLevel = null;
private final Level logLevel = Level.INFO;

@Option(
names = {"--miner-enabled"},
Expand Down Expand Up @@ -577,38 +585,6 @@ void setBannedNodeIds(final List<String> values) {
private Collection<EnodeURL> staticNodes;
private PantheonController<?> pantheonController;

// Inner class so we can get to loggingLevel.
public class PantheonExceptionHandler
extends CommandLine.AbstractHandler<List<Object>, PantheonExceptionHandler>
implements CommandLine.IExceptionHandler2<List<Object>> {

@Override
public List<Object> handleParseException(final ParameterException ex, final String[] args) {
if (logLevel != null && Level.DEBUG.isMoreSpecificThan(logLevel)) {
ex.printStackTrace(err());
} else {
err().println(ex.getMessage());
}
if (!CommandLine.UnmatchedArgumentException.printSuggestions(ex, err())) {
ex.getCommandLine().usage(err(), ansi());
}
return returnResultOrExit(null);
}

@Override
public List<Object> handleExecutionException(
final ExecutionException ex, final CommandLine.ParseResult parseResult) {
return throwOrExit(ex);
}

@Override
protected PantheonExceptionHandler self() {
return this;
}
}

private final Supplier<PantheonExceptionHandler> exceptionHandlerSupplier =
Suppliers.memoize(PantheonExceptionHandler::new);
private final Supplier<MetricsSystem> metricsSystem =
Suppliers.memoize(() -> PrometheusMetricsSystem.init(metricsConfiguration()));

Expand Down Expand Up @@ -857,7 +833,7 @@ private PantheonCommand controller() {
return this;
}

PantheonController<?> buildController() {
public PantheonController<?> buildController() {
try {
return controllerBuilderFactory
.fromEthNetworkConfig(updateNetworkConfig(getNetwork()))
Expand Down Expand Up @@ -965,7 +941,7 @@ private WebSocketConfiguration webSocketConfiguration() {
return webSocketConfiguration;
}

MetricsConfiguration metricsConfiguration() {
public MetricsConfiguration metricsConfiguration() {
if (isMetricsEnabled && isMetricsPushEnabled) {
throw new ParameterException(
this.commandLine,
Expand Down Expand Up @@ -1353,7 +1329,7 @@ private Path pluginsDir() {
}
}

File nodePrivateKeyFile() {
public File nodePrivateKeyFile() {
File nodePrivateKeyFile = null;
if (isFullInstantiation()) {
nodePrivateKeyFile = standaloneCommands.nodePrivateKeyFile;
Expand Down Expand Up @@ -1446,14 +1422,18 @@ public MetricsSystem getMetricsSystem() {
return metricsSystem.get();
}

public PantheonExceptionHandler exceptionHandler() {
return exceptionHandlerSupplier.get();
}

private Set<EnodeURL> loadStaticNodes() throws IOException {
final String staticNodesFilename = "static-nodes.json";
final Path staticNodesPath = dataDir().resolve(staticNodesFilename);

return StaticNodesParser.fromPath(staticNodesPath);
}

public PantheonExceptionHandler exceptionHandler() {
return new PantheonExceptionHandler(this::getLogLevel);
}

private Level getLogLevel() {
return logLevel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.cli;
package tech.pegasys.pantheon.cli.config;

import static java.nio.charset.StandardCharsets.UTF_8;
import static tech.pegasys.pantheon.ethereum.p2p.config.DiscoveryConfiguration.GOERLI_BOOTSTRAP_NODES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.cli;
package tech.pegasys.pantheon.cli.config;

public enum NetworkName {
MAINNET,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.cli.error;

import java.util.List;
import java.util.function.Supplier;

import org.apache.logging.log4j.Level;
import picocli.CommandLine;

public class PantheonExceptionHandler
extends CommandLine.AbstractHandler<List<Object>, PantheonExceptionHandler>
implements CommandLine.IExceptionHandler2<List<Object>> {

private final Supplier<Level> levelSupplier;

public PantheonExceptionHandler(final Supplier<Level> levelSupplier) {
this.levelSupplier = levelSupplier;
}

@Override
public List<Object> handleParseException(
final CommandLine.ParameterException ex, final String[] args) {
final Level logLevel = levelSupplier.get();
if (logLevel != null && Level.DEBUG.isMoreSpecificThan(logLevel)) {
ex.printStackTrace(err());
} else {
err().println(ex.getMessage());
}
if (!CommandLine.UnmatchedArgumentException.printSuggestions(ex, err())) {
ex.getCommandLine().usage(err(), ansi());
}
return returnResultOrExit(null);
}

@Override
public List<Object> handleExecutionException(
final CommandLine.ExecutionException ex, final CommandLine.ParseResult parseResult) {
return throwOrExit(ex);
}

@Override
protected PantheonExceptionHandler self() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.cli;
package tech.pegasys.pantheon.cli.subcommands;

import static com.google.common.base.Preconditions.checkNotNull;
import static tech.pegasys.pantheon.cli.PasswordSubCommand.COMMAND_NAME;
import static tech.pegasys.pantheon.cli.subcommands.PasswordSubCommand.COMMAND_NAME;

import tech.pegasys.pantheon.cli.PasswordSubCommand.HashSubCommand;
import tech.pegasys.pantheon.cli.PantheonCommand;
import tech.pegasys.pantheon.cli.subcommands.PasswordSubCommand.HashSubCommand;

import java.io.PrintStream;

Expand All @@ -31,9 +32,9 @@
description = "This command provides password related actions.",
mixinStandardHelpOptions = true,
subcommands = {HashSubCommand.class})
class PasswordSubCommand implements Runnable {
public class PasswordSubCommand implements Runnable {

static final String COMMAND_NAME = "password";
public static final String COMMAND_NAME = "password";

@SuppressWarnings("unused")
@ParentCommand
Expand All @@ -45,7 +46,7 @@ class PasswordSubCommand implements Runnable {

final PrintStream out;

PasswordSubCommand(final PrintStream out) {
public PasswordSubCommand(final PrintStream out) {
this.out = out;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.cli;
package tech.pegasys.pantheon.cli.subcommands;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.UTF_8;
import static tech.pegasys.pantheon.cli.DefaultCommandValues.MANDATORY_FILE_FORMAT_HELP;
import static tech.pegasys.pantheon.cli.PublicKeySubCommand.COMMAND_NAME;
import static tech.pegasys.pantheon.cli.subcommands.PublicKeySubCommand.COMMAND_NAME;

import tech.pegasys.pantheon.cli.PublicKeySubCommand.AddressSubCommand;
import tech.pegasys.pantheon.cli.PublicKeySubCommand.ExportSubCommand;
import tech.pegasys.pantheon.cli.PantheonCommand;
import tech.pegasys.pantheon.cli.subcommands.PublicKeySubCommand.AddressSubCommand;
import tech.pegasys.pantheon.cli.subcommands.PublicKeySubCommand.ExportSubCommand;
import tech.pegasys.pantheon.crypto.SECP256K1;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.ethereum.core.Address;
Expand Down Expand Up @@ -46,10 +47,10 @@
description = "This command provides node public key related actions.",
mixinStandardHelpOptions = true,
subcommands = {ExportSubCommand.class, AddressSubCommand.class})
class PublicKeySubCommand implements Runnable {
public class PublicKeySubCommand implements Runnable {
private static final Logger LOG = LogManager.getLogger();

static final String COMMAND_NAME = "public-key";
public static final String COMMAND_NAME = "public-key";

@SuppressWarnings("unused")
@ParentCommand
Expand All @@ -62,7 +63,7 @@ class PublicKeySubCommand implements Runnable {
private final PrintStream out;
private final KeyLoader keyLoader;

PublicKeySubCommand(final PrintStream out, final KeyLoader keyLoader) {
public PublicKeySubCommand(final PrintStream out, final KeyLoader keyLoader) {
this.out = out;
this.keyLoader = keyLoader;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.cli;
package tech.pegasys.pantheon.cli.subcommands.blocks;

import static com.google.common.base.Preconditions.checkNotNull;
import static tech.pegasys.pantheon.cli.BlocksSubCommand.COMMAND_NAME;
import static tech.pegasys.pantheon.cli.DefaultCommandValues.MANDATORY_FILE_FORMAT_HELP;
import static tech.pegasys.pantheon.cli.subcommands.blocks.BlocksSubCommand.COMMAND_NAME;

import tech.pegasys.pantheon.cli.BlocksSubCommand.ImportSubCommand;
import tech.pegasys.pantheon.cli.PantheonCommand;
import tech.pegasys.pantheon.cli.subcommands.blocks.BlocksSubCommand.ImportSubCommand;
import tech.pegasys.pantheon.metrics.prometheus.MetricsConfiguration;
import tech.pegasys.pantheon.metrics.prometheus.MetricsService;
import tech.pegasys.pantheon.util.BlockImporter;
Expand Down Expand Up @@ -45,10 +46,10 @@
description = "This command provides blocks related actions.",
mixinStandardHelpOptions = true,
subcommands = {ImportSubCommand.class})
class BlocksSubCommand implements Runnable {
public class BlocksSubCommand implements Runnable {
private static final Logger LOG = LogManager.getLogger();

static final String COMMAND_NAME = "blocks";
public static final String COMMAND_NAME = "blocks";

@SuppressWarnings("unused")
@ParentCommand
Expand All @@ -61,7 +62,7 @@ class BlocksSubCommand implements Runnable {
private final BlockImporter blockImporter;
private final PrintStream out;

BlocksSubCommand(final BlockImporter blockImporter, final PrintStream out) {
public BlocksSubCommand(final BlockImporter blockImporter, final PrintStream out) {
this.blockImporter = blockImporter;
this.out = out;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.cli.operator;
package tech.pegasys.pantheon.cli.subcommands.operator;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.UTF_8;
import static tech.pegasys.pantheon.cli.DefaultCommandValues.MANDATORY_DIRECTORY_FORMAT_HELP;
import static tech.pegasys.pantheon.cli.DefaultCommandValues.MANDATORY_FILE_FORMAT_HELP;
import static tech.pegasys.pantheon.cli.DefaultCommandValues.MANDATORY_PATH_FORMAT_HELP;
import static tech.pegasys.pantheon.cli.operator.OperatorSubCommand.COMMAND_NAME;
import static tech.pegasys.pantheon.cli.subcommands.operator.OperatorSubCommand.COMMAND_NAME;

import tech.pegasys.pantheon.cli.PantheonCommand;
import tech.pegasys.pantheon.config.JsonGenesisConfigOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.cli.rlp;
package tech.pegasys.pantheon.cli.subcommands.rlp;

import tech.pegasys.pantheon.consensus.ibft.IbftExtraData;
import tech.pegasys.pantheon.ethereum.core.Address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.cli.rlp;
package tech.pegasys.pantheon.cli.subcommands.rlp;

import tech.pegasys.pantheon.util.bytes.BytesValue;

Expand Down
Loading

0 comments on commit 94f39d1

Please sign in to comment.