Update.php 3.9 KB

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