Skip to content

Commit

Permalink
Only get current running ES when needed (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischabraam committed Mar 20, 2024
1 parent 8aa9c85 commit 8db24e2
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<?php

use Silly\Application;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -43,7 +44,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' : '[email protected]';
$mySqlVersion = $withMysql81 ? 'mysql' : $mySqlVersion;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand All @@ -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);

Expand All @@ -559,7 +560,7 @@
}

if ($name === null || $optional === null) {
throw new Exception('Missing arguments to change root user password. Use: "valet db pwd <old> <new>"');
throw new RuntimeException('Missing arguments to change root user password. Use: "valet db pwd <old> <new>"');
}

info('Setting password for root user...');
Expand All @@ -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)');

Expand All @@ -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: <fg=green>%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) {
Expand Down Expand Up @@ -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.'
);


/**
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 8db24e2

Please sign in to comment.