Update.php 3.7 KB

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