1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentDockerCompose\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Yaml\Yaml; |
6
|
|
|
use TheAentMachine\AentDockerCompose\DockerCompose\DockerComposeService; |
7
|
|
|
use TheAentMachine\AentDockerCompose\YamlTools\YamlTools; |
8
|
|
|
use TheAentMachine\JsonEventCommand; |
9
|
|
|
|
10
|
|
|
class DeleteServiceEventCommand extends JsonEventCommand |
11
|
|
|
{ |
12
|
|
|
protected function getEventName(): string |
13
|
|
|
{ |
14
|
|
|
return 'DELETE_SERVICE'; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
protected function executeJsonEvent(array $payload): ?array |
18
|
|
|
{ |
19
|
|
|
$dockerComposeService = new DockerComposeService($this->log); |
20
|
|
|
$dockerComposeFilePathnames = $dockerComposeService->getDockerComposePathnames(); |
21
|
|
|
|
22
|
|
|
// $this->log->debug(json_encode($payload, JSON_PRETTY_PRINT)); |
23
|
|
|
|
24
|
|
|
foreach ($dockerComposeFilePathnames as $file) { |
25
|
|
|
$ymlData = Yaml::parseFile($file); |
26
|
|
|
if (array_key_exists('serviceName', $ymlData) && array_key_exists($payload['serviceName'], $ymlData['services'])) { |
27
|
|
|
$serviceName = $payload['serviceName']; |
28
|
|
|
$elemToDelete = ['services', $serviceName]; |
29
|
|
|
|
30
|
|
|
$doDelete = $this->getAentHelper() |
31
|
|
|
->question("Delete the service $serviceName in $file?") |
32
|
|
|
->yesNoQuestion() |
33
|
|
|
->setDefault('n') |
34
|
|
|
->ask(); |
35
|
|
|
|
36
|
|
|
if ($doDelete) { |
37
|
|
|
$this->log->debug('deleting ' . $elemToDelete . ' in ' . $file); |
|
|
|
|
38
|
|
|
YamlTools::deleteYamlItem($elemToDelete, $file); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
if (array_key_exists('namedVolumes', $payload) && array_key_exists('volumes', $ymlData)) { |
42
|
|
|
foreach ($payload['namedVolumes'] as $namedVolume) { |
43
|
|
|
if (array_key_exists($namedVolume, $ymlData['volumes'])) { |
44
|
|
|
$elemToDelete = ['volumes', $namedVolume]; |
45
|
|
|
|
46
|
|
|
$doDelete = $this->getAentHelper() |
47
|
|
|
->question("Delete the named volume $namedVolume in $file?") |
48
|
|
|
->yesNoQuestion() |
49
|
|
|
->setDefault('n') |
50
|
|
|
->ask(); |
51
|
|
|
|
52
|
|
|
if ($doDelete) { |
53
|
|
|
$this->log->debug('deleting ' . $elemToDelete . ' in ' . $file); |
54
|
|
|
YamlTools::deleteYamlItem($elemToDelete, $file); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
return null; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|