GetConfig.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 OC\Core\Command\Base;
  24. use OCP\IConfig;
  25. use Symfony\Component\Console\Input\InputArgument;
  26. use Symfony\Component\Console\Input\InputInterface;
  27. use Symfony\Component\Console\Input\InputOption;
  28. use Symfony\Component\Console\Output\OutputInterface;
  29. class GetConfig extends Base {
  30. /** * @var IConfig */
  31. protected $config;
  32. /**
  33. * @param IConfig $config
  34. */
  35. public function __construct(IConfig $config) {
  36. parent::__construct();
  37. $this->config = $config;
  38. }
  39. protected function configure() {
  40. parent::configure();
  41. $this
  42. ->setName('config:app:get')
  43. ->setDescription('Get an app config value')
  44. ->addArgument(
  45. 'app',
  46. InputArgument::REQUIRED,
  47. 'Name of the app'
  48. )
  49. ->addArgument(
  50. 'name',
  51. InputArgument::REQUIRED,
  52. 'Name of the config to get'
  53. )
  54. ->addOption(
  55. 'default-value',
  56. null,
  57. InputOption::VALUE_OPTIONAL,
  58. 'If no default value is set and the config does not exist, the command will exit with 1'
  59. )
  60. ;
  61. }
  62. /**
  63. * Executes the current command.
  64. *
  65. * @param InputInterface $input An InputInterface instance
  66. * @param OutputInterface $output An OutputInterface instance
  67. * @return null|int null or 0 if everything went fine, or an error code
  68. */
  69. protected function execute(InputInterface $input, OutputInterface $output) {
  70. $appName = $input->getArgument('app');
  71. $configName = $input->getArgument('name');
  72. $defaultValue = $input->getOption('default-value');
  73. if (!in_array($configName, $this->config->getAppKeys($appName)) && !$input->hasParameterOption('--default-value')) {
  74. return 1;
  75. }
  76. if (!in_array($configName, $this->config->getAppKeys($appName))) {
  77. $configValue = $defaultValue;
  78. } else {
  79. $configValue = $this->config->getAppValue($appName, $configName);
  80. }
  81. $this->writeMixedInOutputFormat($input, $output, $configValue);
  82. return 0;
  83. }
  84. }