Expand HelloSshCommand to print files from the data directory
Demonstrates injecting @PluginData.
Change-Id: I3a2c77bc2b0c4ae98c01bad69d0ccf6f48b6b250
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cookbook/pluginprovider/HelloSshCommand.java b/src/main/java/com/googlesource/gerrit/plugins/cookbook/pluginprovider/HelloSshCommand.java
index a2b678e..853d04d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/cookbook/pluginprovider/HelloSshCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/cookbook/pluginprovider/HelloSshCommand.java
@@ -14,39 +14,64 @@
package com.googlesource.gerrit.plugins.cookbook.pluginprovider;
+import com.google.gerrit.extensions.annotations.PluginData;
import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject;
+import org.kohsuke.args4j.Argument;
+
import java.io.IOException;
+import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
-/**
- * SSH command defined by dynamically registered plugins.
- *
- */
-@CommandMetaData(name = "print", description = "Print content of the plugin file")
+/** SSH command defined by dynamically registered plugins. */
+@CommandMetaData(name = "cat", description = "Print content of plugin file")
public final class HelloSshCommand extends SshCommand {
private final String pluginName;
private final Path pluginDir;
+ private final Path dataDir;
+
+ @Argument(usage = "files in data directory to print")
+ private List<String> files = new ArrayList<>();
@Inject
- public HelloSshCommand(@PluginName String pluginName, SitePaths sitePaths) {
+ public HelloSshCommand(@PluginName String pluginName,
+ SitePaths sitePaths,
+ @PluginData Path dataDir) {
this.pluginName = pluginName;
- this.pluginDir = sitePaths.plugins_dir;
+ this.pluginDir = sitePaths.plugins_dir.normalize();
+ this.dataDir = dataDir.normalize();
}
@Override
public void run() {
Path pluginPath = pluginDir.resolve(pluginName + ".ssh");
+ printOne(pluginPath);
+ for (String name : files) {
+ Path p = dataDir.resolve(name).normalize();
+ if (!p.startsWith(dataDir)) {
+ throw new RuntimeException(p + " is outside data directory " + dataDir);
+ }
+ printOne(p);
+ }
+ }
+
+ private void printOne(Path p) {
try {
- Files.copy(pluginPath, out);
+ Files.copy(p, out);
} catch (IOException e) {
- throw new RuntimeException("Cannot read plugin content of " + pluginPath,
- e);
+ try (PrintWriter w = new PrintWriter(err)) {
+ w.write("Error reading contents of ");
+ w.write(p.toAbsolutePath().toString());
+ w.write(": \n");
+ e.printStackTrace(w);
+ }
}
}
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cookbook/pluginprovider/HelloSshPluginProvider.java b/src/main/java/com/googlesource/gerrit/plugins/cookbook/pluginprovider/HelloSshPluginProvider.java
index de213e7..8ffd8c2 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/cookbook/pluginprovider/HelloSshPluginProvider.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/cookbook/pluginprovider/HelloSshPluginProvider.java
@@ -25,16 +25,22 @@
import java.nio.file.Path;
/**
- * Dynamic provider of Gerrit plugins derived by *.ssh files under $GERRIT_SITE/plugins.
- *
+ * Dynamic provider of Gerrit plugins derived by *.ssh files under
+ * $GERRIT_SITE/plugins.
+ * <p>
* Example of how to define a dynamic Gerrit plugin provider to register
* a new plugin based on the content of *.ssh files.
- *
+ * <p>
* This provider allows to define a Gerrit plugin by simply dropping a .ssh file
* (e.g. hello.ssh) under $GERRIT_SITE/plugins.
+ * <p>
* Once the file is created a new plugin is automatically loaded with the name
- * without extension of the .ssh file (e.g. hello) and a new 'cat' SSH command is
- * automatically available from the registered plugin.
+ * without extension of the .ssh file (e.g. hello) and a new 'cat' SSH command
+ * is automatically available from the registered plugin.
+ * <p>
+ * The 'cat' command will print the contents of the .ssh file, along with the
+ * contents of any arguments, resolved against the plugin's data directory
+ * $GERRIT_SITE/data/name.
*/
public class HelloSshPluginProvider implements ServerPluginProvider {
private static final String SSH_EXT = ".ssh";