1
0

GetConfig.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Core\Command\Config\App;
  9. use OCP\Exceptions\AppConfigUnknownKeyException;
  10. use OCP\IAppConfig;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class GetConfig extends Base {
  16. public function __construct(
  17. protected IAppConfig $appConfig,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure() {
  22. parent::configure();
  23. $this
  24. ->setName('config:app:get')
  25. ->setDescription('Get an app config value')
  26. ->addArgument(
  27. 'app',
  28. InputArgument::REQUIRED,
  29. 'Name of the app'
  30. )
  31. ->addArgument(
  32. 'name',
  33. InputArgument::REQUIRED,
  34. 'Name of the config to get'
  35. )
  36. ->addOption(
  37. 'details',
  38. null,
  39. InputOption::VALUE_NONE,
  40. 'returns complete details about the app config value'
  41. )
  42. ->addOption(
  43. 'default-value',
  44. null,
  45. InputOption::VALUE_OPTIONAL,
  46. 'If no default value is set and the config does not exist, the command will exit with 1'
  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. $configName = $input->getArgument('name');
  60. $defaultValue = $input->getOption('default-value');
  61. if ($input->getOption('details')) {
  62. $details = $this->appConfig->getDetails($appName, $configName);
  63. $details['type'] = $details['typeString'];
  64. unset($details['typeString']);
  65. $this->writeArrayInOutputFormat($input, $output, $details);
  66. return 0;
  67. }
  68. try {
  69. $configValue = $this->appConfig->getDetails($appName, $configName)['value'];
  70. } catch (AppConfigUnknownKeyException $e) {
  71. if (!$input->hasParameterOption('--default-value')) {
  72. return 1;
  73. }
  74. $configValue = $defaultValue;
  75. }
  76. $this->writeMixedInOutputFormat($input, $output, $configValue);
  77. return 0;
  78. }
  79. }