Skip to content

Commit

Permalink
CLI interface enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 5, 2024
1 parent bc2c3c8 commit 247dcf9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
9 changes: 5 additions & 4 deletions convex-cli/src/main/java/convex/cli/ACommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ public boolean isInteractive() {

/**
* Prompt the user for String input
* @param string
* @param message
* @return
*/
public String prompt(String string) {
if (!isInteractive()) throw new CLIError("Can't prompt for user input in non-interactive mode: "+string);
public String prompt(String message) {
if (!isInteractive()) throw new CLIError("Can't prompt for user input in non-interactive mode: "+message);

inform(0,isColoured()?string:Coloured.blue(string));
if (isColoured()) message=Coloured.blue(message);
inform(0,message);
try (Scanner scanner = new Scanner(System.in)) {
String s=scanner.nextLine();
return s;
Expand Down
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/mixins/KeyMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public char[] getKeyPassword() {
}

if (keypass.length == 0) {
paranoia("Cannot use an empty private key password in --strict-security mode");
paranoia("Cannot use an empty password in --strict-security mode");
}
return keypass;
}
Expand Down
8 changes: 6 additions & 2 deletions convex-cli/src/main/java/convex/cli/mixins/PeerKeyMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class PeerKeyMixin extends AMixin {
description = "Peer Key pair password in key store. Can also specify with CONVEX_PEER_KEY_PASSWORD environment variable.")
protected String keyPassword;

/**
* Gets the specified public key alias for the Peer
* @return Public key alias specified on CLI, or null if unspecified
*/
public String getPublicKey() {
return publicKey;
}
Expand All @@ -39,7 +43,7 @@ public char[] getKeyPassword() {
keypass = this.keyPassword.toCharArray();
} else {
if (isInteractive()) {
keypass = readPassword("Private Key Encryption Password: ");
keypass = readPassword("Peer Key Encryption Password: ");
}

if (keypass == null) {
Expand All @@ -50,7 +54,7 @@ public char[] getKeyPassword() {
this.keyPassword=new String(keypass);
}
if (keypass.length == 0) {
paranoia("Cannot use an empty private key password");
paranoia("Cannot use an empty password in --strict-security mode");
}
return keypass;
}
Expand Down
25 changes: 19 additions & 6 deletions convex-cli/src/main/java/convex/cli/peer/APeerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,25 @@ protected AKeyPair ensurePeerKey() {
if (peerPublicKey==null) {
if (!isInteractive()) {
throw new CLIError(ExitCodes.USAGE,"--peer-key must be specified in non-interactive mode");
} else {
boolean shouldGenerate=question("No --peer-key specified. Generate one? (y/n)");
if (shouldGenerate) {
AKeyPair kp=AKeyPair.generate();
inform(2,"Generated peer key: "+kp.getAccountKey());
char[] keyPass=peerKeyMixin.getKeyPassword();
storeMixin.addKeyPairToStore(kp, keyPass);
storeMixin.saveKeyStore();
return kp;
} else {
throw new CLIError("Opertation cancelled");
}
}
} else {
char[] keyPass=peerKeyMixin.getKeyPassword();
AKeyPair result=storeMixin.loadKeyFromStore(peerPublicKey, keyPass);
if (result==null) throw new CLIError("Peer key not found in store");
return result;
}

char[] keyPass=peerKeyMixin.getKeyPassword();

AKeyPair result=storeMixin.loadKeyFromStore(peerPublicKey, keyPass);
return result;
}

/**
Expand All @@ -65,12 +77,13 @@ protected AKeyPair ensureControllerKey() {
if (peerPublicKey==null) {
if (!isInteractive()) {
throw new CLIError(ExitCodes.USAGE,"Controller --key must be specified in non-interactive mode");
}
}
}

char[] keyPass=keyMixin.getKeyPassword();

AKeyPair result=storeMixin.loadKeyFromStore(peerPublicKey, keyPass);
if (result==null) throw new CLIError("Peer controller key not found in store");
return result;
}
}

0 comments on commit 247dcf9

Please sign in to comment.