GetConfig.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Maxence Lange <maxence@artificial-owl.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\Config\App;
  25. use OCP\Exceptions\AppConfigUnknownKeyException;
  26. use OCP\IAppConfig;
  27. use Symfony\Component\Console\Input\InputArgument;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Input\InputOption;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class GetConfig extends Base {
  32. public function __construct(
  33. protected IAppConfig $appConfig,
  34. ) {
  35. parent::__construct();
  36. }
  37. protected function configure() {
  38. parent::configure();
  39. $this
  40. ->setName('config:app:get')
  41. ->setDescription('Get an app config value')
  42. ->addArgument(
  43. 'app',
  44. InputArgument::REQUIRED,
  45. 'Name of the app'
  46. )
  47. ->addArgument(
  48. 'name',
  49. InputArgument::REQUIRED,
  50. 'Name of the config to get'
  51. )
  52. ->addOption(
  53. 'details',
  54. null,
  55. InputOption::VALUE_NONE,
  56. 'returns complete details about the app config value'
  57. )
  58. ->addOption(
  59. 'default-value',
  60. null,
  61. InputOption::VALUE_OPTIONAL,
  62. 'If no default value is set and the config does not exist, the command will exit with 1'
  63. )
  64. ;
  65. }
  66. /**
  67. * Executes the current command.
  68. *
  69. * @param InputInterface $input An InputInterface instance
  70. * @param OutputInterface $output An OutputInterface instance
  71. * @return int 0 if everything went fine, or an error code
  72. */
  73. protected function execute(InputInterface $input, OutputInterface $output): int {
  74. $appName = $input->getArgument('app');
  75. $configName = $input->getArgument('name');
  76. $defaultValue = $input->getOption('default-value');
  77. if ($input->getOption('details')) {
  78. $details = $this->appConfig->getDetails($appName, $configName);
  79. $details['type'] = $details['typeString'];
  80. unset($details['typeString']);
  81. $this->writeArrayInOutputFormat($input, $output, $details);
  82. return 0;
  83. }
  84. try {
  85. $configValue = $this->appConfig->getDetails($appName, $configName)['value'];
  86. } catch (AppConfigUnknownKeyException $e) {
  87. if (!$input->hasParameterOption('--default-value')) {
  88. return 1;
  89. }
  90. $configValue = $defaultValue;
  91. }
  92. $this->writeMixedInOutputFormat($input, $output, $configValue);
  93. return 0;
  94. }
  95. }