LCOV - code coverage report
Current view: top level - sshd/commands - ListGroupsCommand.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 1 21 4.8 %
Date: 2022-11-19 15:00:39 Functions: 1 3 33.3 %

          Line data    Source code
       1             : // Copyright (C) 2011 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.sshd.commands;
      16             : 
      17             : import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE;
      18             : 
      19             : import com.google.common.base.MoreObjects;
      20             : import com.google.common.base.Strings;
      21             : import com.google.gerrit.entities.AccountGroup;
      22             : import com.google.gerrit.entities.InternalGroup;
      23             : import com.google.gerrit.extensions.common.GroupInfo;
      24             : import com.google.gerrit.extensions.restapi.Url;
      25             : import com.google.gerrit.server.account.GroupCache;
      26             : import com.google.gerrit.server.ioutil.ColumnFormatter;
      27             : import com.google.gerrit.server.restapi.group.ListGroups;
      28             : import com.google.gerrit.sshd.CommandMetaData;
      29             : import com.google.gerrit.sshd.SshCommand;
      30             : import com.google.gerrit.util.cli.Options;
      31             : import com.google.inject.Inject;
      32             : import java.util.Optional;
      33             : import org.kohsuke.args4j.Option;
      34             : 
      35             : @CommandMetaData(
      36             :     name = "ls-groups",
      37             :     description = "List groups visible to the caller",
      38             :     runsAt = MASTER_OR_SLAVE)
      39           1 : public class ListGroupsCommand extends SshCommand {
      40             :   @Inject private GroupCache groupCache;
      41             : 
      42             :   @Inject @Options public ListGroups listGroups;
      43             : 
      44             :   @Option(
      45             :       name = "--verbose",
      46             :       aliases = {"-v"},
      47             :       usage =
      48             :           "verbose output format with tab-separated columns for the "
      49             :               + "group name, UUID, description, owner group name, "
      50             :               + "owner group UUID, and whether the group is visible to all")
      51             :   private boolean verboseOutput;
      52             : 
      53             :   @Override
      54             :   public void run() throws Exception {
      55           0 :     enableGracefulStop();
      56           0 :     if (listGroups.getUser() != null && !listGroups.getProjects().isEmpty()) {
      57           0 :       throw die("--user and --project options are not compatible.");
      58             :     }
      59             : 
      60           0 :     ColumnFormatter formatter = new ColumnFormatter(stdout, '\t');
      61           0 :     for (GroupInfo info : listGroups.get()) {
      62           0 :       formatter.addColumn(MoreObjects.firstNonNull(info.name, "n/a"));
      63           0 :       if (verboseOutput) {
      64             :         Optional<InternalGroup> group =
      65           0 :             info.ownerId != null
      66           0 :                 ? groupCache.get(AccountGroup.uuid(Url.decode(info.ownerId)))
      67           0 :                 : Optional.empty();
      68             : 
      69           0 :         formatter.addColumn(Url.decode(info.id));
      70           0 :         formatter.addColumn(Strings.nullToEmpty(info.description));
      71           0 :         formatter.addColumn(group.map(InternalGroup::getName).orElse("n/a"));
      72           0 :         formatter.addColumn(group.map(g -> g.getGroupUUID().get()).orElse(""));
      73           0 :         formatter.addColumn(
      74           0 :             Boolean.toString(MoreObjects.firstNonNull(info.options.visibleToAll, Boolean.FALSE)));
      75             :       }
      76           0 :       formatter.nextLine();
      77           0 :     }
      78           0 :     formatter.finish();
      79           0 :   }
      80             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750