Update.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018, michag86 (michag86@arcor.de)
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\Core\Command\App;
  22. use OCP\App\IAppManager;
  23. use OC\Installer;
  24. use OCP\ILogger;
  25. use Symfony\Component\Console\Command\Command;
  26. use Symfony\Component\Console\Input\InputArgument;
  27. use Symfony\Component\Console\Input\InputOption;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. class Update extends Command {
  31. /** @var IAppManager */
  32. protected $manager;
  33. /** @var Installer */
  34. private $installer;
  35. /** @var ILogger */
  36. private $logger;
  37. /**
  38. * @param IAppManager $manager
  39. * @param Installer $installer
  40. */
  41. public function __construct(IAppManager $manager, Installer $installer, ILogger $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. ;
  69. }
  70. protected function execute(InputInterface $input, OutputInterface $output) {
  71. $singleAppId = $input->getArgument('app-id');
  72. if ($singleAppId) {
  73. $apps = array($singleAppId);
  74. try {
  75. $this->manager->getAppPath($singleAppId);
  76. } catch (\OCP\App\AppPathNotFoundException $e) {
  77. $output->writeln($singleAppId . ' not installed');
  78. return 1;
  79. }
  80. } else if ($input->getOption('all') || $input->getOption('showonly')) {
  81. $apps = \OC_App::getAllApps();
  82. } else {
  83. $output->writeln("<error>Please specify an app to update or \"--all\" to update all updatable apps\"</error>");
  84. return 1;
  85. }
  86. $return = 0;
  87. foreach ($apps as $appId) {
  88. $newVersion = $this->installer->isUpdateAvailable($appId);
  89. if ($newVersion) {
  90. $output->writeln($appId . ' new version available: ' . $newVersion);
  91. if (!$input->getOption('showonly')) {
  92. try {
  93. $result = $this->installer->updateAppstoreApp($appId);
  94. } catch(\Exception $e) {
  95. $this->logger->logException($e, ['message' => 'Failure during update of app "' . $appId . '"','app' => 'app:update']);
  96. $output->writeln('Error: ' . $e->getMessage());
  97. $return = 1;
  98. }
  99. if ($result === false) {
  100. $output->writeln($appId . ' couldn\'t be updated');
  101. $return = 1;
  102. } else if($result === true) {
  103. $output->writeln($appId . ' updated');
  104. }
  105. }
  106. }
  107. }
  108. return $return;
  109. }
  110. }