Check.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018, Tobia De Koninck (tobia@ledfan.be)
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Tobia De Koninck <LEDfan@users.noreply.github.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\UpdateNotification\Command;
  26. use OC\App\AppManager;
  27. use OC\Installer;
  28. use OCA\UpdateNotification\UpdateChecker;
  29. use Symfony\Component\Console\Command\Command;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class Check extends Command {
  33. /**
  34. * @var Installer $installer
  35. */
  36. private $installer;
  37. /**
  38. * @var AppManager $appManager
  39. */
  40. private $appManager;
  41. /**
  42. * @var UpdateChecker $updateChecker
  43. */
  44. private $updateChecker;
  45. public function __construct(AppManager $appManager, UpdateChecker $updateChecker, Installer $installer) {
  46. parent::__construct();
  47. $this->installer = $installer;
  48. $this->appManager = $appManager;
  49. $this->updateChecker = $updateChecker;
  50. }
  51. protected function configure() {
  52. $this
  53. ->setName('update:check')
  54. ->setDescription('Check for server and app updates')
  55. ;
  56. }
  57. protected function execute(InputInterface $input, OutputInterface $output): int {
  58. $updatesAvailableCount = 0;
  59. // Server
  60. $r = $this->updateChecker->getUpdateState();
  61. if (isset($r['updateAvailable']) && $r['updateAvailable']) {
  62. $output->writeln($r['updateVersionString'] . ' is available. Get more information on how to update at '. $r['updateLink'] . '.');
  63. $updatesAvailableCount += 1;
  64. }
  65. // Apps
  66. $apps = $this->appManager->getInstalledApps();
  67. foreach ($apps as $app) {
  68. $update = $this->installer->isUpdateAvailable($app);
  69. if ($update !== false) {
  70. $output->writeln('Update for ' . $app . ' to version ' . $update . ' is available.');
  71. $updatesAvailableCount += 1;
  72. }
  73. }
  74. // Report summary
  75. if ($updatesAvailableCount === 0) {
  76. $output->writeln('<info>Everything up to date</info>');
  77. } elseif ($updatesAvailableCount === 1) {
  78. $output->writeln('<comment>1 update available</comment>');
  79. } else {
  80. $output->writeln('<comment>' . $updatesAvailableCount . ' updates available</comment>');
  81. }
  82. return 0;
  83. }
  84. }