From 8db24e23973f2028468804e64b75fc1d219c18f0 Mon Sep 17 00:00:00 2001 From: Mischa Braam Date: Wed, 20 Mar 2024 12:15:21 +0100 Subject: [PATCH] Only get current running ES when needed (#634) --- cli/valet.php | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/cli/valet.php b/cli/valet.php index 02452bd3..9479e1d9 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -2,6 +2,7 @@ command('install', function (InputInterface $input, OutputInterface $output, $withMariadb, $withMysql80, $withMysql81, $withBinary) use ($cmd) { $types = $withMariadb + $withMysql80 + $withMysql81; if ($types > 1) { - throw new Exception('Cannot install Valet+ with multiple DBMS, please pick one.'); + throw new RuntimeException('Cannot install Valet+ with multiple DBMS, please pick one.'); } $mySqlVersion = $withMariadb ? 'mariadb' : 'mysql@5.7'; $mySqlVersion = $withMysql81 ? 'mysql' : $mySqlVersion; @@ -293,7 +294,7 @@ $modes = ['install', 'on', 'enable', 'off', 'disable', 'uninstall']; if (!in_array($mode, $modes)) { - throw new Exception('Mode not found. Available modes: ' . implode(', ', $modes)); + throw new RuntimeException(sprintf('Not enough arguments (missing: "mode"). Available modes: %s', implode(', ', $modes))); } switch ($mode) { @@ -331,7 +332,7 @@ $modes = ['install', 'on', 'enable', 'off', 'disable', 'uninstall']; if (!in_array($mode, $modes)) { - throw new Exception('Mode not found. Available modes: ' . implode(', ', $modes)); + throw new RuntimeException(sprintf('Not enough arguments (missing: "mode"). Available modes: %s', implode(', ', $modes))); } switch ($mode) { @@ -366,7 +367,7 @@ $modes = ['install', 'on', 'enable', 'off', 'disable', 'uninstall']; if (!in_array($mode, $modes)) { - throw new Exception('Mode not found. Available modes: ' . implode(', ', $modes)); + throw new RuntimeException(sprintf('Not enough arguments (missing: "mode"). Available modes: %s', implode(', ', $modes))); } switch ($mode) { @@ -401,7 +402,7 @@ $modes = ['install', 'on', 'enable', 'off', 'disable', 'uninstall']; if (!in_array($mode, $modes)) { - throw new Exception('Mode not found. Available modes: ' . implode(', ', $modes)); + throw new RuntimeException(sprintf('Not enough arguments (missing: "mode"). Available modes: %s', implode(', ', $modes))); } switch ($mode) { @@ -505,7 +506,7 @@ if ($run === 'import') { info('Importing database...'); if (!$name) { - throw new Exception('Please provide a dump file'); + throw new RuntimeException('Please provide a dump file'); } // check if database already exists. @@ -532,7 +533,7 @@ } info('Resetting database, importing database...'); if (!$name) { - throw new Exception('Please provide a dump file'); + throw new RuntimeException('Please provide a dump file'); } Mysql::reimportDatabase($name, $optional); @@ -559,7 +560,7 @@ } if ($name === null || $optional === null) { - throw new Exception('Missing arguments to change root user password. Use: "valet db pwd "'); + throw new RuntimeException('Missing arguments to change root user password. Use: "valet db pwd "'); } info('Setting password for root user...'); @@ -568,7 +569,7 @@ return; } - throw new Exception('Command not found'); + throw new RuntimeException('Command not found'); }) ->descriptions('Database commands (list/ls, create, drop, reset, import, reimport, export/dump, pwd/password)'); @@ -577,14 +578,20 @@ */ $esVersions = Elasticsearch::getSupportedVersions(); $esDockerVersions = Elasticsearch::getDockerVersions(); - $esCurrentVersion = Elasticsearch::getCurrentVersion(); $app - ->command('elasticsearch', function (InputInterface $input, OutputInterface $output, $mode, $targetVersion = null) { + ->command('elasticsearch', function (InputInterface $input, OutputInterface $output, $mode, $targetVersion = null, $current = false) { $modes = ['install', 'use', 'on', 'enable', 'off', 'disable', 'uninstall']; $targetVersion = $targetVersion ?? 'opensearch'; //@todo only when we don't have any installed versions, if we do pick the first installed? + if ($current) { + // Show current running version information. + $esCurrentVersion = Elasticsearch::getCurrentVersion(); + output(sprintf('Current running version: %s', ($esCurrentVersion ?: 'none'))); + return; + } + if (!in_array($mode, $modes)) { - throw new Exception('Mode not found. Available modes: ' . implode(', ', $modes)); + throw new RuntimeException(sprintf('Not enough arguments (missing: "mode"). Available modes: %s', implode(', ', $modes))); } switch ($mode) { @@ -614,12 +621,17 @@ }) ->descriptions( 'Enable/disable/switch Elasticsearch. ' . - 'The versions [' . implode(', ', $esDockerVersions) . '] require Docker. ' . - ($esCurrentVersion !== null ? "\n " . 'Current running version: ' . $esCurrentVersion : '') + 'The versions [' . implode(', ', $esDockerVersions) . '] require Docker.' ) ->setAliases(['es']) - ->addArgument('mode', InputArgument::REQUIRED, 'Available modes: ' . implode(', ', ['install', 'use', 'on', 'enable', 'off', 'disable', 'uninstall'])) - ->addArgument('targetVersion', InputArgument::OPTIONAL, "Version to use, supported versions: " . implode(', ', $esVersions), null); + ->addArgument('mode', InputArgument::OPTIONAL, 'Available modes: ' . implode(', ', ['install', 'use', 'on', 'enable', 'off', 'disable', 'uninstall'])) + ->addArgument('targetVersion', InputArgument::OPTIONAL, "Version to use, supported versions: " . implode(', ', $esVersions), null) + ->addOption( + 'current', + 'c', + null, + 'Displays the current running version of Elasticsearch.' + ); /** @@ -629,7 +641,7 @@ $modes = ['on', 'enable', 'off', 'disable']; if (!in_array($mode, $modes)) { - throw new Exception('Mode not found. Available modes: ' . implode(', ', $modes)); + throw new RuntimeException(sprintf('Not enough arguments (missing: "mode"). Available modes: %s', implode(', ', $modes))); } $restart = false; @@ -655,7 +667,7 @@ $modes = ['on', 'enable', 'off', 'disable']; if (!in_array($mode, $modes)) { - throw new Exception('Mode not found. Available modes: ' . implode(', ', $modes)); + throw new RuntimeException(sprintf('Not enough arguments (missing: "mode"). Available modes: %s', implode(', ', $modes))); } $restart = false;