ListConfigs.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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;
  24. use OC\Core\Command\Base;
  25. use OC\SystemConfig;
  26. use OCP\IAppConfig;
  27. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  28. use Symfony\Component\Console\Input\InputArgument;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Input\InputOption;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class ListConfigs extends Base {
  33. protected string $defaultOutputFormat = self::OUTPUT_FORMAT_JSON_PRETTY;
  34. public function __construct(
  35. protected SystemConfig $systemConfig,
  36. protected IAppConfig $appConfig,
  37. ) {
  38. parent::__construct();
  39. }
  40. protected function configure() {
  41. parent::configure();
  42. $this
  43. ->setName('config:list')
  44. ->setDescription('List all configs')
  45. ->addArgument(
  46. 'app',
  47. InputArgument::OPTIONAL,
  48. 'Name of the app ("system" to get the config.php values, "all" for all apps and system)',
  49. 'all'
  50. )
  51. ->addOption(
  52. 'private',
  53. null,
  54. InputOption::VALUE_NONE,
  55. 'Use this option when you want to include sensitive configs like passwords, salts, ...'
  56. )
  57. ;
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output): int {
  60. $app = $input->getArgument('app');
  61. $noSensitiveValues = !$input->getOption('private');
  62. if (!is_string($app)) {
  63. $output->writeln('<error>Invalid app value given</error>');
  64. return 1;
  65. }
  66. switch ($app) {
  67. case 'system':
  68. $configs = [
  69. 'system' => $this->getSystemConfigs($noSensitiveValues),
  70. ];
  71. break;
  72. case 'all':
  73. $apps = $this->appConfig->getApps();
  74. $configs = [
  75. 'system' => $this->getSystemConfigs($noSensitiveValues),
  76. 'apps' => [],
  77. ];
  78. foreach ($apps as $appName) {
  79. $configs['apps'][$appName] = $this->getAppConfigs($appName, $noSensitiveValues);
  80. }
  81. break;
  82. default:
  83. $configs = [
  84. 'apps' => [$app => $this->getAppConfigs($app, $noSensitiveValues)],
  85. ];
  86. }
  87. $this->writeArrayInOutputFormat($input, $output, $configs);
  88. return 0;
  89. }
  90. /**
  91. * Get the system configs
  92. *
  93. * @param bool $noSensitiveValues
  94. * @return array
  95. */
  96. protected function getSystemConfigs(bool $noSensitiveValues): array {
  97. $keys = $this->systemConfig->getKeys();
  98. $configs = [];
  99. foreach ($keys as $key) {
  100. if ($noSensitiveValues) {
  101. $value = $this->systemConfig->getFilteredValue($key, serialize(null));
  102. } else {
  103. $value = $this->systemConfig->getValue($key, serialize(null));
  104. }
  105. if ($value !== 'N;') {
  106. $configs[$key] = $value;
  107. }
  108. }
  109. return $configs;
  110. }
  111. /**
  112. * Get the app configs
  113. *
  114. * @param string $app
  115. * @param bool $noSensitiveValues
  116. * @return array
  117. */
  118. protected function getAppConfigs(string $app, bool $noSensitiveValues) {
  119. if ($noSensitiveValues) {
  120. return $this->appConfig->getFilteredValues($app, false);
  121. } else {
  122. return $this->appConfig->getValues($app, false);
  123. }
  124. }
  125. /**
  126. * @param string $argumentName
  127. * @param CompletionContext $context
  128. * @return string[]
  129. */
  130. public function completeArgumentValues($argumentName, CompletionContext $context) {
  131. if ($argumentName === 'app') {
  132. return array_merge(['all', 'system'], \OC_App::getAllApps());
  133. }
  134. return [];
  135. }
  136. }