1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentDockerCompose\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Yaml\Yaml; |
6
|
|
|
use TheAentMachine\AentDockerCompose\Aenthill\Enum\EventEnum; |
7
|
|
|
use TheAentMachine\AentDockerCompose\DockerCompose\DockerComposeService; |
8
|
|
|
use TheAentMachine\AentDockerCompose\YamlTools\YamlTools; |
9
|
|
|
use TheAentMachine\JsonEventCommand; |
10
|
|
|
|
11
|
|
|
class DeleteDockerServiceEventCommand extends JsonEventCommand |
12
|
|
|
{ |
13
|
|
|
protected function getEventName(): string |
14
|
|
|
{ |
15
|
|
|
return EventEnum::DELETE_DOCKER_SERVICE; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
protected function executeJsonEvent(array $payload): void |
19
|
|
|
{ |
20
|
|
|
$dockerComposeService = new DockerComposeService($this->log); |
21
|
|
|
$dockerComposeFilePathnames = $dockerComposeService->getDockerComposePathnames(); |
22
|
|
|
|
23
|
|
|
print_r($payload); |
24
|
|
|
|
25
|
|
|
foreach ($dockerComposeFilePathnames as $file) { |
26
|
|
|
$ymlData = Yaml::parseFile($file); |
27
|
|
|
if (array_key_exists('serviceName', $ymlData) && array_key_exists($payload['serviceName'], $ymlData['services'])) { |
28
|
|
|
$toDelete = 'services.' . $payload['serviceName']; |
29
|
|
|
$this->log->info('deleting ' . $toDelete . ' in ' . $file); |
30
|
|
|
YamlTools::delete($toDelete, $file, $file); |
31
|
|
|
} |
32
|
|
|
if (array_key_exists('namedVolumes', $payload) && array_key_exists('volumes', $ymlData)) { |
33
|
|
|
foreach ($payload['namedVolumes'] as $namedVolume) { |
34
|
|
|
if (array_key_exists($namedVolume, $ymlData['volumes'])) { |
35
|
|
|
$toDelete = 'volumes.' . $namedVolume; |
36
|
|
|
$this->log->info('deleting ' . $toDelete . ' in ' . $file); |
37
|
|
|
YamlTools::delete($toDelete, $file, $file); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|