1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentDockerCompose\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
6
|
|
|
use Symfony\Component\Yaml\Yaml; |
7
|
|
|
use TheAentMachine\AentDockerCompose\Aenthill\Enum\EventEnum; |
8
|
|
|
use TheAentMachine\AentDockerCompose\DockerCompose\DockerComposeService; |
9
|
|
|
use TheAentMachine\AentDockerCompose\YamlTools\YamlTools; |
10
|
|
|
use TheAentMachine\JsonEventCommand; |
11
|
|
|
use TheAentMachine\Service\Service; |
12
|
|
|
|
13
|
|
|
class NewDockerServiceInfoEventCommand extends JsonEventCommand |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
protected function getEventName(): string |
17
|
|
|
{ |
18
|
|
|
return EventEnum::NEW_DOCKER_SERVICE_INFO; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function executeJsonEvent(array $payload): void |
22
|
|
|
{ |
23
|
|
|
$service = Service::parsePayload($payload); |
24
|
|
|
$formattedPayload = DockerComposeService::dockerComposeServiceSerialize($service); |
25
|
|
|
|
26
|
|
|
// $this->log->debug(json_encode($formattedPayload, JSON_PRETTY_PRINT)); |
27
|
|
|
|
28
|
|
|
$yml = Yaml::dump($formattedPayload, 256, 4, Yaml::DUMP_OBJECT_AS_MAP); |
29
|
|
|
file_put_contents(YamlTools::TMP_YAML_FILE, $yml); |
30
|
|
|
DockerComposeService::checkDockerComposeFileValidity(YamlTools::TMP_YAML_FILE); |
31
|
|
|
|
32
|
|
|
$dockerComposeService = new DockerComposeService($this->log); |
33
|
|
|
$dockerComposeFilePathnames = $dockerComposeService->getDockerComposePathnames(); |
34
|
|
|
if (count($dockerComposeFilePathnames) === 1) { |
35
|
|
|
$toMerge = $dockerComposeFilePathnames; |
36
|
|
|
} else { |
37
|
|
|
$helper = $this->getHelper('question'); |
38
|
|
|
$question = new ChoiceQuestion( |
39
|
|
|
'Please choose the docker-compose file(s) in which the service will be added (e.g. 0,1) : ', |
40
|
|
|
$dockerComposeFilePathnames, |
41
|
|
|
null |
42
|
|
|
); |
43
|
|
|
$question->setMultiselect(true); |
44
|
|
|
|
45
|
|
|
$toMerge = $helper->ask($this->input, $this->output, $question); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
foreach ($toMerge as $file) { |
49
|
|
|
YamlTools::merge($file, YamlTools::TMP_YAML_FILE, $file); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
unlink(YamlTools::TMP_YAML_FILE); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|