Check.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UpdateNotification\Command;
  8. use OC\App\AppManager;
  9. use OC\Installer;
  10. use OCA\UpdateNotification\UpdateChecker;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. class Check extends Command {
  15. /**
  16. * @var Installer $installer
  17. */
  18. private $installer;
  19. /**
  20. * @var AppManager $appManager
  21. */
  22. private $appManager;
  23. public function __construct(
  24. AppManager $appManager,
  25. private UpdateChecker $updateChecker,
  26. Installer $installer,
  27. ) {
  28. parent::__construct();
  29. $this->installer = $installer;
  30. $this->appManager = $appManager;
  31. }
  32. protected function configure(): void {
  33. $this
  34. ->setName('update:check')
  35. ->setDescription('Check for server and app updates')
  36. ;
  37. }
  38. protected function execute(InputInterface $input, OutputInterface $output): int {
  39. $updatesAvailableCount = 0;
  40. // Server
  41. $r = $this->updateChecker->getUpdateState();
  42. if (isset($r['updateAvailable']) && $r['updateAvailable']) {
  43. $output->writeln($r['updateVersionString'] . ' is available. Get more information on how to update at ' . $r['updateLink'] . '.');
  44. $updatesAvailableCount += 1;
  45. }
  46. // Apps
  47. $apps = $this->appManager->getInstalledApps();
  48. foreach ($apps as $app) {
  49. $update = $this->installer->isUpdateAvailable($app);
  50. if ($update !== false) {
  51. $output->writeln('Update for ' . $app . ' to version ' . $update . ' is available.');
  52. $updatesAvailableCount += 1;
  53. }
  54. }
  55. // Report summary
  56. if ($updatesAvailableCount === 0) {
  57. $output->writeln('<info>Everything up to date</info>');
  58. } elseif ($updatesAvailableCount === 1) {
  59. $output->writeln('<comment>1 update available</comment>');
  60. } else {
  61. $output->writeln('<comment>' . $updatesAvailableCount . ' updates available</comment>');
  62. }
  63. return 0;
  64. }
  65. }