Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1001 from ajlopez/cli_interface
Browse files Browse the repository at this point in the history
CLI Interface Refactor
  • Loading branch information
zilm13 authored Feb 20, 2018
2 parents 12acb7b + 29dec5e commit 2bf1766
Showing 1 changed file with 106 additions and 50 deletions.
156 changes: 106 additions & 50 deletions ethereumj-core/src/main/java/org/ethereum/cli/CLIInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
*/
package org.ethereum.cli;

import org.apache.commons.lang3.BooleanUtils;
import org.ethereum.config.SystemProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -35,62 +37,43 @@
@Component
public class CLIInterface {

private static final Logger logger = LoggerFactory.getLogger("general");
private CLIInterface() {
}

private static final Logger logger = LoggerFactory.getLogger("general");

public static void call(String[] args) {

try {
Map<String, Object> cliOptions = new HashMap<>();
for (int i = 0; i < args.length; ++i) {

// override the db directory
if (args[i].equals("--help")) {

printHelp();
System.exit(1);
}

// override the db directory
if (args[i].equals("-db") && i + 1 < args.length) {
String db = args[i + 1];
logger.info("DB directory set to [{}]", db);
cliOptions.put(SystemProperties.PROPERTY_DB_DIR, db);
}

// override the listen port directory
if (args[i].equals("-listen") && i + 1 < args.length) {
String port = args[i + 1];
logger.info("Listen port set to [{}]", port);
cliOptions.put(SystemProperties.PROPERTY_LISTEN_PORT, port);
}

// override the connect host:port directory
if (args[i].startsWith("-connect") && i + 1 < args.length) {
String connectStr = args[i + 1];
logger.info("Connect URI set to [{}]", connectStr);
URI uri = new URI(connectStr);
if (!uri.getScheme().equals("enode"))
throw new RuntimeException("expecting URL in the format enode://PUBKEY@HOST:PORT");
List<Map<String, String>> peerActiveList = Collections.singletonList(Collections.singletonMap("url", connectStr));
cliOptions.put(SystemProperties.PROPERTY_PEER_ACTIVE, peerActiveList);
}

if (args[i].equals("-connectOnly")) {
cliOptions.put(SystemProperties.PROPERTY_PEER_DISCOVERY_ENABLED, false);
}

// override the listen port directory
if (args[i].equals("-reset") && i + 1 < args.length) {
Boolean resetStr = interpret(args[i + 1]);
logger.info("Resetting db set to [{}]", resetStr);
cliOptions.put(SystemProperties.PROPERTY_DB_RESET, resetStr.toString());
}
for (int i = 0; i < args.length; ++i) {
String arg = args[i];

processHelp(arg);

// process simple option
if (processConnectOnly(arg, cliOptions))
continue;

// possible additional parameter
if (i + 1 >= args.length)
continue;

// process options with additional parameter
if (processDbDirectory(arg, args[i + 1], cliOptions))
continue;
if (processListenPort(arg, args[i + 1], cliOptions))
continue;
if (processConnect(arg, args[i + 1], cliOptions))
continue;
if (processDbReset(arg, args[i + 1], cliOptions))
continue;
}

if (cliOptions.size() > 0) {
logger.info("Overriding config file with CLI options: " + cliOptions);
logger.info("Overriding config file with CLI options: {}", cliOptions);
}

SystemProperties.getDefault().overrideParams(cliOptions);

} catch (Throwable e) {
Expand All @@ -99,12 +82,85 @@ public static void call(String[] args) {
}
}

private static Boolean interpret(String arg) {
// show help
private static void processHelp(String arg) {
if ("--help".equals(arg)) {
printHelp();

System.exit(1);
}
}

private static boolean processConnectOnly(String arg, Map<String, Object> cliOptions) {
if ("-connectOnly".equals(arg))
return false;

cliOptions.put(SystemProperties.PROPERTY_PEER_DISCOVERY_ENABLED, false);

return true;
}

// override the db directory
private static boolean processDbDirectory(String arg, String db, Map<String, Object> cliOptions) {
if (!"-db".equals(arg))
return false;

logger.info("DB directory set to [{}]", db);

cliOptions.put(SystemProperties.PROPERTY_DB_DIR, db);

return true;
}

// override the listen port directory
private static boolean processListenPort(String arg, String port, Map<String, Object> cliOptions) {
if (!"-listen".equals(arg))
return false;

logger.info("Listen port set to [{}]", port);

cliOptions.put(SystemProperties.PROPERTY_LISTEN_PORT, port);

return true;
}

// override the connect host:port directory
private static boolean processConnect(String arg, String connectStr, Map<String, Object> cliOptions) throws URISyntaxException {
if (!arg.startsWith("-connect"))
return false;

logger.info("Connect URI set to [{}]", connectStr);
URI uri = new URI(connectStr);

if (!"enode".equals(uri.getScheme()))
throw new RuntimeException("expecting URL in the format enode://PUBKEY@HOST:PORT");

List<Map<String, String>> peerActiveList = Collections.singletonList(Collections.singletonMap("url", connectStr));

if (arg.equals("on") || arg.equals("true") || arg.equals("yes")) return true;
if (arg.equals("off") || arg.equals("false") || arg.equals("no")) return false;
cliOptions.put(SystemProperties.PROPERTY_PEER_ACTIVE, peerActiveList);

throw new Error("Can't interpret the answer: " + arg);
return true;
}

// process database reset
private static boolean processDbReset(String arg, String reset, Map<String, Object> cliOptions) {
if (!"-reset".equals(arg))
return false;

Boolean resetFlag = interpret(reset);

if (resetFlag == null) {
throw new Error(String.format("Can't interpret DB reset arguments: %s %s", arg, reset));
}

logger.info("Resetting db set to [{}]", resetFlag);
cliOptions.put(SystemProperties.PROPERTY_DB_RESET, resetFlag.toString());

return true;
}

private static Boolean interpret(String arg) {
return BooleanUtils.toBooleanObject(arg);
}

private static void printHelp() {
Expand Down

0 comments on commit 2bf1766

Please sign in to comment.