Update.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. protected IAppManager $manager;
  39. private Installer $installer;
  40. private LoggerInterface $logger;
  41. public function __construct(IAppManager $manager, Installer $installer, LoggerInterface $logger) {
  42. parent::__construct();
  43. $this->manager = $manager;
  44. $this->installer = $installer;
  45. $this->logger = $logger;
  46. }
  47. protected function configure() {
  48. $this
  49. ->setName('app:update')
  50. ->setDescription('update an app or all apps')
  51. ->addArgument(
  52. 'app-id',
  53. InputArgument::OPTIONAL,
  54. 'update the specified app'
  55. )
  56. ->addOption(
  57. 'all',
  58. null,
  59. InputOption::VALUE_NONE,
  60. 'update all updatable apps'
  61. )
  62. ->addOption(
  63. 'showonly',
  64. null,
  65. InputOption::VALUE_NONE,
  66. 'show update(s) without updating'
  67. )
  68. ->addOption(
  69. 'allow-unstable',
  70. null,
  71. InputOption::VALUE_NONE,
  72. 'allow updating to unstable releases'
  73. )
  74. ;
  75. }
  76. protected function execute(InputInterface $input, OutputInterface $output): int {
  77. $singleAppId = $input->getArgument('app-id');
  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. $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. $return = 1;
  107. }
  108. if ($result === false) {
  109. $output->writeln($appId . ' couldn\'t be updated');
  110. $return = 1;
  111. } elseif ($result === true) {
  112. $output->writeln($appId . ' updated');
  113. }
  114. }
  115. }
  116. }
  117. return $return;
  118. }
  119. }