Update.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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() {
  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. if ($singleAppId) {
  77. $apps = [$singleAppId];
  78. try {
  79. $this->manager->getAppPath($singleAppId);
  80. } catch (\OCP\App\AppPathNotFoundException $e) {
  81. $output->writeln($singleAppId . ' not installed');
  82. return 1;
  83. }
  84. } elseif ($input->getOption('all') || $input->getOption('showonly')) {
  85. $apps = \OC_App::getAllApps();
  86. } else {
  87. $output->writeln("<error>Please specify an app to update or \"--all\" to update all updatable apps\"</error>");
  88. return 1;
  89. }
  90. $return = 0;
  91. foreach ($apps as $appId) {
  92. $newVersion = $this->installer->isUpdateAvailable($appId, $input->getOption('allow-unstable'));
  93. if ($newVersion) {
  94. $output->writeln($appId . ' new version available: ' . $newVersion);
  95. if (!$input->getOption('showonly')) {
  96. try {
  97. $result = $this->installer->updateAppstoreApp($appId, $input->getOption('allow-unstable'));
  98. } catch (\Exception $e) {
  99. $this->logger->error('Failure during update of app "' . $appId . '"', [
  100. 'app' => 'app:update',
  101. 'exception' => $e,
  102. ]);
  103. $output->writeln('Error: ' . $e->getMessage());
  104. $return = 1;
  105. }
  106. if ($result === false) {
  107. $output->writeln($appId . ' couldn\'t be updated');
  108. $return = 1;
  109. } elseif ($result === true) {
  110. $output->writeln($appId . ' updated');
  111. }
  112. }
  113. }
  114. }
  115. return $return;
  116. }
  117. }