Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
(d) This part prevents any management requests to be run on the daemo…
Browse files Browse the repository at this point in the history
…n: boolean sendToRest = daemonRunning & localDirHandledInDaemonScope && needsToRunInInitializedScope;. The if/then/else block inside sendToRest is obsolete by that.

(e) Do we want to run ManagementRequests in the daemon scope? Installing plugins? I don't think so...

(f) I am not a big fan of hardcoded strings in the source code. Can you move the thisPackage descriptions to a constant or better yet, derive it from a class name? Same for + "FolderRequest".

ad (c): Maybe commands should have a "canExecuteInDaemonScope()" method to allow/prevent "remote" execution. Just thinking out loud ...
  • Loading branch information
vwiencek committed Sep 8, 2014
1 parent 512a45a commit 45ab17e
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,9 @@ public void printResults(OperationResult operationResult) {
}
}


@Override
public boolean canExecuteInDaemonScope() {
// TODO [low] : right now only read commands in daemon scope
return false;
}
}
2 changes: 2 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public abstract class Command {
public abstract OperationOptions parseOptions(String[] operationArgs) throws Exception;
public abstract void printResults(OperationResult result);

public abstract boolean canExecuteInDaemonScope();

public void setLocalDir(File localDir) {
this.localDir = localDir;
}
Expand Down
30 changes: 3 additions & 27 deletions syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private int runCommand(Command command, String commandName, String[] commandArgs
boolean localDirHandledInDaemonScope = portFile != null && portFile.exists();
boolean daemonRunning = PidFileUtil.isProcessRunning(PIDFile);
boolean needsToRunInInitializedScope = command.getRequiredCommandScope() == CommandScope.INITIALIZED_LOCALDIR;
boolean sendToRest = daemonRunning & localDirHandledInDaemonScope && needsToRunInInitializedScope;
boolean sendToRest = daemonRunning & localDirHandledInDaemonScope && needsToRunInInitializedScope && command.canExecuteInDaemonScope();

command.setOut(out);

Expand Down Expand Up @@ -385,12 +385,7 @@ private int sendToRest(Command command, String commandName, String[] commandArgs

// Create and send HTTP/REST request

if (command.getRequiredCommandScope() == CommandScope.INITIALIZED_LOCALDIR) {
request = buildFolderRequestFromCommand(command, commandName, config.getLocalDir().getAbsolutePath());
}
else {
request = buildManagementRequestFromCommand(command, commandName);
}
request = buildFolderRequestFromCommand(command, commandName, config.getLocalDir().getAbsolutePath());

post.setEntity(new StringEntity(MessageFactory.toRequest(request)));

Expand Down Expand Up @@ -422,27 +417,8 @@ else if (response instanceof BadRequestResponse) {
}
}

private Request buildManagementRequestFromCommand(Command command, String commandName) throws Exception {
String thisPackage = "org.syncany.operations.daemon.messages";
String camelCaseMessageType = StringUtil.toCamelCase(commandName) + "ManagementRequest";
String fqMessageClassName = thisPackage + "." + camelCaseMessageType;

// Try to load!
try {
Class<? extends FolderRequest> message = Class.forName(fqMessageClassName).asSubclass(FolderRequest.class);
FolderRequest request = message.newInstance();
request.setId(new Random().nextInt());
request.setOptions(command.parseOptions(args));
return request;
}
catch (Exception e) {
logger.log(Level.INFO, "Could not find FQCN " + fqMessageClassName, e);
throw new Exception("Cannot read request class from request type: " + commandName, e);
}
}

private Request buildFolderRequestFromCommand(Command command, String commandName, String root) throws Exception {
String thisPackage = "org.syncany.operations.daemon.messages";
String thisPackage = Request.class.getName().replaceAll(".api", "");
String camelCaseMessageType = StringUtil.toCamelCase(commandName) + "FolderRequest";
String fqMessageClassName = thisPackage + "." + camelCaseMessageType;

Expand Down
5 changes: 5 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/ConnectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,9 @@ public OperationOptions parseOptions(String[] operationArgs) throws Exception {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean canExecuteInDaemonScope() {
return false;
}
}
5 changes: 5 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/DaemonCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public void printResults(OperationResult result) {
// TODO Auto-generated method stub

}

@Override
public boolean canExecuteInDaemonScope() {
return false;
}
}
6 changes: 5 additions & 1 deletion syncany-cli/src/main/java/org/syncany/cli/DebugCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public OperationOptions parseOptions(String[] operationArgs) throws Exception {
@Override
public void printResults(OperationResult result) {
// TODO Auto-generated method stub

}

@Override
public boolean canExecuteInDaemonScope() {
return false;
}
}
6 changes: 6 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/DownCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,10 @@ public void printResults(OperationResult operationResult) {
}

}

@Override
public boolean canExecuteInDaemonScope() {
// TODO [low] : right now only read commands in daemon scope
return false;
}
}
6 changes: 6 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/GenlinkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ public void printResults(OperationResult operationResult) {

printLink(concreteOperationREsut, commandOptions.isShortOutput());
}

@Override
public boolean canExecuteInDaemonScope() {
// TODO [low] : right now only read commands in daemon scope
return false;
}
}
5 changes: 5 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/InitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,9 @@ public String getString(CipherSpec cipherSpec) {

return cipherTransformerTO;
}

@Override
public boolean canExecuteInDaemonScope() {
return false;
}
}
6 changes: 6 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/LsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,10 @@ private int calculateLongestValue(Map<String, FileVersion> fileVersions, Functio

return result;
}

@Override
public boolean canExecuteInDaemonScope() {
// TODO [low] : right now only read commands in daemon scope
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public OperationOptions parseOptions(String[] operationArgs) throws Exception {
@Override
public void printResults(OperationResult result) {
// TODO Auto-generated method stub

}

@Override
public boolean canExecuteInDaemonScope() {
// TODO [low] : right now only read commands in daemon scope
return false;
}
}
6 changes: 6 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/PluginCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,10 @@ public void printResults(OperationResult result) {
// TODO Auto-generated method stub

}

@Override
public boolean canExecuteInDaemonScope() {
// TODO [low] : right now only read commands in daemon scope
return false;
}
}
6 changes: 6 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/RestoreCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,10 @@ public void printResults(OperationResult operationResult) {
throw new RuntimeException("Invalid result code: " + concreteOperationResult.getResultCode());
}
}

@Override
public boolean canExecuteInDaemonScope() {
// TODO [low] : right now only read commands in daemon scope
return false;
}
}
5 changes: 5 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/StatusCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ public void printResults(OperationResult operationResult) {
out.println("No local changes.");
}
}

@Override
public boolean canExecuteInDaemonScope() {
return true;
}
}
6 changes: 6 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/UpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,10 @@ else if (concreteOperationResult.getResultCode() == UpResultCode.OK_CHANGES_UPLO
out.println("Sync up skipped, no local changes.");
}
}

@Override
public boolean canExecuteInDaemonScope() {
// TODO [low] : right now only read commands in daemon scope
return false;
}
}
7 changes: 6 additions & 1 deletion syncany-cli/src/main/java/org/syncany/cli/WatchCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public OperationOptions parseOptions(String[] operationArgs) throws Exception {
@Override
public void printResults(OperationResult result) {
// TODO Auto-generated method stub

}

@Override
public boolean canExecuteInDaemonScope() {
// TODO [low] : right now only read commands in daemon scope
return false;
}
}

0 comments on commit 45ab17e

Please sign in to comment.