GetConfig.php 2.8 KB

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