Skip to content

Commit

Permalink
Feature/move subclass in pantheon command (PegaSysEng#1272)
Browse files Browse the repository at this point in the history
* clean PantheonCommand class

- organize imports
- move `RpcApisConverter` and `RpcApisConversionException` to separate files

* spotlessApply

* register converters

* Revert "register converters"

This reverts commit aec4735.
  • Loading branch information
AbdelStark authored and notlesh committed May 4, 2019
1 parent 33ccff1 commit 4cf8e91
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
import tech.pegasys.pantheon.Runner;
import tech.pegasys.pantheon.RunnerBuilder;
import tech.pegasys.pantheon.cli.PublicKeySubCommand.KeyLoader;
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.rlp.RLPSubCommand;
import tech.pegasys.pantheon.config.GenesisConfigFile;
import tech.pegasys.pantheon.consensus.clique.jsonrpc.CliqueRpcApis;
import tech.pegasys.pantheon.consensus.ibft.jsonrpc.IbftRpcApis;
import tech.pegasys.pantheon.controller.KeyPairUtil;
import tech.pegasys.pantheon.controller.PantheonController;
import tech.pegasys.pantheon.ethereum.core.Address;
Expand Down Expand Up @@ -86,10 +85,8 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -124,29 +121,6 @@
footer = "Pantheon is licensed under the Apache License 2.0")
public class PantheonCommand implements DefaultCommandValues, Runnable {

static class RpcApisConverter implements CommandLine.ITypeConverter<RpcApi> {

@Override
public RpcApi convert(final String name) throws RpcApisConversionException {
final String uppercaseName = name.trim().toUpperCase();

return Stream.<Function<String, Optional<RpcApi>>>of(
RpcApis::valueOf, CliqueRpcApis::valueOf, IbftRpcApis::valueOf)
.map(f -> f.apply(uppercaseName))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.orElseThrow(() -> new RpcApisConversionException("Invalid value: " + name));
}
}

static class RpcApisConversionException extends Exception {

public RpcApisConversionException(final String s) {
super(s);
}
}

private final Logger logger;

private CommandLine commandLine;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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
*
* http://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.converter;

import tech.pegasys.pantheon.cli.converter.exception.RpcApisConversionException;
import tech.pegasys.pantheon.consensus.clique.jsonrpc.CliqueRpcApis;
import tech.pegasys.pantheon.consensus.ibft.jsonrpc.IbftRpcApis;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApi;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApis;

import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

import picocli.CommandLine;

public class RpcApisConverter implements CommandLine.ITypeConverter<RpcApi> {

@Override
public RpcApi convert(final String name) throws RpcApisConversionException {
return Stream.<Function<String, Optional<RpcApi>>>of(
RpcApis::valueOf, CliqueRpcApis::valueOf, IbftRpcApis::valueOf)
.map(f -> f.apply(name.trim().toUpperCase()))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.orElseThrow(() -> new RpcApisConversionException(name));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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
*
* http://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.converter.exception;

import static java.lang.String.format;

public final class RpcApisConversionException extends Exception {

public RpcApisConversionException(final String name) {
super(format("Invalid value: %s", name));
}
}

0 comments on commit 4cf8e91

Please sign in to comment.