From 9851c86c59602d6619c1a6f2f2aa43eae431f9ee Mon Sep 17 00:00:00 2001 From: Lee Garrett Date: Thu, 22 Dec 2022 22:03:21 +0100 Subject: [PATCH 1/3] Implement occ status command via return codes (Fixes: #35704) Running `./occ status -e` will produce any output. However, it will: exit 0 during normal operation, exit 1 when in maintenance mode, exit 2 when `./occ upgrade` is needed. Signed-off-by: Lee Garrett --- core/Command/Status.php | 23 ++++++++++++++++++++--- lib/private/Console/Application.php | 3 ++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/core/Command/Status.php b/core/Command/Status.php index 45ccb28f5c499..9b3af59b94a34 100644 --- a/core/Command/Status.php +++ b/core/Command/Status.php @@ -29,6 +29,7 @@ use OCP\IConfig; use OCP\Util; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class Status extends Base { @@ -47,17 +48,33 @@ protected function configure() { $this ->setDescription('show some status information') - ; + ->addOption( + 'exit-code', + 'e', + InputOption::VALUE_NONE, + 'exit with 0 if running in normal mode, 1 when in maintenance mode, 2 when `./occ upgrade` is needed. Does not write any output to STDOUT.' + ); } protected function execute(InputInterface $input, OutputInterface $output): int { + $maintenanceMode = $this->config->getSystemValueBool('maintenance', false); + $needUpgrade = Util::needUpgrade(); + if ($input->getOption('exit-code')) { + if ($maintenanceMode === true) { + return 1; + } elseif ($needUpgrade == true) { + return 2; + } else { + return 0; + } + } $values = [ 'installed' => $this->config->getSystemValueBool('installed', false), 'version' => implode('.', Util::getVersion()), 'versionstring' => OC_Util::getVersionString(), 'edition' => '', - 'maintenance' => $this->config->getSystemValueBool('maintenance', false), - 'needsDbUpgrade' => Util::needUpgrade(), + 'maintenance' => $maintenanceMode, + 'needsDbUpgrade' => $needUpgrade, 'productname' => $this->themingDefaults->getProductName(), 'extendedSupport' => Util::hasExtendedSupport() ]; diff --git a/lib/private/Console/Application.php b/lib/private/Console/Application.php index fc48f57e4995d..2b48f9f5adace 100644 --- a/lib/private/Console/Application.php +++ b/lib/private/Console/Application.php @@ -181,7 +181,8 @@ private function writeMaintenanceModeInfo( InputInterface $input, ConsoleOutputInterface $output ) { if ($input->getArgument('command') !== '_completion' - && $input->getArgument('command') !== 'maintenance:mode') { + && $input->getArgument('command') !== 'maintenance:mode' + && $input->getArgument('command') !== 'status') { $errOutput = $output->getErrorOutput(); $errOutput->writeln( 'Nextcloud is in maintenance mode, hence the database isn\'t accessible.' . PHP_EOL . From 02ded7f3ee0697988b1a054252ab2e0bb90136cf Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 3 Jan 2023 12:47:58 +0100 Subject: [PATCH 2/3] Print output also with status code Signed-off-by: Joas Schilling --- core/Command/Status.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/core/Command/Status.php b/core/Command/Status.php index 9b3af59b94a34..7e6e3502adacb 100644 --- a/core/Command/Status.php +++ b/core/Command/Status.php @@ -59,15 +59,6 @@ protected function configure() { protected function execute(InputInterface $input, OutputInterface $output): int { $maintenanceMode = $this->config->getSystemValueBool('maintenance', false); $needUpgrade = Util::needUpgrade(); - if ($input->getOption('exit-code')) { - if ($maintenanceMode === true) { - return 1; - } elseif ($needUpgrade == true) { - return 2; - } else { - return 0; - } - } $values = [ 'installed' => $this->config->getSystemValueBool('installed', false), 'version' => implode('.', Util::getVersion()), @@ -80,6 +71,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int ]; $this->writeArrayInOutputFormat($input, $output, $values); + if ($input->getOption('exit-code')) { + if ($maintenanceMode === true) { + return 1; + } + if ($needUpgrade === true) { + return 2; + } + } return 0; } } From 87678c2fd55c0a5ebb05bf1cb71b4e43cb6a830d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 3 Jan 2023 15:43:25 +0100 Subject: [PATCH 3/3] No output by default when --exit-code is used Signed-off-by: Joas Schilling --- core/Command/Status.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/Command/Status.php b/core/Command/Status.php index 7e6e3502adacb..c59dac557a867 100644 --- a/core/Command/Status.php +++ b/core/Command/Status.php @@ -70,7 +70,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'extendedSupport' => Util::hasExtendedSupport() ]; - $this->writeArrayInOutputFormat($input, $output, $values); + if ($input->getOption('verbose') || !$input->getOption('exit-code')) { + $this->writeArrayInOutputFormat($input, $output, $values); + } + if ($input->getOption('exit-code')) { if ($maintenanceMode === true) { return 1;