Add ssh command to list groups.

Change-Id: Iaae73ec644e4f1ca631e8be1d90f7334ae5bd8c7
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/Documentation/cmd-index.txt b/Documentation/cmd-index.txt
index a6296b4..c3b165d 100644
--- a/Documentation/cmd-index.txt
+++ b/Documentation/cmd-index.txt
@@ -54,6 +54,9 @@
 'gerrit approve'::
 	'Deprecated alias for `gerrit review`.'
 
+link:cmd-ls-groups.html[gerrit ls-groups]::
+	List groups visible to the caller.
+
 link:cmd-ls-projects.html[gerrit ls-projects]::
 	List projects visible to the caller.
 
diff --git a/Documentation/cmd-ls-groups.txt b/Documentation/cmd-ls-groups.txt
new file mode 100644
index 0000000..842abfd
--- /dev/null
+++ b/Documentation/cmd-ls-groups.txt
@@ -0,0 +1,44 @@
+gerrit ls-groups
+================
+
+NAME
+----
+gerrit ls-groups - List groups visible to caller
+
+SYNOPSIS
+--------
+[verse]
+'ssh' -p <port> <host> 'gerrit ls-groups'
+
+DESCRIPTION
+-----------
+Displays the list of group names, one per line, that are visible to
+the account of the calling user.
+
+If the caller is a member of the privileged 'Administrators' group,
+all groups are listed.
+
+ACCESS
+------
+Any user who has configured an SSH key.
+
+SCRIPTING
+---------
+This command is intended to be used in scripts.
+
+EXAMPLES
+--------
+
+List visible groups:
+=====
+	$ ssh -p 29418 review.example.com gerrit ls-groups
+	Administrators
+	Anonymous Users
+	MyProject_Committers
+	Project Owners
+	Registered Users
+=====
+
+GERRIT
+------
+Part of link:index.html[Gerrit Code Review]
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountModule.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountModule.java
index 8b34689..d2e6118 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountModule.java
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountModule.java
@@ -35,7 +35,7 @@
         factory(GroupDetailHandler.Factory.class);
         factory(MyGroupsFactory.Factory.class);
         factory(RenameGroup.Factory.class);
-        factory(VisibleGroups.Factory.class);
+        factory(VisibleGroupsHandler.Factory.class);
       }
     });
     rpc(AccountSecurityImpl.class);
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/GroupAdminServiceImpl.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/GroupAdminServiceImpl.java
index 6fac53a..448baf9 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/GroupAdminServiceImpl.java
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/GroupAdminServiceImpl.java
@@ -63,7 +63,7 @@
   private final CreateGroup.Factory createGroupFactory;
   private final RenameGroup.Factory renameGroupFactory;
   private final GroupDetailHandler.Factory groupDetailFactory;
-  private final VisibleGroups.Factory visibleGroupsFactory;
+  private final VisibleGroupsHandler.Factory visibleGroupsFactory;
 
   @Inject
   GroupAdminServiceImpl(final Provider<ReviewDb> schema,
@@ -76,7 +76,7 @@
       final CreateGroup.Factory createGroupFactory,
       final RenameGroup.Factory renameGroupFactory,
       final GroupDetailHandler.Factory groupDetailFactory,
-      final VisibleGroups.Factory visibleGroupsFactory) {
+      final VisibleGroupsHandler.Factory visibleGroupsFactory) {
     super(schema, currentUser);
     this.accountCache = accountCache;
     this.groupIncludeCache = groupIncludeCache;
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroupsHandler.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroupsHandler.java
new file mode 100644
index 0000000..54f91f7
--- /dev/null
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroupsHandler.java
@@ -0,0 +1,39 @@
+// Copyright (C) 2011 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.google.gerrit.httpd.rpc.account;
+
+import com.google.gerrit.common.data.GroupList;
+import com.google.gerrit.httpd.rpc.Handler;
+import com.google.gerrit.server.account.VisibleGroups;
+import com.google.inject.Inject;
+
+public class VisibleGroupsHandler extends Handler<GroupList> {
+
+  interface Factory {
+    VisibleGroupsHandler create();
+  }
+
+  private final VisibleGroups.Factory visibleGroupsFactory;
+
+  @Inject
+  VisibleGroupsHandler(final VisibleGroups.Factory visibleGroupsFactory) {
+    this.visibleGroupsFactory = visibleGroupsFactory;
+  }
+
+  @Override
+  public GroupList call() throws Exception {
+    return visibleGroupsFactory.create().get();
+  }
+}
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroups.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/VisibleGroups.java
similarity index 81%
rename from gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroups.java
rename to gerrit-server/src/main/java/com/google/gerrit/server/account/VisibleGroups.java
index 163d7e3..c47c231 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroups.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/VisibleGroups.java
@@ -10,17 +10,16 @@
 // 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.
+// limitations under the License
 
-package com.google.gerrit.httpd.rpc.account;
+package com.google.gerrit.server.account;
 
 import com.google.gerrit.common.data.GroupDetail;
 import com.google.gerrit.common.data.GroupList;
-import com.google.gerrit.httpd.rpc.Handler;
+import com.google.gerrit.common.errors.NoSuchGroupException;
 import com.google.gerrit.reviewdb.AccountGroup;
 import com.google.gerrit.server.IdentifiedUser;
-import com.google.gerrit.server.account.GroupCache;
-import com.google.gerrit.server.account.GroupControl;
+import com.google.gwtorm.client.OrmException;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 
@@ -29,30 +28,29 @@
 import java.util.ArrayList;
 import java.util.List;
 
-public class VisibleGroups extends Handler<GroupList> {
+public class VisibleGroups {
 
-  interface Factory {
+  public interface Factory {
     VisibleGroups create();
   }
 
   private final Provider<IdentifiedUser> identifiedUser;
   private final GroupCache groupCache;
   private final GroupControl.Factory groupControlFactory;
-  private final GroupDetailHandler.Factory groupDetailFactory;
+  private final GroupDetailFactory.Factory groupDetailFactory;
 
   @Inject
   VisibleGroups(final Provider<IdentifiedUser> currentUser,
       final GroupCache groupCache,
       final GroupControl.Factory groupControlFactory,
-      final GroupDetailHandler.Factory groupDetailFactory) {
+      final GroupDetailFactory.Factory groupDetailFactory) {
     this.identifiedUser = currentUser;
     this.groupCache = groupCache;
     this.groupControlFactory = groupControlFactory;
     this.groupDetailFactory = groupDetailFactory;
   }
 
-  @Override
-  public GroupList call() throws Exception {
+  public GroupList get() throws OrmException, NoSuchGroupException {
     final IdentifiedUser user = identifiedUser.get();
     final List<AccountGroup> list = new ArrayList<AccountGroup>();
     if (user.getCapabilities().canAdministrateServer()) {
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritRequestModule.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritRequestModule.java
index 42c2531..afd524c 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritRequestModule.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritRequestModule.java
@@ -26,6 +26,7 @@
 import com.google.gerrit.server.account.GroupMembersFactory;
 import com.google.gerrit.server.account.PerformCreateGroup;
 import com.google.gerrit.server.account.PerformRenameGroup;
+import com.google.gerrit.server.account.VisibleGroups;
 import com.google.gerrit.server.git.CreateCodeReviewNotes;
 import com.google.gerrit.server.git.MergeOp;
 import com.google.gerrit.server.git.MetaDataUpdate;
@@ -91,6 +92,7 @@
     factory(RegisterNewEmailSender.Factory.class);
     factory(PerformCreateGroup.Factory.class);
     factory(PerformRenameGroup.Factory.class);
+    factory(VisibleGroups.Factory.class);
     factory(GroupDetailFactory.Factory.class);
     factory(GroupMembersFactory.Factory.class);
   }
diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/DefaultCommandModule.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/DefaultCommandModule.java
index a369b86..5cee06e 100644
--- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/DefaultCommandModule.java
+++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/DefaultCommandModule.java
@@ -37,6 +37,7 @@
     command(gerrit).toProvider(new DispatchCommandProvider(gerrit));
     command(gerrit, "flush-caches").to(FlushCaches.class);
     command(gerrit, "ls-projects").to(ListProjects.class);
+    command(gerrit, "ls-groups").to(ListGroupsCommand.class);
     command(gerrit, "query").to(Query.class);
     command(gerrit, "show-caches").to(ShowCaches.class);
     command(gerrit, "show-connections").to(ShowConnections.class);
diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/ListGroupsCommand.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/ListGroupsCommand.java
new file mode 100644
index 0000000..b25bee4
--- /dev/null
+++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/ListGroupsCommand.java
@@ -0,0 +1,62 @@
+// Copyright (C) 2011 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.google.gerrit.sshd.commands;
+
+import com.google.gerrit.common.data.GroupDetail;
+import com.google.gerrit.common.data.GroupList;
+import com.google.gerrit.common.errors.NoSuchGroupException;
+import com.google.gerrit.server.account.VisibleGroups;
+import com.google.gerrit.sshd.BaseCommand;
+import com.google.gwtorm.client.OrmException;
+import com.google.inject.Inject;
+
+import org.apache.sshd.server.Environment;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+public class ListGroupsCommand extends BaseCommand {
+
+  @Inject
+  private VisibleGroups.Factory visibleGroupsFactory;
+
+  @Override
+  public void start(final Environment env) throws IOException {
+    startThread(new CommandRunnable() {
+      @Override
+      public void run() throws Exception {
+        parseCommandLine();
+        ListGroupsCommand.this.display();
+      }
+    });
+  }
+
+  private void display() throws Failure {
+    final PrintWriter stdout = toPrintWriter(out);
+    try {
+      final GroupList visibleGroups =
+          visibleGroupsFactory.create().get();
+      for (final GroupDetail groupDetail : visibleGroups.getGroups()) {
+        stdout.print(groupDetail.group.getName() + "\n");
+      }
+    } catch (OrmException e) {
+      throw die(e);
+    } catch (NoSuchGroupException e) {
+      throw die(e);
+    } finally {
+      stdout.flush();
+    }
+  }
+}