GetPath.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Core\Command\App;
  25. use OC\Core\Command\Base;
  26. use OCP\App\AppPathNotFoundException;
  27. use OCP\App\IAppManager;
  28. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class GetPath extends Base {
  33. public function __construct(
  34. protected IAppManager $appManager,
  35. ) {
  36. parent::__construct();
  37. }
  38. protected function configure(): void {
  39. parent::configure();
  40. $this
  41. ->setName('app:getpath')
  42. ->setDescription('Get an absolute path to the app directory')
  43. ->addArgument(
  44. 'app',
  45. InputArgument::REQUIRED,
  46. 'Name of the app'
  47. )
  48. ;
  49. }
  50. /**
  51. * Executes the current command.
  52. *
  53. * @param InputInterface $input An InputInterface instance
  54. * @param OutputInterface $output An OutputInterface instance
  55. * @return int 0 if everything went fine, or an error code
  56. */
  57. protected function execute(InputInterface $input, OutputInterface $output): int {
  58. $appName = $input->getArgument('app');
  59. try {
  60. $path = $this->appManager->getAppPath($appName);
  61. } catch (AppPathNotFoundException) {
  62. // App not found, exit with non-zero
  63. return self::FAILURE;
  64. }
  65. $output->writeln($path);
  66. return self::SUCCESS;
  67. }
  68. /**
  69. * @param string $argumentName
  70. * @param CompletionContext $context
  71. * @return string[]
  72. */
  73. public function completeArgumentValues($argumentName, CompletionContext $context): array {
  74. if ($argumentName === 'app') {
  75. return \OC_App::getAllApps();
  76. }
  77. return [];
  78. }
  79. }