From b25c2cdfcf4bc114cc002417118309d578ee0934 Mon Sep 17 00:00:00 2001 From: Mischa Braam Date: Thu, 21 Sep 2023 16:23:00 +0200 Subject: [PATCH] Add uninstall to services --- cli/ValetPlus/AbstractService.php | 24 ++++++++++++++++-- cli/ValetPlus/Mailhog.php | 1 + cli/ValetPlus/Rabbitmq.php | 4 +++ cli/ValetPlus/Redis.php | 3 +++ cli/ValetPlus/Varnish.php | 1 + cli/valet.php | 42 ++++++++++++++++++++----------- 6 files changed, 58 insertions(+), 17 deletions(-) diff --git a/cli/ValetPlus/AbstractService.php b/cli/ValetPlus/AbstractService.php index 09bd5369..6977d05e 100644 --- a/cli/ValetPlus/AbstractService.php +++ b/cli/ValetPlus/AbstractService.php @@ -66,7 +66,7 @@ public function getConfigClassName(): string } /** - * Returns wether the service is enabled or not. + * Returns whether the service is enabled or not. * * @return bool * @throws JsonException @@ -84,7 +84,7 @@ public function isEnabled(): bool } /** - * Stores the active state in the configuration. + * Stores the enabled state of the service in the configuration. * * @param bool $state * @throws JsonException @@ -100,6 +100,26 @@ public function setEnabled(bool $state): void $this->configuration->write($config); } + /** + * Removes the enabled state of the service from the configuration. + * + * @return void + * @throws JsonException + */ + public function removeEnabled(): void + { + $config = $this->configuration->read(); + $name = $this->getConfigClassName(); + if (!isset($config[$name])) { + $config[$name] = []; + } + if (isset($config[$name]['enabled'])) { + unset($config[$name]['enabled']); + } + $config = array_filter($config); + $this->configuration->write($config); + } + /** * Stops the service and stores in configuration it should not be started. * diff --git a/cli/ValetPlus/Mailhog.php b/cli/ValetPlus/Mailhog.php index b1344d00..9be3a895 100644 --- a/cli/ValetPlus/Mailhog.php +++ b/cli/ValetPlus/Mailhog.php @@ -109,6 +109,7 @@ public function restart(): void public function uninstall(): void { $this->stop(); + $this->removeEnabled(); $this->brew->uninstallFormula(static::SERVICE_NAME); $this->files->unlink(BREW_PREFIX . '/var/log/mailhog.log'); } diff --git a/cli/ValetPlus/Rabbitmq.php b/cli/ValetPlus/Rabbitmq.php index 852a985a..c1910315 100644 --- a/cli/ValetPlus/Rabbitmq.php +++ b/cli/ValetPlus/Rabbitmq.php @@ -64,10 +64,14 @@ public function restart(): void public function uninstall(): void { $this->stop(); + $this->removeEnabled(); $this->brew->uninstallFormula(static::SERVICE_NAME); if (file_exists(BREW_PREFIX . '/var/lib/rabbitmq')) { $this->files->rmDirAndContents(BREW_PREFIX . '/var/lib/rabbitmq'); } + if (file_exists(BREW_PREFIX . '/var/log/rabbitmq')) { + $this->files->rmDirAndContents(BREW_PREFIX . '/var/log/rabbitmq'); + } } } diff --git a/cli/ValetPlus/Redis.php b/cli/ValetPlus/Redis.php index b5f81b9c..a13e4ae7 100644 --- a/cli/ValetPlus/Redis.php +++ b/cli/ValetPlus/Redis.php @@ -61,6 +61,9 @@ public function restart(): void public function uninstall(): void { $this->stop(); + $this->removeEnabled(); $this->brew->uninstallFormula(static::SERVICE_NAME); + + $this->files->unlink(BREW_PREFIX . '/var/log/redis.log'); } } diff --git a/cli/ValetPlus/Varnish.php b/cli/ValetPlus/Varnish.php index 4440fcd5..fe0409a6 100644 --- a/cli/ValetPlus/Varnish.php +++ b/cli/ValetPlus/Varnish.php @@ -61,6 +61,7 @@ public function restart(): void public function uninstall(): void { $this->stop(); + $this->removeEnabled(); $this->brew->uninstallFormula(static::SERVICE_NAME); if (file_exists(BREW_PREFIX . '/var/varnish')) { diff --git a/cli/valet.php b/cli/valet.php index 0143a4d3..4ddb7353 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -291,7 +291,7 @@ */ $app ->command('varnish', function (OutputInterface $output, string $mode = null) { - $modes = ['install', 'on', 'enable', 'off', 'disable']; + $modes = ['install', 'on', 'enable', 'off', 'disable', 'uninstall']; if (!in_array($mode, $modes)) { throw new Exception('Mode not found. Available modes: ' . implode(', ', $modes)); @@ -301,28 +301,32 @@ case 'install': Varnish::install(); - return; + break; case 'enable': case 'on': Varnish::enable(); - return; + break; case 'disable': case 'off': Varnish::disable(); - return; + break; + case 'uninstall': + Varnish::uninstall(); + + break; } }) ->descriptions('Enable/disable Varnish') - ->addArgument('mode', InputArgument::REQUIRED, 'Available modes: ' . implode(', ', ['install', 'on', 'enable', 'off', 'disable'])); + ->addArgument('mode', InputArgument::REQUIRED, 'Available modes: ' . implode(', ', ['install', 'on', 'enable', 'off', 'disable', 'uninstall'])); /** * Redis services. */ $app ->command('redis', function (OutputInterface $output, string $mode = null) { - $modes = ['install', 'on', 'enable', 'off', 'disable']; + $modes = ['install', 'on', 'enable', 'off', 'disable', 'uninstall']; if (!in_array($mode, $modes)) { throw new Exception('Mode not found. Available modes: ' . implode(', ', $modes)); @@ -332,28 +336,32 @@ case 'install': Redis::install(); - return; + break; case 'enable': case 'on': Redis::enable(); - return; + break; case 'disable': case 'off': Redis::disable(); - return; + break; + case 'uninstall': + Redis::uninstall(); + + break; } }) ->descriptions('Enable/disable Redis') - ->addArgument('mode', InputArgument::REQUIRED, 'Available modes: ' . implode(', ', ['install', 'on', 'enable', 'off', 'disable'])); + ->addArgument('mode', InputArgument::REQUIRED, 'Available modes: ' . implode(', ', ['install', 'on', 'enable', 'off', 'disable', 'uninstall'])); /** * Rabbitmq services. */ $app ->command('rabbitmq', function (OutputInterface $output, string $mode = null) { - $modes = ['install', 'on', 'enable', 'off', 'disable']; + $modes = ['install', 'on', 'enable', 'off', 'disable', 'uninstall']; if (!in_array($mode, $modes)) { throw new Exception('Mode not found. Available modes: ' . implode(', ', $modes)); @@ -363,21 +371,25 @@ case 'install': Rabbitmq::install(); - return; + break; case 'enable': case 'on': Rabbitmq::enable(); - return; + break; case 'disable': case 'off': Rabbitmq::disable(); - return; + break; + case 'uninstall': + Rabbitmq::uninstall(); + + break; } }) ->descriptions('Enable/disable Rabbitmq') - ->addArgument('mode', InputArgument::REQUIRED, 'Available modes: ' . implode(', ', ['install', 'on', 'enable', 'off', 'disable'])); + ->addArgument('mode', InputArgument::REQUIRED, 'Available modes: ' . implode(', ', ['install', 'on', 'enable', 'off', 'disable', 'uninstall'])); /** * Database services and commands.