|
1
|
|
|
<?php |
|
2
|
|
|
namespace TheAentMachine\AentDockerCompose\Command; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Command\Command; |
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
8
|
|
|
use TheAentMachine\AentDockerCompose\Aenthill\Enum\EventEnum; |
|
9
|
|
|
use TheAentMachine\AentDockerCompose\Aenthill\Enum\PheromoneEnum; |
|
10
|
|
|
use TheAentMachine\AentDockerCompose\Aenthill\Log; |
|
11
|
|
|
use TheAentMachine\AentDockerCompose\DockerCompose\DockerCompose; |
|
12
|
|
|
|
|
13
|
|
|
class RootCommand extends Command |
|
14
|
|
|
{ |
|
15
|
|
|
protected function configure() |
|
16
|
|
|
{ |
|
17
|
|
|
$this |
|
18
|
|
|
->setName('handle') |
|
19
|
|
|
->setDescription('Handle an event') |
|
20
|
|
|
->setHelp('handle event [payload]') |
|
21
|
|
|
->addArgument('event', InputArgument::REQUIRED, 'The event name') |
|
22
|
|
|
->addArgument('payload', InputArgument::OPTIONAL, 'The event payload'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param InputInterface $input |
|
27
|
|
|
* @param OutputInterface $output |
|
28
|
|
|
* @return int |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
31
|
|
|
{ |
|
32
|
|
|
$this->printWelcomeMessage($output); |
|
33
|
|
|
$log = new Log($output); |
|
34
|
|
|
|
|
35
|
|
|
try { |
|
36
|
|
|
$log->setLevel(getenv(PheromoneEnum::PHEROMONE_LOG_LEVEL)); |
|
37
|
|
|
} catch (\Exception $e) { |
|
38
|
|
|
$log->errorln($e->getMessage()); |
|
39
|
|
|
return 1; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$event = $input->getArgument('event'); |
|
43
|
|
|
$payload = $input->getArgument('payload'); |
|
44
|
|
|
$command = $this->handleEvent($event); |
|
45
|
|
|
|
|
46
|
|
|
if (empty($command)) { |
|
47
|
|
|
$log->infoln("event $event is not handled by this aent, bye!"); |
|
48
|
|
|
return 0; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$dockerCompose = new DockerCompose($log); |
|
52
|
|
|
try { |
|
53
|
|
|
$dockerCompose->seekFiles(); |
|
54
|
|
|
} catch (\Exception $e) { |
|
55
|
|
|
$log->errorln($e->getMessage()); |
|
56
|
|
|
return 1; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$command->setLog($log); |
|
60
|
|
|
$command->setPayload($payload); |
|
61
|
|
|
|
|
62
|
|
|
return $command->execute($input, $output); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $event |
|
67
|
|
|
* @return null|AddEventCommand|RemoveEventCommand |
|
68
|
|
|
*/ |
|
69
|
|
|
private function handleEvent(string $event): ?EventCommand |
|
70
|
|
|
{ |
|
71
|
|
|
switch ($event) { |
|
72
|
|
|
case EventEnum::ADD: |
|
73
|
|
|
return new AddEventCommand(); |
|
74
|
|
|
case EventEnum::REMOVE: |
|
75
|
|
|
return new RemoveEventCommand(); |
|
76
|
|
|
default: |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param OutputInterface $output |
|
83
|
|
|
*/ |
|
84
|
|
|
private function printWelcomeMessage(OutputInterface $output): void |
|
85
|
|
|
{ |
|
86
|
|
|
$output->writeln(" |
|
87
|
|
|
_ _____ _ _____ |
|
88
|
|
|
/\ | | | __ \ | | / ____| |
|
89
|
|
|
/ \ ___ _ __ | |_| | | | ___ ___| | _____ _ __| | ___ _ __ ___ _ __ ___ ___ ___ |
|
90
|
|
|
/ /\ \ / _ \ '_ \| __| | | |/ _ \ / __| |/ / _ \ '__| | / _ \| '_ ` _ \| '_ \ / _ \/ __|/ _ \ |
|
91
|
|
|
/ ____ \ __/ | | | |_| |__| | (_) | (__| < __/ | | |___| (_) | | | | | | |_) | (_) \__ \ __/ |
|
92
|
|
|
/_/ \_\___|_| |_|\__|_____/ \___/ \___|_|\_\___|_| \_____\___/|_| |_| |_| .__/ \___/|___/\___| |
|
93
|
|
|
| | |
|
94
|
|
|
|_| |
|
95
|
|
|
"); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|