GetConfig.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Core\Command\Config\App;
  23. use OCP\IConfig;
  24. use Symfony\Component\Console\Input\InputArgument;
  25. use Symfony\Component\Console\Input\InputInterface;
  26. use Symfony\Component\Console\Input\InputOption;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. class GetConfig extends Base {
  29. protected IConfig $config;
  30. public function __construct(IConfig $config) {
  31. parent::__construct();
  32. $this->config = $config;
  33. }
  34. protected function configure() {
  35. parent::configure();
  36. $this
  37. ->setName('config:app:get')
  38. ->setDescription('Get an app config value')
  39. ->addArgument(
  40. 'app',
  41. InputArgument::REQUIRED,
  42. 'Name of the app'
  43. )
  44. ->addArgument(
  45. 'name',
  46. InputArgument::REQUIRED,
  47. 'Name of the config to get'
  48. )
  49. ->addOption(
  50. 'default-value',
  51. null,
  52. InputOption::VALUE_OPTIONAL,
  53. 'If no default value is set and the config does not exist, the command will exit with 1'
  54. )
  55. ;
  56. }
  57. /**
  58. * Executes the current command.
  59. *
  60. * @param InputInterface $input An InputInterface instance
  61. * @param OutputInterface $output An OutputInterface instance
  62. * @return int 0 if everything went fine, or an error code
  63. */
  64. protected function execute(InputInterface $input, OutputInterface $output): int {
  65. $appName = $input->getArgument('app');
  66. $configName = $input->getArgument('name');
  67. $defaultValue = $input->getOption('default-value');
  68. if (!in_array($configName, $this->config->getAppKeys($appName)) && !$input->hasParameterOption('--default-value')) {
  69. return 1;
  70. }
  71. if (!in_array($configName, $this->config->getAppKeys($appName))) {
  72. $configValue = $defaultValue;
  73. } else {
  74. $configValue = $this->config->getAppValue($appName, $configName);
  75. }
  76. $this->writeMixedInOutputFormat($input, $output, $configValue);
  77. return 0;
  78. }
  79. }