Check.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /**
  24. * @var UpdateChecker $updateChecker
  25. */
  26. private $updateChecker;
  27. public function __construct(AppManager $appManager, UpdateChecker $updateChecker, Installer $installer) {
  28. parent::__construct();
  29. $this->installer = $installer;
  30. $this->appManager = $appManager;
  31. $this->updateChecker = $updateChecker;
  32. }
  33. protected function configure(): void {
  34. $this
  35. ->setName('update:check')
  36. ->setDescription('Check for server and app updates')
  37. ;
  38. }
  39. protected function execute(InputInterface $input, OutputInterface $output): int {
  40. $updatesAvailableCount = 0;
  41. // Server
  42. $r = $this->updateChecker->getUpdateState();
  43. if (isset($r['updateAvailable']) && $r['updateAvailable']) {
  44. $output->writeln($r['updateVersionString'] . ' is available. Get more information on how to update at '. $r['updateLink'] . '.');
  45. $updatesAvailableCount += 1;
  46. }
  47. // Apps
  48. $apps = $this->appManager->getInstalledApps();
  49. foreach ($apps as $app) {
  50. $update = $this->installer->isUpdateAvailable($app);
  51. if ($update !== false) {
  52. $output->writeln('Update for ' . $app . ' to version ' . $update . ' is available.');
  53. $updatesAvailableCount += 1;
  54. }
  55. }
  56. // Report summary
  57. if ($updatesAvailableCount === 0) {
  58. $output->writeln('<info>Everything up to date</info>');
  59. } elseif ($updatesAvailableCount === 1) {
  60. $output->writeln('<comment>1 update available</comment>');
  61. } else {
  62. $output->writeln('<comment>' . $updatesAvailableCount . ' updates available</comment>');
  63. }
  64. return 0;
  65. }
  66. }