ListConfigs.php 4.0 KB

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