1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* (c) Anton Medvedev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Deployer\Command; |
12
|
|
|
|
13
|
|
|
use Deployer\Deployer; |
14
|
|
|
use Deployer\Exception\WillAskUser; |
15
|
|
|
use Deployer\Task\Context; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface as Input; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface as Output; |
20
|
|
|
use Symfony\Component\Yaml\Yaml; |
21
|
|
|
|
22
|
|
|
class ConfigCommand extends SelectCommand |
23
|
|
|
{ |
24
|
|
|
public function __construct(Deployer $deployer) |
25
|
|
|
{ |
26
|
|
|
parent::__construct('config', $deployer); |
27
|
|
|
$this->setDescription('Get all configuration options for hosts'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
parent::configure(); |
33
|
|
|
$this->addOption('format', null, InputOption::VALUE_OPTIONAL, 'The output format (json, yaml)', 'yaml'); |
34
|
|
|
$this->getDefinition()->getArgument('selector')->setDefault(['all']); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function execute(Input $input, Output $output): int |
38
|
|
|
{ |
39
|
|
|
$this->deployer->input = $input; |
40
|
|
|
$this->deployer->output = new NullOutput(); |
41
|
|
|
$hosts = $this->selectHosts($input, $output); |
42
|
|
|
$data = []; |
43
|
|
|
$keys = $this->deployer->config->keys(); |
44
|
|
|
define('DEPLOYER_NO_ASK', true); |
45
|
|
|
foreach ($hosts as $host) { |
46
|
|
|
Context::push(new Context($host)); |
47
|
|
|
$values = []; |
48
|
|
|
foreach ($keys as $key) { |
49
|
|
|
try { |
50
|
|
|
$values[$key] = $host->get($key); |
51
|
|
|
} catch (WillAskUser $exception) { |
52
|
|
|
$values[$key] = ['ask' => $exception->getMessage()]; |
53
|
|
|
} catch (\Throwable $exception) { |
54
|
|
|
$values[$key] = ['error' => $exception->getMessage()]; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
foreach ($host->config()->persist() as $k => $v) { |
58
|
|
|
$values[$k] = $v; |
59
|
|
|
} |
60
|
|
|
ksort($values); |
61
|
|
|
$data[$host->getAlias()] = $values; |
62
|
|
|
Context::pop(); |
63
|
|
|
} |
64
|
|
|
$format = $input->getOption('format'); |
65
|
|
|
switch ($format) { |
66
|
|
|
case 'json': |
67
|
|
|
$output->writeln(json_encode($data, JSON_PRETTY_PRINT)); |
68
|
|
|
break; |
69
|
|
|
|
70
|
|
|
case 'yaml': |
71
|
|
|
$output->write(Yaml::dump($data)); |
72
|
|
|
break; |
73
|
|
|
|
74
|
|
|
default: |
75
|
|
|
throw new \Exception("Unknown format: $format."); |
76
|
|
|
} |
77
|
|
|
return 0; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|