Update.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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) <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 OCP\ILogger;
  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. /** @var IAppManager */
  39. protected $manager;
  40. /** @var Installer */
  41. private $installer;
  42. /** @var ILogger */
  43. private $logger;
  44. /**
  45. * @param IAppManager $manager
  46. * @param Installer $installer
  47. */
  48. public function __construct(IAppManager $manager, Installer $installer, ILogger $logger) {
  49. parent::__construct();
  50. $this->manager = $manager;
  51. $this->installer = $installer;
  52. $this->logger = $logger;
  53. }
  54. protected function configure() {
  55. $this
  56. ->setName('app:update')
  57. ->setDescription('update an app or all apps')
  58. ->addArgument(
  59. 'app-id',
  60. InputArgument::OPTIONAL,
  61. 'update the specified app'
  62. )
  63. ->addOption(
  64. 'all',
  65. null,
  66. InputOption::VALUE_NONE,
  67. 'update all updatable apps'
  68. )
  69. ->addOption(
  70. 'showonly',
  71. null,
  72. InputOption::VALUE_NONE,
  73. 'show update(s) without updating'
  74. )
  75. ->addOption(
  76. 'allow-unstable',
  77. null,
  78. InputOption::VALUE_NONE,
  79. 'allow updating to unstable releases'
  80. )
  81. ;
  82. }
  83. protected function execute(InputInterface $input, OutputInterface $output): int {
  84. $singleAppId = $input->getArgument('app-id');
  85. if ($singleAppId) {
  86. $apps = [$singleAppId];
  87. try {
  88. $this->manager->getAppPath($singleAppId);
  89. } catch (\OCP\App\AppPathNotFoundException $e) {
  90. $output->writeln($singleAppId . ' not installed');
  91. return 1;
  92. }
  93. } elseif ($input->getOption('all') || $input->getOption('showonly')) {
  94. $apps = \OC_App::getAllApps();
  95. } else {
  96. $output->writeln("<error>Please specify an app to update or \"--all\" to update all updatable apps\"</error>");
  97. return 1;
  98. }
  99. $return = 0;
  100. foreach ($apps as $appId) {
  101. $newVersion = $this->installer->isUpdateAvailable($appId, $input->getOption('allow-unstable'));
  102. if ($newVersion) {
  103. $output->writeln($appId . ' new version available: ' . $newVersion);
  104. if (!$input->getOption('showonly')) {
  105. try {
  106. $result = $this->installer->updateAppstoreApp($appId, $input->getOption('allow-unstable'));
  107. } catch (\Exception $e) {
  108. $this->logger->logException($e, ['message' => 'Failure during update of app "' . $appId . '"','app' => 'app:update']);
  109. $output->writeln('Error: ' . $e->getMessage());
  110. $return = 1;
  111. }
  112. if ($result === false) {
  113. $output->writeln($appId . ' couldn\'t be updated');
  114. $return = 1;
  115. } elseif ($result === true) {
  116. $output->writeln($appId . ' updated');
  117. }
  118. }
  119. }
  120. }
  121. return $return;
  122. }
  123. }