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\Exception\Exception; |
||
12 | use Deployer\Exception\GracefulShutdownException; |
||
13 | use Deployer\Executor\Planner; |
||
14 | use Deployer\Host\Host; |
||
15 | use Deployer\Host\Localhost; |
||
16 | use Symfony\Component\Console\Command\Command; |
||
17 | use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
||
18 | use Symfony\Component\Console\Helper\QuestionHelper; |
||
19 | use Symfony\Component\Console\Helper\Table; |
||
20 | use Symfony\Component\Console\Input\InputArgument; |
||
21 | use Symfony\Component\Console\Input\InputInterface as Input; |
||
22 | use Symfony\Component\Console\Input\InputOption as Option; |
||
23 | use Symfony\Component\Console\Output\OutputInterface as Output; |
||
24 | use Symfony\Component\Console\Question\ChoiceQuestion; |
||
25 | |||
26 | abstract class SelectCommand extends Command |
||
27 | { |
||
28 | protected $deployer; |
||
29 | |||
30 | 12 | public function __construct(string $name, Deployer $deployer) |
|
31 | { |
||
32 | 12 | $this->deployer = $deployer; |
|
33 | 12 | parent::__construct($name); |
|
34 | 12 | } |
|
35 | |||
36 | 12 | protected function configure() |
|
37 | { |
||
38 | 12 | $this->addOption('select', 's', Option::VALUE_OPTIONAL, 'Host selector'); |
|
39 | 12 | } |
|
40 | |||
41 | 12 | protected function selectHosts(Input $input, Output $output) |
|
42 | { |
||
43 | 12 | $output->getFormatter()->setStyle('success', new OutputFormatterStyle('green')); |
|
44 | 12 | if (!$output->isDecorated() && !defined('NO_ANSI')) { |
|
45 | 1 | define('NO_ANSI', 'true'); |
|
46 | } |
||
47 | 12 | $selectExpression = $input->getOption('select'); |
|
48 | |||
49 | 12 | if (empty($selectExpression)) { |
|
50 | 2 | if (count($this->deployer->hosts) === 1) { |
|
51 | $hosts = $this->deployer->hosts->all(); |
||
52 | 2 | } else if ($input->isInteractive()) { |
|
53 | 2 | $hostsAliases = []; |
|
54 | 2 | foreach ($this->deployer->hosts as $host) { |
|
55 | 2 | $hostsAliases[] = $host->getAlias(); |
|
56 | } |
||
57 | /** @var QuestionHelper $helper */ |
||
58 | 2 | $helper = $this->getHelper('question'); |
|
59 | 2 | $question = new ChoiceQuestion( |
|
60 | 2 | '<question>Select hosts:</question> (comma separated)', |
|
61 | $hostsAliases |
||
62 | ); |
||
63 | 2 | $question->setMultiselect(true); |
|
64 | 2 | $question->setErrorMessage('There is no "%s" host.'); |
|
65 | 2 | $answer = $helper->ask($input, $output, $question); |
|
66 | 2 | $answer = array_unique($answer); |
|
67 | $hosts = $this->deployer->hosts->select(function (Host $host) use ($answer) { |
||
68 | 2 | return in_array($host->getAlias(), $answer, true); |
|
69 | 2 | }); |
|
70 | } |
||
71 | } else { |
||
72 | 10 | $hosts = $this->deployer->selector->selectHosts($selectExpression); |
|
73 | } |
||
74 | |||
75 | 12 | if (empty($hosts)) { |
|
76 | throw new Exception('No host selected'); |
||
77 | } |
||
78 | |||
79 | 12 | return $hosts; |
|
80 | } |
||
81 | } |
||
82 |