ListConfigs.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\Config;
  8. use OC\Core\Command\Base;
  9. use OC\SystemConfig;
  10. use OCP\IAppConfig;
  11. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class ListConfigs extends Base {
  17. protected string $defaultOutputFormat = self::OUTPUT_FORMAT_JSON_PRETTY;
  18. public function __construct(
  19. protected SystemConfig $systemConfig,
  20. protected IAppConfig $appConfig,
  21. ) {
  22. parent::__construct();
  23. }
  24. protected function configure() {
  25. parent::configure();
  26. $this
  27. ->setName('config:list')
  28. ->setDescription('List all configs')
  29. ->addArgument(
  30. 'app',
  31. InputArgument::OPTIONAL,
  32. 'Name of the app ("system" to get the config.php values, "all" for all apps and system)',
  33. 'all'
  34. )
  35. ->addOption(
  36. 'private',
  37. null,
  38. InputOption::VALUE_NONE,
  39. 'Use this option when you want to include sensitive configs like passwords, salts, ...'
  40. )
  41. ;
  42. }
  43. protected function execute(InputInterface $input, OutputInterface $output): int {
  44. $app = $input->getArgument('app');
  45. $noSensitiveValues = !$input->getOption('private');
  46. if (!is_string($app)) {
  47. $output->writeln('<error>Invalid app value given</error>');
  48. return 1;
  49. }
  50. switch ($app) {
  51. case 'system':
  52. $configs = [
  53. 'system' => $this->getSystemConfigs($noSensitiveValues),
  54. ];
  55. break;
  56. case 'all':
  57. $apps = $this->appConfig->getApps();
  58. $configs = [
  59. 'system' => $this->getSystemConfigs($noSensitiveValues),
  60. 'apps' => [],
  61. ];
  62. foreach ($apps as $appName) {
  63. $configs['apps'][$appName] = $this->getAppConfigs($appName, $noSensitiveValues);
  64. }
  65. break;
  66. default:
  67. $configs = [
  68. 'apps' => [$app => $this->getAppConfigs($app, $noSensitiveValues)],
  69. ];
  70. }
  71. $this->writeArrayInOutputFormat($input, $output, $configs);
  72. return 0;
  73. }
  74. /**
  75. * Get the system configs
  76. *
  77. * @param bool $noSensitiveValues
  78. * @return array
  79. */
  80. protected function getSystemConfigs(bool $noSensitiveValues): array {
  81. $keys = $this->systemConfig->getKeys();
  82. $configs = [];
  83. foreach ($keys as $key) {
  84. if ($noSensitiveValues) {
  85. $value = $this->systemConfig->getFilteredValue($key, serialize(null));
  86. } else {
  87. $value = $this->systemConfig->getValue($key, serialize(null));
  88. }
  89. if ($value !== 'N;') {
  90. $configs[$key] = $value;
  91. }
  92. }
  93. return $configs;
  94. }
  95. /**
  96. * Get the app configs
  97. *
  98. * @param string $app
  99. * @param bool $noSensitiveValues
  100. * @return array
  101. */
  102. protected function getAppConfigs(string $app, bool $noSensitiveValues) {
  103. if ($noSensitiveValues) {
  104. return $this->appConfig->getFilteredValues($app, false);
  105. } else {
  106. return $this->appConfig->getValues($app, false);
  107. }
  108. }
  109. /**
  110. * @param string $argumentName
  111. * @param CompletionContext $context
  112. * @return string[]
  113. */
  114. public function completeArgumentValues($argumentName, CompletionContext $context) {
  115. if ($argumentName === 'app') {
  116. return array_merge(['all', 'system'], \OC_App::getAllApps());
  117. }
  118. return [];
  119. }
  120. }