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

          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.server.plugins.PluginInstallException;
      21             : import com.google.gerrit.sshd.CommandMetaData;
      22             : import java.io.File;
      23             : import java.io.IOException;
      24             : import java.io.InputStream;
      25             : import java.net.MalformedURLException;
      26             : import java.net.URL;
      27             : import java.nio.file.Files;
      28             : import org.kohsuke.args4j.Argument;
      29             : import org.kohsuke.args4j.Option;
      30             : 
      31             : @CommandMetaData(name = "install", description = "Install/Add a plugin", runsAt = MASTER_OR_SLAVE)
      32           1 : final class PluginInstallCommand extends PluginAdminSshCommand {
      33             :   @Option(
      34             :       name = "--name",
      35             :       aliases = {"-n"},
      36             :       usage = "install under name")
      37             :   private String name;
      38             : 
      39             :   @Option(name = "-")
      40             :   void useInput(@SuppressWarnings("unused") boolean on) {
      41           0 :     source = "-";
      42           0 :   }
      43             : 
      44             :   @Argument(index = 0, metaVar = "-|URL", usage = "JAR to load")
      45             :   private String source;
      46             : 
      47             :   @SuppressWarnings("resource")
      48             :   @Override
      49             :   protected void doRun() throws UnloggedFailure {
      50           0 :     if (Strings.isNullOrEmpty(source)) {
      51           0 :       throw die("Argument \"-|URL\" is required");
      52             :     }
      53           0 :     if (Strings.isNullOrEmpty(name) && "-".equalsIgnoreCase(source)) {
      54           0 :       throw die("--name required when source is stdin");
      55             :     }
      56             : 
      57           0 :     if (Strings.isNullOrEmpty(name)) {
      58           0 :       int s = source.lastIndexOf('/');
      59           0 :       if (0 <= s) {
      60           0 :         name = source.substring(s + 1);
      61             :       } else {
      62           0 :         name = source;
      63             :       }
      64             :     }
      65             : 
      66             :     InputStream data;
      67           0 :     if ("-".equalsIgnoreCase(source)) {
      68           0 :       data = in;
      69           0 :     } else if (new File(source).isFile() && source.equals(new File(source).getAbsolutePath())) {
      70             :       try {
      71           0 :         data = Files.newInputStream(new File(source).toPath());
      72           0 :       } catch (IOException e) {
      73           0 :         throw die("cannot read " + source, e);
      74           0 :       }
      75             :     } else {
      76             :       try {
      77           0 :         data = new URL(source).openStream();
      78           0 :       } catch (MalformedURLException e) {
      79           0 :         throw die("invalid url " + source, e);
      80           0 :       } catch (IOException e) {
      81           0 :         throw die("cannot read " + source, e);
      82           0 :       }
      83             :     }
      84             :     try {
      85           0 :       loader.installPluginFromStream(name, data);
      86           0 :     } catch (IOException e) {
      87           0 :       throw die("cannot install plugin", e);
      88           0 :     } catch (PluginInstallException e) {
      89           0 :       e.printStackTrace(stderr);
      90           0 :       String msg = String.format("Plugin failed to install. Cause: %s", e.getMessage());
      91           0 :       throw die(msg);
      92             :     } finally {
      93             :       try {
      94           0 :         data.close();
      95           0 :       } catch (IOException err) {
      96             :         // Ignored
      97           0 :       }
      98             :     }
      99           0 :   }
     100             : }

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