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
|
|
|
public function __construct(string $name, Deployer $deployer) |
31
|
|
|
{ |
32
|
|
|
$this->deployer = $deployer; |
33
|
|
|
parent::__construct($name); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
$this->addOption('selector', 's', Option::VALUE_OPTIONAL, 'Host selector'); |
39
|
|
|
|
40
|
|
|
// Add global options defined with `option()` func. |
41
|
|
|
$this->getDefinition()->addOptions($this->deployer->inputDefinition->getOptions()); |
42
|
|
|
|
43
|
|
|
$this->addOption( |
44
|
|
|
'option', |
45
|
|
|
'o', |
46
|
|
|
Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, |
47
|
|
|
'Sets configuration option' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function selectHosts(Input $input, Output $output) |
52
|
|
|
{ |
53
|
|
|
$output->getFormatter()->setStyle('success', new OutputFormatterStyle('green')); |
54
|
|
|
if (!$output->isDecorated()) { |
55
|
|
|
define('NO_ANSI', 'true'); |
56
|
|
|
} |
57
|
|
|
$selectExpression = $input->getOption('selector'); |
58
|
|
|
$this->parseOptions($input->getOption('option')); |
59
|
|
|
|
60
|
|
|
if (empty($selectExpression)) { |
61
|
|
|
$hostsAliases = []; |
62
|
|
|
foreach ($this->deployer->hosts as $host) { |
63
|
|
|
$hostsAliases[] = $host->alias(); |
64
|
|
|
} |
65
|
|
|
/** @var QuestionHelper $helper */ |
66
|
|
|
$helper = $this->getHelper('question'); |
67
|
|
|
$question = new ChoiceQuestion( |
68
|
|
|
'<question>Select hosts:</question> (comma separated)', |
69
|
|
|
$hostsAliases |
70
|
|
|
); |
71
|
|
|
$question->setMultiselect(true); |
72
|
|
|
$question->setErrorMessage('There is no "%s" host.'); |
73
|
|
|
$answer = $helper->ask($input, $output, $question); |
74
|
|
|
$answer = array_unique($answer); |
75
|
|
|
$hosts = $this->deployer->hosts->select(function (Host $host) use ($answer) { |
76
|
|
|
return in_array($host->alias(), $answer, true); |
77
|
|
|
}); |
78
|
|
|
} else { |
79
|
|
|
$hosts = $this->deployer->selector->selectHosts($selectExpression); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (empty($hosts)) { |
83
|
|
|
throw new Exception('No host selected'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $hosts; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function parseOptions(array $options) |
90
|
|
|
{ |
91
|
|
|
foreach ($options as $option) { |
92
|
|
|
list($name, $value) = explode('=', $option); |
93
|
|
|
$value = $this->castValueToPhpType($value); |
94
|
|
|
$this->deployer->config->set($name, $value); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function castValueToPhpType($value) |
99
|
|
|
{ |
100
|
|
|
switch ($value) { |
101
|
|
|
case 'true': |
102
|
|
|
return true; |
103
|
|
|
case 'false': |
104
|
|
|
return false; |
105
|
|
|
default: |
106
|
|
|
return $value; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|