Skip to content

Commit

Permalink
Refactoring Peer CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Jan 15, 2024
1 parent 1e0ab83 commit 2f7741d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 30 deletions.
9 changes: 8 additions & 1 deletion convex-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

The `convex-cli` module provides a CLI interface to Convex, including the ability to operate as either a client or peer on the Convex Network

## Usage Examples

### List available commands and options

```
convex --help
```

## License

Copyright 2021-2023 The Convex Foundation and Contributors
Copyright 2021-2024 The Convex Foundation and Contributors

Code in `convex-cli` is provided under the Convex Public License
19 changes: 1 addition & 18 deletions convex-cli/src/main/java/convex/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,10 @@
description="Convex Command Line Interface")

public class Main implements Runnable {

private static Logger log = LoggerFactory.getLogger(Main.class);

public CommandLine commandLine=new CommandLine(this);

@Option(names={ "-c", "--config"},
scope = ScopeType.INHERIT,
description="Use the specified config file. If not specified, will check ~/.convex/convex.config")
private String configFilename;

@Option(names={"-e", "--etch"},
scope = ScopeType.INHERIT,
defaultValue="${env:CONVEX_ETCH_FILE}",
description="Convex Etch database filename. A temporary storage file will be created if required.")
private String etchStoreFilename;

@Option(names={"-k", "--keystore"},
defaultValue="${env:CONVEX_KEYSTORE_PASSWORD:-" +Constants.KEYSTORE_FILENAME+"}",
scope = ScopeType.INHERIT,
Expand Down Expand Up @@ -262,12 +250,7 @@ public String getKeyStoreFilename() {
return null;
}

public String getEtchStoreFilename() {
if ( etchStoreFilename != null) {
return Helpers.expandTilde(etchStoreFilename).strip();
}
return null;
}


private boolean keyStoreLoaded=false;
private KeyStore keyStore=null;
Expand Down
17 changes: 17 additions & 0 deletions convex-cli/src/main/java/convex/cli/peer/APeerCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package convex.cli.peer;

import convex.cli.Helpers;
import convex.cli.Main;
import picocli.CommandLine.ParentCommand;

public abstract class APeerCommand implements Runnable {

@ParentCommand
private Peer peerParent;

public String getEtchStoreFilename() {
if ( peerParent.etchStoreFilename != null) {
return Helpers.expandTilde(peerParent.etchStoreFilename).strip();
}
return null;
}

protected Main cli() {
return peerParent.cli();
}
}
16 changes: 15 additions & 1 deletion convex-cli/src/main/java/convex/cli/peer/Peer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package convex.cli.peer;

import convex.cli.ATopCommand;
import convex.cli.Main;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParentCommand;
import picocli.CommandLine.ScopeType;


/**
Expand All @@ -21,7 +24,18 @@
},
mixinStandardHelpOptions=true,
description="Operates a local peer.")
public class Peer implements Runnable {
public class Peer extends ATopCommand {

@Option(names={ "-c", "--config"},
scope = ScopeType.INHERIT,
description="Use the specified config file. If not specified, will check ~/.convex/convex.config")
private String configFilename;

@Option(names={"-e", "--etch"},
scope = ScopeType.INHERIT,
defaultValue="${env:CONVEX_ETCH_FILE}",
description="Convex Etch database filename. A temporary storage file will be created if required.")
String etchStoreFilename;

// private static final Logger log = Logger.getLogger(Peer.class.getName());

Expand Down
17 changes: 7 additions & 10 deletions convex-cli/src/main/java/convex/cli/peer/PeerCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
import java.io.File;
import java.security.KeyStore;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import convex.api.Convex;
import convex.cli.CLIError;
import convex.cli.Constants;
import convex.cli.Main;
import convex.cli.output.RecordOutput;
import convex.core.Result;
import convex.core.crypto.AKeyPair;
import convex.core.crypto.PFXTools;
import convex.core.data.Address;
import convex.core.data.ACell;
import convex.core.data.Address;
import convex.core.lang.Reader;
import convex.core.transactions.ATransaction;
import convex.core.transactions.Invoke;
import convex.core.Result;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParentCommand;
import picocli.CommandLine.Spec;

/**
Expand All @@ -43,9 +42,6 @@ public class PeerCreate extends APeerCommand {

private static final Logger log = LoggerFactory.getLogger(PeerCreate.class);

@ParentCommand
private Peer peerParent;

@Spec CommandSpec spec;

@Option(names={"--public-key"},
Expand All @@ -72,7 +68,7 @@ public class PeerCreate extends APeerCommand {
@Override
public void run() {

Main mainParent = peerParent.mainParent;
Main mainParent = cli();

long peerStake = convex.core.Constants.MINIMUM_EFFECTIVE_STAKE;

Expand Down Expand Up @@ -142,4 +138,5 @@ public void run() {
throw new CLIError("Error creating Peer",t);
}
}

}

0 comments on commit 2f7741d

Please sign in to comment.