LCOV - code coverage report
Current view: top level - sshd/commands - PluginLsCommand.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 2 24 8.3 %
Date: 2022-11-19 15:00:39 Functions: 1 4 25.0 %

          Line data    Source code
       1             : // Copyright (C) 2012 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.Strings;
      20             : import com.google.gerrit.common.data.GlobalCapability;
      21             : import com.google.gerrit.extensions.annotations.RequiresCapability;
      22             : import com.google.gerrit.extensions.common.PluginInfo;
      23             : import com.google.gerrit.extensions.restapi.TopLevelResource;
      24             : import com.google.gerrit.json.OutputFormat;
      25             : import com.google.gerrit.server.plugins.ListPlugins;
      26             : import com.google.gerrit.sshd.CommandMetaData;
      27             : import com.google.gerrit.sshd.SshCommand;
      28             : import com.google.gerrit.util.cli.Options;
      29             : import com.google.gson.reflect.TypeToken;
      30             : import com.google.inject.Inject;
      31             : import java.util.Map;
      32             : import org.kohsuke.args4j.Option;
      33             : 
      34             : @RequiresCapability(GlobalCapability.VIEW_PLUGINS)
      35             : @CommandMetaData(name = "ls", description = "List the installed plugins", runsAt = MASTER_OR_SLAVE)
      36           1 : public class PluginLsCommand extends SshCommand {
      37             :   @Inject @Options public ListPlugins list;
      38             : 
      39           1 :   @Option(name = "--format", usage = "output format")
      40             :   private OutputFormat format = OutputFormat.TEXT;
      41             : 
      42             :   @Override
      43             :   public void run() throws Exception {
      44           0 :     enableGracefulStop();
      45           0 :     Map<String, PluginInfo> output = list.apply(TopLevelResource.INSTANCE).value();
      46             : 
      47           0 :     if (format.isJson()) {
      48           0 :       format
      49           0 :           .newGson()
      50           0 :           .toJson(output, new TypeToken<Map<String, PluginInfo>>() {}.getType(), stdout);
      51           0 :       stdout.print('\n');
      52             :     } else {
      53           0 :       String template = "%-30s %-10s %-16s %-8s %s\n";
      54           0 :       stdout.format(template, "Name", "Version", "Api-Version", "Status", "File");
      55           0 :       stdout.print(
      56             :           "-------------------------------------------------------------------------------\n");
      57           0 :       for (Map.Entry<String, PluginInfo> p : output.entrySet()) {
      58           0 :         PluginInfo info = p.getValue();
      59           0 :         stdout.format(
      60             :             template,
      61           0 :             p.getKey(),
      62           0 :             Strings.nullToEmpty(info.version),
      63           0 :             Strings.nullToEmpty(info.apiVersion),
      64           0 :             status(info.disabled),
      65           0 :             Strings.nullToEmpty(info.filename));
      66           0 :       }
      67             :     }
      68           0 :     stdout.flush();
      69           0 :   }
      70             : 
      71             :   private String status(Boolean disabled) {
      72           0 :     return disabled != null && disabled.booleanValue() ? "DISABLED" : "ENABLED";
      73             :   }
      74             : }

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