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