Add SSH command to resume project copy

Change-Id: I91d6d3099dc96aa483bed8ae37146db24d6646eb
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/ResumeCopyCommand.java b/src/main/java/com/googlesource/gerrit/plugins/importer/ResumeCopyCommand.java
new file mode 100644
index 0000000..be81762
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/ResumeCopyCommand.java
@@ -0,0 +1,62 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.importer;
+
+import com.google.gerrit.extensions.annotations.RequiresCapability;
+import com.google.gerrit.extensions.restapi.IdString;
+import com.google.gerrit.extensions.restapi.RestApiException;
+import com.google.gerrit.extensions.restapi.TopLevelResource;
+import com.google.gerrit.server.project.ProjectResource;
+import com.google.gerrit.server.project.ProjectsCollection;
+import com.google.gerrit.sshd.CommandMetaData;
+import com.google.gerrit.sshd.SshCommand;
+import com.google.inject.Inject;
+
+import org.kohsuke.args4j.Argument;
+import org.kohsuke.args4j.Option;
+
+@RequiresCapability(CopyProjectCapability.ID)
+@CommandMetaData(name = "resume-copy", description = "Resumes project copy")
+public class ResumeCopyCommand extends SshCommand {
+
+  @Option(name = "--quiet", usage = "suppress progress messages")
+  private boolean quiet;
+
+  @Argument(index = 0, required = true, metaVar = "NAME",
+      usage = "name of the target project")
+  private String project;
+
+  @Inject
+  private ResumeCopyProject resume;
+
+  @Inject
+  private ProjectsCollection projects;
+
+  @Override
+  protected void run() throws UnloggedFailure, Failure, Exception {
+    try {
+      ProjectResource rsrc =
+          projects.parse(TopLevelResource.INSTANCE,
+              IdString.fromDecoded(project));
+      if (!quiet) {
+        resume.setErr(stderr);
+      }
+      CopyProject.Input input = new CopyProject.Input();
+      resume.apply(rsrc, input);
+    } catch (RestApiException e) {
+      throw die(e.getMessage());
+    }
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/ResumeCopyProject.java b/src/main/java/com/googlesource/gerrit/plugins/importer/ResumeCopyProject.java
index 64b1a42..95d0525 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/ResumeCopyProject.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/ResumeCopyProject.java
@@ -33,15 +33,14 @@
 import com.google.gwtorm.server.OrmException;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
-import com.google.inject.Singleton;
 
 import com.googlesource.gerrit.plugins.importer.CopyProject.Input;
 
 import org.eclipse.jgit.api.errors.GitAPIException;
 
 import java.io.IOException;
+import java.io.Writer;
 
-@Singleton
 @RequiresCapability(CopyProjectCapability.ID)
 class ResumeCopyProject implements RestModifyView<ProjectResource, Input>,
     UiAction<ProjectResource> {
@@ -51,6 +50,8 @@
   private final String pluginName;
   private final ProjectCache projectCache;
 
+  private Writer err;
+
   @Inject
   ResumeCopyProject(
       Provider<ResumeProjectImport> resumeProjectImport,
@@ -65,6 +66,11 @@
     this.projectCache = projectCache;
   }
 
+  ResumeCopyProject setErr(Writer err) {
+    this.err = err;
+    return this;
+  }
+
   @Override
   public ResumeImportStatistic apply(ProjectResource rsrc, Input input)
       throws RestApiException, IOException, OrmException, ValidationException,
@@ -74,6 +80,7 @@
             IdString.fromDecoded(rsrc.getName()));
     return resumeProjectImport.get()
         .setCopy(true)
+        .setErr(err)
         .apply(projectResource, new ResumeProjectImport.Input());
   }
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/SshModule.java b/src/main/java/com/googlesource/gerrit/plugins/importer/SshModule.java
index 29a452d..53f5f6c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/SshModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/SshModule.java
@@ -23,6 +23,7 @@
     command(ListProjectImportsCommand.class);
     command(ResumeProjectCommand.class);
     command(CopyProjectCommand.class);
+    command(ResumeCopyCommand.class);
     command(CompleteProjectImportCommand.class);
 
     command(GroupCommand.class);
diff --git a/src/main/resources/Documentation/cmd-resume-copy.md b/src/main/resources/Documentation/cmd-resume-copy.md
new file mode 100644
index 0000000..6a1e4e2
--- /dev/null
+++ b/src/main/resources/Documentation/cmd-resume-copy.md
@@ -0,0 +1,42 @@
+@PLUGIN@ resume-copy
+====================
+
+NAME
+----
+@PLUGIN@ resume-copy - Resumes project copy
+
+SYNOPSIS
+--------
+```
+ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ resume-copy \
+  [--quiet] \
+  <NAME>
+```
+
+DESCRIPTION
+-----------
+Resumes project copy.
+
+ACCESS
+------
+Caller must be a member of a group that is granted the 'Copy Project'
+capability (provided by this plugin) or the 'Administrate Server'
+capability.
+
+SCRIPTING
+---------
+This command is intended to be used in scripts.
+
+OPTIONS
+-------
+
+`--quiet`
+:	Suppress progress messages.
+
+EXAMPLES
+--------
+Resume copy of the myProject project:
+
+```
+  $ ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ resume-copy myProject
+```