Exemplo n.º 1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $configHelper = $this->getHelper('configuration');
     /* @var $configHelper GlobalConfigurationHelper */
     try {
         $configHelper->getConfiguration()->getRepositoryConfiguration($input->getArgument('repository'));
         throw new RepositoryExistsException($input->getArgument('repository'));
     } catch (NoSuchRepositoryException $ex) {
         // no op
     }
     if (!Util::isSshRepositoryUrl($input->getArgument('repository'))) {
         throw new NoSshRepositoryException($input->getArgument('repository'));
     }
     $repositoryConfig = new RepositoryConfiguration();
     $repositoryConfig->setSshAlias($input->getOption('alias') ?: sha1($input->getArgument('repository') . time()));
     $sshKeyFile = new \SplFileInfo($input->getArgument('key'));
     if (!$sshKeyFile->isFile()) {
         throw new NotAFileException($sshKeyFile);
     }
     $repositoryConfig->setIdentityFile($sshKeyFile->getRealPath());
     $sshConfigFile = new \SplFileInfo($configHelper->getConfiguration()->getSshDirectory() . '/config');
     if (!file_exists($sshConfigFile->getPathname())) {
         FsUtil::touch($sshConfigFile->getPathname());
     }
     if (!$sshConfigFile->isFile()) {
         throw new NotAFileException($sshConfigFile);
     }
     if (!$sshConfigFile->isReadable()) {
         throw new UnreadableFileException($sshConfigFile);
     }
     if (!$sshConfigFile->isWritable()) {
         throw new UnwritableFileException($sshConfigFile);
     }
     $sshConfigLines = file($sshConfigFile);
     try {
         if (Util::addSshAliasLines($sshConfigLines, $input->getArgument('repository'), $repositoryConfig)) {
             FsUtil::file_put_contents($sshConfigFile, implode(PHP_EOL, $sshConfigLines));
             $output->writeln(sprintf('Added section <info>%s</info> to <info>%s</info>', 'Host ' . $repositoryConfig->getSshAlias(), $sshConfigFile->getPathname()), OutputInterface::VERBOSITY_VERBOSE);
         } else {
             $output->writeln(sprintf('Added section <info>%s</info> already exists in <info>%s</info>', 'Host ' . $repositoryConfig->getSshAlias(), $sshConfigFile->getPathname()), OutputInterface::VERBOSITY_VERBOSE);
         }
     } catch (SshAliasExistsException $ex) {
         throw new SshAliasExistsException($ex->getAliasName(), $ex->getLineNumber(), $sshConfigFile);
     }
     $configHelper->getConfiguration()->setRepositoryConfiguration($input->getArgument('repository'), $repositoryConfig);
     $configHelper->getConfiguration()->write();
     $output->writeln(sprintf('Registered private key <info>%s</info> for repository <info>%s</info>', $repositoryConfig->getIdentityFile(), $input->getArgument('repository')));
     return 0;
 }