1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentDockerCompose\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
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\Hercule; |
11
|
|
|
use TheAentMachine\JsonEventCommand; |
12
|
|
|
|
13
|
|
|
class DeleteDockerServiceEventCommand extends JsonEventCommand |
14
|
|
|
{ |
15
|
|
|
protected function getEventName(): string |
16
|
|
|
{ |
17
|
|
|
return EventEnum::DELETE_DOCKER_SERVICE; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
protected function executeJsonEvent(array $payload): void |
21
|
|
|
{ |
22
|
|
|
Hercule::setHandledEvents(EventEnum::getHandledEvents()); |
23
|
|
|
|
24
|
|
|
$dockerComposeService = new DockerComposeService($this->log); |
25
|
|
|
$dockerComposeFilePathnames = $dockerComposeService->getDockerComposePathnames(); |
26
|
|
|
|
27
|
|
|
$this->log->debug(json_encode($payload, JSON_PRETTY_PRINT)); |
28
|
|
|
$helper = $this->getHelper('question'); |
29
|
|
|
foreach ($dockerComposeFilePathnames as $file) { |
30
|
|
|
$ymlData = Yaml::parseFile($file); |
31
|
|
|
if (array_key_exists('serviceName', $ymlData) && array_key_exists($payload['serviceName'], $ymlData['services'])) { |
32
|
|
|
$toDelete = 'services.' . $payload['serviceName']; |
33
|
|
|
$question = $this->getDeleteConfirmationQuestion($toDelete); |
34
|
|
|
$doDelete = $helper->ask($this->input, $this->output, $question); |
35
|
|
|
if ($doDelete) { |
36
|
|
|
$this->log->info('deleting ' . $toDelete . ' in ' . $file); |
37
|
|
|
YamlTools::delete($toDelete, $file, $file); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
if (array_key_exists('namedVolumes', $payload) && array_key_exists('volumes', $ymlData)) { |
41
|
|
|
foreach ($payload['namedVolumes'] as $namedVolume) { |
42
|
|
|
if (array_key_exists($namedVolume, $ymlData['volumes'])) { |
43
|
|
|
$toDelete = 'volumes.' . $namedVolume; |
44
|
|
|
$question = $this->getDeleteConfirmationQuestion($toDelete); |
45
|
|
|
$doDelete = $helper->ask($this->input, $this->output, $question); |
46
|
|
|
if ($doDelete) { |
47
|
|
|
$this->log->info('deleting ' . $toDelete . ' in ' . $file); |
48
|
|
|
YamlTools::delete($toDelete, $file, $file); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $elementToDel |
58
|
|
|
* @param bool $default |
59
|
|
|
* @return ConfirmationQuestion |
60
|
|
|
*/ |
61
|
|
|
private function getDeleteConfirmationQuestion(string $elementToDel, bool $default = false): ConfirmationQuestion |
62
|
|
|
{ |
63
|
|
|
return new ConfirmationQuestion( |
64
|
|
|
"Do you want to delete $elementToDel ? [y/N]\n > ", |
65
|
|
|
$default |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|