Skip to content

Commit

Permalink
Improve CLI for query commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Jul 30, 2024
1 parent f5f43f5 commit 91d0509
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
6 changes: 6 additions & 0 deletions convex-cli/src/main/java/convex/cli/ACommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package convex.cli;

import picocli.CommandLine;

/**
* Base class for Convex CLI commands
*/
Expand All @@ -10,4 +12,8 @@ public abstract class ACommand implements Runnable {
* @return CLI instance
*/
public abstract Main cli();

public void showUsage() {
CommandLine.usage(this, System.out);
}
}
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Constants {

public static final String CONFIG_FILENAME = "~/.convex/convex.properties";

public static final String ETCH_FILENAME = "~/.convex//etch.db";
public static final String ETCH_FILENAME = "~/.convex/etch.db";

public static final int KEY_GENERATE_COUNT = 1;

Expand Down
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Main() {
@Override
public void run() {
// no command provided - so show help
CommandLine.usage(new Main(), System.out);
showUsage();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package convex.cli.client;

import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -85,7 +86,7 @@ protected boolean ensureKeyPair(Convex convex) {

protected convex.api.Convex connect() throws IOException,TimeoutException {
if (port==null) port=convex.core.Constants.DEFAULT_PEER_PORT;
if (hostname==null) hostname="localhost";
if (hostname==null) hostname=convex.cli.Constants.HOSTNAME_PEER;
try {
InetSocketAddress sa=new InetSocketAddress(hostname,port);
Convex c;
Expand All @@ -99,6 +100,8 @@ protected convex.api.Convex connect() throws IOException,TimeoutException {
}

return c;
} catch (ConnectException ce) {
throw new CLIError("Cannot connect to: "+hostname+" on port "+port);
} catch (Exception e) {
throw e;
}
Expand Down
30 changes: 15 additions & 15 deletions convex-cli/src/main/java/convex/cli/client/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import java.io.IOException;
import java.util.concurrent.TimeoutException;

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

import convex.api.Convex;
import convex.cli.CLIError;
import convex.core.Result;
Expand All @@ -23,31 +20,34 @@
*/
@Command(name="query",
mixinStandardHelpOptions=true,
description="Execute a user query.")
description="Execute user queries. ")
public class Query extends AClientCommand {

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

@Parameters(paramLabel="queryCommand", description="Query Command")
private String queryCommand;

@Parameters(paramLabel="queryCommand", description="Query command(s). Multiple commands will be executed in sequence unless one fails")
private String[] commands;

@Override
public void run() {
// sub command run with no command provided
log.debug("query command: {}", queryCommand);
if ((commands==null)||(commands.length==0)) {
showUsage();
return;
}

try {
Convex convex = connect();
log.debug("Executing query: %s\n", queryCommand);
ACell message = Reader.read(queryCommand);
Result result = convex.querySync(message, timeout);
mainParent.printResult(result);
for (int i=0; i<commands.length; i++) {
ACell message = Reader.read(commands[i]);
Result result = convex.querySync(message, timeout);
cli().printResult(result);
if (result.isError()) {
break;
}
}
} catch (IOException e) {
throw new CLIError("IO Error executing query",e);
} catch (TimeoutException e) {
throw new CLIError("Query timed out");
}
}

}

0 comments on commit 91d0509

Please sign in to comment.