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
|
7 |
|
public function __construct(string $name, Deployer $deployer) |
31
|
|
|
{ |
32
|
7 |
|
$this->deployer = $deployer; |
33
|
7 |
|
parent::__construct($name); |
34
|
7 |
|
} |
35
|
|
|
|
36
|
7 |
|
protected function configure() |
37
|
|
|
{ |
38
|
7 |
|
$this->addOption('select', 's', Option::VALUE_OPTIONAL, 'Host selector'); |
39
|
7 |
|
} |
40
|
|
|
|
41
|
6 |
|
protected function selectHosts(Input $input, Output $output) |
42
|
|
|
{ |
43
|
6 |
|
$output->getFormatter()->setStyle('success', new OutputFormatterStyle('green')); |
44
|
6 |
|
if (!$output->isDecorated() && !defined('NO_ANSI')) { |
45
|
1 |
|
define('NO_ANSI', 'true'); |
46
|
|
|
} |
47
|
6 |
|
$selectExpression = $input->getOption('select'); |
48
|
|
|
|
49
|
6 |
|
if (empty($selectExpression)) { |
50
|
1 |
|
if (count($this->deployer->hosts) === 1) { |
51
|
|
|
$hosts = $this->deployer->hosts->all(); |
52
|
1 |
|
} else if ($input->isInteractive()) { |
53
|
1 |
|
$hostsAliases = []; |
54
|
1 |
|
foreach ($this->deployer->hosts as $host) { |
55
|
1 |
|
$hostsAliases[] = $host->alias(); |
56
|
|
|
} |
57
|
|
|
/** @var QuestionHelper $helper */ |
58
|
1 |
|
$helper = $this->getHelper('question'); |
59
|
1 |
|
$question = new ChoiceQuestion( |
60
|
1 |
|
'<question>Select hosts:</question> (comma separated)', |
61
|
|
|
$hostsAliases |
62
|
|
|
); |
63
|
1 |
|
$question->setMultiselect(true); |
64
|
1 |
|
$question->setErrorMessage('There is no "%s" host.'); |
65
|
1 |
|
$answer = $helper->ask($input, $output, $question); |
66
|
1 |
|
$answer = array_unique($answer); |
67
|
|
|
$hosts = $this->deployer->hosts->select(function (Host $host) use ($answer) { |
68
|
1 |
|
return in_array($host->alias(), $answer, true); |
69
|
1 |
|
}); |
70
|
|
|
} |
71
|
|
|
} else { |
72
|
5 |
|
$hosts = $this->deployer->selector->selectHosts($selectExpression); |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
if (empty($hosts)) { |
76
|
|
|
throw new Exception('No host selected'); |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
return $hosts; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|