Passed
Push — master ( 459c89...662542 )
by Anton
02:25
created

SshCommand::execute()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 7
nop 2
dl 0
loc 39
rs 8.9848
c 0
b 0
f 0
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Console;
9
10
use Deployer\Deployer;
11
use Deployer\Host\Localhost;
12
use Deployer\Task\Context;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ChoiceQuestion;
18
19
/**
20
 * @codeCoverageIgnore
21
 */
22
class SshCommand extends Command
23
{
24
    /**
25
     * @var Deployer
26
     */
27
    private $deployer;
28
29
    /**
30
     * SshCommand constructor.
31
     * @param Deployer $deployer
32
     */
33
    public function __construct(Deployer $deployer)
34
    {
35
        parent::__construct('ssh');
36
        $this->setDescription('Connect to host through ssh');
37
        $this->deployer = $deployer;
38
    }
39
40
    /**
41
     * Configures the command
42
     */
43
    protected function configure()
44
    {
45
        $this->addArgument(
46
            'hostname',
47
            InputArgument::OPTIONAL,
48
            'Hostname'
49
        );
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $hostname = $input->getArgument('hostname');
58
        if (!empty($hostname)) {
59
            $host = $this->deployer->hosts->get($hostname);
60
        } else {
61
            $hosts = $this->deployer->hosts->select(function ($host) {
62
                return !($host instanceof Localhost);
63
            });
64
65
            if (count($hosts) === 0) {
66
                $output->writeln('No remote hosts.');
67
                return; // Because there are no hosts.
68
            } elseif (count($hosts) === 1) {
69
                $host = array_shift($hosts);
70
            } else {
71
                $helper = $this->getHelper('question');
72
                $question = new ChoiceQuestion(
73
                    'Select host:',
74
                    $hosts
75
                );
76
                $question->setErrorMessage('There is no "%s" host.');
77
78
                $hostname = $helper->ask($input, $output, $question);
79
                $host = $this->deployer->hosts->get($hostname);
80
            }
81
        }
82
83
        $shell_path = 'exec $SHELL -l';
84
        if ($host->has('shell_path')) {
85
            $shell_path = 'exec '.$host->get('shell_path').' -l';
86
        }
87
88
        Context::push(new Context($host, $input, $output));
89
        $options = $host->getSshArguments();
90
        $deployPath = $host->get('deploy_path', '~');
91
92
        passthru("ssh -t $options $host 'cd '''$deployPath/current'''; $shell_path'");
93
    }
94
}
95