Update.php 3.5 KB

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