ListConfigs.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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' => [
  85. $app => $this->getAppConfigs($app, $noSensitiveValues),
  86. ],
  87. ];
  88. }
  89. $this->writeArrayInOutputFormat($input, $output, $configs);
  90. return 0;
  91. }
  92. /**
  93. * Get the system configs
  94. *
  95. * @param bool $noSensitiveValues
  96. * @return array
  97. */
  98. protected function getSystemConfigs($noSensitiveValues) {
  99. $keys = $this->systemConfig->getKeys();
  100. $configs = [];
  101. foreach ($keys as $key) {
  102. if ($noSensitiveValues) {
  103. $value = $this->systemConfig->getFilteredValue($key, serialize(null));
  104. } else {
  105. $value = $this->systemConfig->getValue($key, serialize(null));
  106. }
  107. if ($value !== 'N;') {
  108. $configs[$key] = $value;
  109. }
  110. }
  111. return $configs;
  112. }
  113. /**
  114. * Get the app configs
  115. *
  116. * @param string $app
  117. * @param bool $noSensitiveValues
  118. * @return array
  119. */
  120. protected function getAppConfigs($app, $noSensitiveValues) {
  121. if ($noSensitiveValues) {
  122. return $this->appConfig->getFilteredValues($app, false);
  123. } else {
  124. return $this->appConfig->getValues($app, false);
  125. }
  126. }
  127. /**
  128. * @param string $argumentName
  129. * @param CompletionContext $context
  130. * @return string[]
  131. */
  132. public function completeArgumentValues($argumentName, CompletionContext $context) {
  133. if ($argumentName === 'app') {
  134. return array_merge(['all', 'system'], \OC_App::getAllApps());
  135. }
  136. return [];
  137. }
  138. }