Update.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 OC\Core\Command\App;
  8. use OC\Installer;
  9. use OCP\App\IAppManager;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class Update extends Command {
  17. public function __construct(
  18. protected IAppManager $manager,
  19. private Installer $installer,
  20. private LoggerInterface $logger,
  21. ) {
  22. parent::__construct();
  23. }
  24. protected function configure(): void {
  25. $this
  26. ->setName('app:update')
  27. ->setDescription('update an app or all apps')
  28. ->addArgument(
  29. 'app-id',
  30. InputArgument::OPTIONAL,
  31. 'update the specified app'
  32. )
  33. ->addOption(
  34. 'all',
  35. null,
  36. InputOption::VALUE_NONE,
  37. 'update all updatable apps'
  38. )
  39. ->addOption(
  40. 'showonly',
  41. null,
  42. InputOption::VALUE_NONE,
  43. 'show update(s) without updating'
  44. )
  45. ->addOption(
  46. 'allow-unstable',
  47. null,
  48. InputOption::VALUE_NONE,
  49. 'allow updating to unstable releases'
  50. )
  51. ;
  52. }
  53. protected function execute(InputInterface $input, OutputInterface $output): int {
  54. $singleAppId = $input->getArgument('app-id');
  55. $updateFound = false;
  56. if ($singleAppId) {
  57. $apps = [$singleAppId];
  58. try {
  59. $this->manager->getAppPath($singleAppId);
  60. } catch (\OCP\App\AppPathNotFoundException $e) {
  61. $output->writeln($singleAppId . ' not installed');
  62. return 1;
  63. }
  64. } elseif ($input->getOption('all') || $input->getOption('showonly')) {
  65. $apps = $this->manager->getAllAppsInAppsFolders();
  66. } else {
  67. $output->writeln('<error>Please specify an app to update or "--all" to update all updatable apps"</error>');
  68. return 1;
  69. }
  70. $return = 0;
  71. foreach ($apps as $appId) {
  72. $newVersion = $this->installer->isUpdateAvailable($appId, $input->getOption('allow-unstable'));
  73. if ($newVersion) {
  74. $updateFound = true;
  75. $output->writeln($appId . ' new version available: ' . $newVersion);
  76. if (!$input->getOption('showonly')) {
  77. try {
  78. $result = $this->installer->updateAppstoreApp($appId, $input->getOption('allow-unstable'));
  79. } catch (\Exception $e) {
  80. $this->logger->error('Failure during update of app "' . $appId . '"', [
  81. 'app' => 'app:update',
  82. 'exception' => $e,
  83. ]);
  84. $output->writeln('Error: ' . $e->getMessage());
  85. $result = false;
  86. $return = 1;
  87. }
  88. if ($result === false) {
  89. $output->writeln($appId . ' couldn\'t be updated');
  90. $return = 1;
  91. } else {
  92. $output->writeln($appId . ' updated');
  93. }
  94. }
  95. }
  96. }
  97. if (!$updateFound) {
  98. if ($singleAppId) {
  99. $output->writeln($singleAppId . ' is up-to-date or no updates could be found');
  100. } else {
  101. $output->writeln('All apps are up-to-date or no updates could be found');
  102. }
  103. }
  104. return $return;
  105. }
  106. }