1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Command; |
4
|
|
|
|
5
|
|
|
use Db3v4l\Core\DatabaseSchemaManager; |
|
|
|
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
|
|
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
|
|
|
9
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
|
|
|
10
|
|
|
|
11
|
|
|
class InstanceList extends SQLExecutingCommand |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
protected static $defaultName = 'db3v4l:instance:list'; |
14
|
|
|
|
15
|
|
|
protected function configure() |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$this |
18
|
|
|
->setDescription('Lists all configured database servers') |
19
|
|
|
->addCommonOptions() |
|
|
|
|
20
|
|
|
; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @todo allow to dump full configuration, not just db vendor + version |
|
|
|
|
25
|
|
|
* @param InputInterface $input |
|
|
|
|
26
|
|
|
* @param OutputInterface $output |
|
|
|
|
27
|
|
|
* @return int |
|
|
|
|
28
|
|
|
* @throws \Exception |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
31
|
|
|
{ |
32
|
|
|
$this->setOutput($output); |
33
|
|
|
$this->setVerbosity($output->getVerbosity()); |
34
|
|
|
|
35
|
|
|
$instanceList = $this->parseCommonOptions($input); |
36
|
|
|
|
37
|
|
|
$result = $this->listInstances($instanceList); |
38
|
|
|
|
39
|
|
|
$extraResults = $this->listDatabasesVersion($instanceList)['data']; |
40
|
|
|
foreach($result as $instanceName => $instanceDesc) { |
|
|
|
|
41
|
|
|
if (isset($extraResults[$instanceName])) { |
42
|
|
|
$result[$instanceName]['version'] = $extraResults[$instanceName]; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->writeResult($result); |
47
|
|
|
|
48
|
|
|
return 0; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
|
|
|
|
52
|
|
|
* @todo allow to retrieve exact version number dynamically, see getRetrieveVersionInfoSqlAction |
|
|
|
|
53
|
|
|
* @param string[][] $instanceList |
|
|
|
|
54
|
|
|
* @return string[][] |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
protected function listInstances($instanceList) |
57
|
|
|
{ |
58
|
|
|
$out = []; |
59
|
|
|
foreach($instanceList as $instanceName => $connectionSpec) { |
|
|
|
|
60
|
|
|
//$connectionSpec = $this->dbConfigurationManager->getInstanceConfiguration($instanceName); |
61
|
|
|
$out[$instanceName] = [ |
62
|
|
|
'vendor' => $connectionSpec['vendor'], |
63
|
|
|
'version' => $connectionSpec['version'] |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
return $out; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function listDatabasesVersion($instanceList) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
return $this->executeSqlAction( |
72
|
|
|
$instanceList, |
73
|
|
|
'Getting database version information', |
74
|
|
|
function ($schemaManager, $instanceName) { |
|
|
|
|
75
|
|
|
/** @var DatabaseSchemaManager $schemaManager */ |
|
|
|
|
76
|
|
|
return $schemaManager->getRetrieveVersionInfoSqlAction(); |
77
|
|
|
} |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function writeResult($result) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
switch ($this->outputFormat) { |
84
|
|
|
case 'json': |
|
|
|
|
85
|
|
|
$data = json_encode($result, JSON_PRETTY_PRINT); |
86
|
|
|
break; |
87
|
|
|
case 'php': |
|
|
|
|
88
|
|
|
$data = var_export($result, true); |
89
|
|
|
break; |
90
|
|
|
case 'text': |
|
|
|
|
91
|
|
|
case 'yml': |
|
|
|
|
92
|
|
|
case 'yaml': |
|
|
|
|
93
|
|
|
$data = Yaml::dump($result); |
94
|
|
|
break; |
95
|
|
|
default: |
|
|
|
|
96
|
|
|
throw new \Exception("Unsupported output format: '{$this->outputFormat}'"); |
97
|
|
|
break; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($this->outputFile != null) { |
101
|
|
|
file_put_contents($this->outputFile, $data); |
102
|
|
|
$this->writeln("Results saved to file {$this->outputFile}"); |
103
|
|
|
} else { |
104
|
|
|
$this->writeln($data, OutputInterface::VERBOSITY_QUIET, OutputInterface::OUTPUT_RAW); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|