Update.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, michag86 (michag86@arcor.de)
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author michag86 <micha_g@arcor.de>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OC\Core\Command\App;
  30. use OC\Installer;
  31. use OCP\App\IAppManager;
  32. use Psr\Log\LoggerInterface;
  33. use Symfony\Component\Console\Command\Command;
  34. use Symfony\Component\Console\Input\InputArgument;
  35. use Symfony\Component\Console\Input\InputInterface;
  36. use Symfony\Component\Console\Input\InputOption;
  37. use Symfony\Component\Console\Output\OutputInterface;
  38. class Update extends Command {
  39. public function __construct(
  40. protected IAppManager $manager,
  41. private Installer $installer,
  42. private LoggerInterface $logger,
  43. ) {
  44. parent::__construct();
  45. }
  46. protected function configure(): void {
  47. $this
  48. ->setName('app:update')
  49. ->setDescription('update an app or all apps')
  50. ->addArgument(
  51. 'app-id',
  52. InputArgument::OPTIONAL,
  53. 'update the specified app'
  54. )
  55. ->addOption(
  56. 'all',
  57. null,
  58. InputOption::VALUE_NONE,
  59. 'update all updatable apps'
  60. )
  61. ->addOption(
  62. 'showonly',
  63. null,
  64. InputOption::VALUE_NONE,
  65. 'show update(s) without updating'
  66. )
  67. ->addOption(
  68. 'allow-unstable',
  69. null,
  70. InputOption::VALUE_NONE,
  71. 'allow updating to unstable releases'
  72. )
  73. ;
  74. }
  75. protected function execute(InputInterface $input, OutputInterface $output): int {
  76. $singleAppId = $input->getArgument('app-id');
  77. $updateFound = false;
  78. if ($singleAppId) {
  79. $apps = [$singleAppId];
  80. try {
  81. $this->manager->getAppPath($singleAppId);
  82. } catch (\OCP\App\AppPathNotFoundException $e) {
  83. $output->writeln($singleAppId . ' not installed');
  84. return 1;
  85. }
  86. } elseif ($input->getOption('all') || $input->getOption('showonly')) {
  87. $apps = \OC_App::getAllApps();
  88. } else {
  89. $output->writeln("<error>Please specify an app to update or \"--all\" to update all updatable apps\"</error>");
  90. return 1;
  91. }
  92. $return = 0;
  93. foreach ($apps as $appId) {
  94. $newVersion = $this->installer->isUpdateAvailable($appId, $input->getOption('allow-unstable'));
  95. if ($newVersion) {
  96. $updateFound = true;
  97. $output->writeln($appId . ' new version available: ' . $newVersion);
  98. if (!$input->getOption('showonly')) {
  99. try {
  100. $result = $this->installer->updateAppstoreApp($appId, $input->getOption('allow-unstable'));
  101. } catch (\Exception $e) {
  102. $this->logger->error('Failure during update of app "' . $appId . '"', [
  103. 'app' => 'app:update',
  104. 'exception' => $e,
  105. ]);
  106. $output->writeln('Error: ' . $e->getMessage());
  107. $result = false;
  108. $return = 1;
  109. }
  110. if ($result === false) {
  111. $output->writeln($appId . ' couldn\'t be updated');
  112. $return = 1;
  113. } else {
  114. $output->writeln($appId . ' updated');
  115. }
  116. }
  117. }
  118. }
  119. if (!$updateFound) {
  120. if ($singleAppId) {
  121. $output->writeln($singleAppId . ' is up-to-date or no updates could be found');
  122. } else {
  123. $output->writeln('All apps are up-to-date or no updates could be found');
  124. }
  125. }
  126. return $return;
  127. }
  128. }