diff options
Diffstat (limited to 'core/Command/Config')
-rw-r--r-- | core/Command/Config/App/Base.php | 38 | ||||
-rw-r--r-- | core/Command/Config/App/DeleteConfig.php | 55 | ||||
-rw-r--r-- | core/Command/Config/App/GetConfig.php | 93 | ||||
-rw-r--r-- | core/Command/Config/App/SetConfig.php | 238 | ||||
-rw-r--r-- | core/Command/Config/Import.php | 206 | ||||
-rw-r--r-- | core/Command/Config/ListConfigs.php | 145 | ||||
-rw-r--r-- | core/Command/Config/Preset.php | 69 | ||||
-rw-r--r-- | core/Command/Config/System/Base.php | 65 | ||||
-rw-r--r-- | core/Command/Config/System/CastHelper.php | 76 | ||||
-rw-r--r-- | core/Command/Config/System/DeleteConfig.php | 96 | ||||
-rw-r--r-- | core/Command/Config/System/GetConfig.php | 80 | ||||
-rw-r--r-- | core/Command/Config/System/SetConfig.php | 124 |
12 files changed, 1285 insertions, 0 deletions
diff --git a/core/Command/Config/App/Base.php b/core/Command/Config/App/Base.php new file mode 100644 index 00000000000..e90a8e78f5b --- /dev/null +++ b/core/Command/Config/App/Base.php @@ -0,0 +1,38 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Core\Command\Config\App; + +use OC\Config\ConfigManager; +use OCP\IAppConfig; +use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; + +abstract class Base extends \OC\Core\Command\Base { + public function __construct( + protected IAppConfig $appConfig, + protected readonly ConfigManager $configManager, + ) { + parent::__construct(); + } + + /** + * @param string $argumentName + * @param CompletionContext $context + * @return string[] + */ + public function completeArgumentValues($argumentName, CompletionContext $context) { + if ($argumentName === 'app') { + return $this->appConfig->getApps(); + } + + if ($argumentName === 'name') { + $appName = $context->getWordAtIndex($context->getWordIndex() - 1); + return $this->appConfig->getKeys($appName); + } + return []; + } +} diff --git a/core/Command/Config/App/DeleteConfig.php b/core/Command/Config/App/DeleteConfig.php new file mode 100644 index 00000000000..5a08ecbdc42 --- /dev/null +++ b/core/Command/Config/App/DeleteConfig.php @@ -0,0 +1,55 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Core\Command\Config\App; + +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class DeleteConfig extends Base { + protected function configure() { + parent::configure(); + + $this + ->setName('config:app:delete') + ->setDescription('Delete an app config value') + ->addArgument( + 'app', + InputArgument::REQUIRED, + 'Name of the app' + ) + ->addArgument( + 'name', + InputArgument::REQUIRED, + 'Name of the config to delete' + ) + ->addOption( + 'error-if-not-exists', + null, + InputOption::VALUE_NONE, + 'Checks whether the config exists before deleting it' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $appName = $input->getArgument('app'); + $configName = $input->getArgument('name'); + + if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->appConfig->getKeys($appName), true)) { + $output->writeln('<error>Config ' . $configName . ' of app ' . $appName . ' could not be deleted because it did not exist</error>'); + return 1; + } + + $this->appConfig->deleteKey($appName, $configName); + $output->writeln('<info>Config value ' . $configName . ' of app ' . $appName . ' deleted</info>'); + return 0; + } +} diff --git a/core/Command/Config/App/GetConfig.php b/core/Command/Config/App/GetConfig.php new file mode 100644 index 00000000000..af0c5648232 --- /dev/null +++ b/core/Command/Config/App/GetConfig.php @@ -0,0 +1,93 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Core\Command\Config\App; + +use OCP\Exceptions\AppConfigUnknownKeyException; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class GetConfig extends Base { + protected function configure() { + parent::configure(); + + $this + ->setName('config:app:get') + ->setDescription('Get an app config value') + ->addArgument( + 'app', + InputArgument::REQUIRED, + 'Name of the app' + ) + ->addArgument( + 'name', + InputArgument::REQUIRED, + 'Name of the config to get' + ) + ->addOption( + 'details', + null, + InputOption::VALUE_NONE, + 'returns complete details about the app config value' + ) + ->addOption( + '--key-details', + null, + InputOption::VALUE_NONE, + 'returns complete details about the app config key' + ) + ->addOption( + 'default-value', + null, + InputOption::VALUE_OPTIONAL, + 'If no default value is set and the config does not exist, the command will exit with 1' + ) + ; + } + + /** + * Executes the current command. + * + * @param InputInterface $input An InputInterface instance + * @param OutputInterface $output An OutputInterface instance + * @return int 0 if everything went fine, or an error code + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $appName = $input->getArgument('app'); + $configName = $input->getArgument('name'); + $defaultValue = $input->getOption('default-value'); + + if ($input->getOption('details')) { + $details = $this->appConfig->getDetails($appName, $configName); + $details['type'] = $details['typeString']; + unset($details['typeString']); + $this->writeArrayInOutputFormat($input, $output, $details); + return 0; + } + + if ($input->getOption('key-details')) { + $details = $this->appConfig->getKeyDetails($appName, $configName); + $this->writeArrayInOutputFormat($input, $output, $details); + return 0; + } + + try { + $configValue = $this->appConfig->getDetails($appName, $configName)['value']; + } catch (AppConfigUnknownKeyException $e) { + if (!$input->hasParameterOption('--default-value')) { + return 1; + } + $configValue = $defaultValue; + } + + $this->writeMixedInOutputFormat($input, $output, $configValue); + return 0; + } +} diff --git a/core/Command/Config/App/SetConfig.php b/core/Command/Config/App/SetConfig.php new file mode 100644 index 00000000000..c818404fc0e --- /dev/null +++ b/core/Command/Config/App/SetConfig.php @@ -0,0 +1,238 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Core\Command\Config\App; + +use OC\AppConfig; +use OCP\Exceptions\AppConfigUnknownKeyException; +use OCP\IAppConfig; +use Symfony\Component\Console\Helper\QuestionHelper; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\Question; + +class SetConfig extends Base { + protected function configure() { + parent::configure(); + + $this + ->setName('config:app:set') + ->setDescription('Set an app config value') + ->addArgument( + 'app', + InputArgument::REQUIRED, + 'Name of the app' + ) + ->addArgument( + 'name', + InputArgument::REQUIRED, + 'Name of the config to set' + ) + ->addOption( + 'value', + null, + InputOption::VALUE_REQUIRED, + 'The new value of the config' + ) + ->addOption( + 'type', + null, + InputOption::VALUE_REQUIRED, + 'Value type [string, integer, float, boolean, array]', + 'string' + ) + ->addOption( + 'lazy', + null, + InputOption::VALUE_NEGATABLE, + 'Set value as lazy loaded', + ) + ->addOption( + 'sensitive', + null, + InputOption::VALUE_NEGATABLE, + 'Set value as sensitive', + ) + ->addOption( + 'update-only', + null, + InputOption::VALUE_NONE, + 'Only updates the value, if it is not set before, it is not being added' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $appName = $input->getArgument('app'); + $configName = $input->getArgument('name'); + + if (!($this->appConfig instanceof AppConfig)) { + throw new \Exception('Only compatible with OC\AppConfig as it uses internal methods'); + } + + if ($input->hasParameterOption('--update-only') && !$this->appConfig->hasKey($appName, $configName)) { + $output->writeln( + '<comment>Config value ' . $configName . ' for app ' . $appName + . ' not updated, as it has not been set before.</comment>' + ); + + return 1; + } + + $type = $typeString = null; + if ($input->hasParameterOption('--type')) { + $typeString = $input->getOption('type'); + $type = $this->appConfig->convertTypeToInt($typeString); + } + + /** + * If --Value is not specified, returns an exception if no value exists in database + * compare with current status in database and displays a reminder that this can break things. + * confirmation is required by admin, unless --no-interaction + */ + $updated = false; + if (!$input->hasParameterOption('--value')) { + if (!$input->getOption('lazy') && $this->appConfig->isLazy($appName, $configName) && $this->ask($input, $output, 'NOT LAZY')) { + $updated = $this->appConfig->updateLazy($appName, $configName, false); + } + if ($input->getOption('lazy') && !$this->appConfig->isLazy($appName, $configName) && $this->ask($input, $output, 'LAZY')) { + $updated = $this->appConfig->updateLazy($appName, $configName, true) || $updated; + } + if (!$input->getOption('sensitive') && $this->appConfig->isSensitive($appName, $configName) && $this->ask($input, $output, 'NOT SENSITIVE')) { + $updated = $this->appConfig->updateSensitive($appName, $configName, false) || $updated; + } + if ($input->getOption('sensitive') && !$this->appConfig->isSensitive($appName, $configName) && $this->ask($input, $output, 'SENSITIVE')) { + $updated = $this->appConfig->updateSensitive($appName, $configName, true) || $updated; + } + if ($type !== null && $type !== $this->appConfig->getValueType($appName, $configName) && $typeString !== null && $this->ask($input, $output, $typeString)) { + $updated = $this->appConfig->updateType($appName, $configName, $type) || $updated; + } + } else { + /** + * If --type is specified in the command line, we upgrade the type in database + * after a confirmation from admin. + * If not we get the type from current stored value or VALUE_MIXED as default. + */ + try { + $currType = $this->appConfig->getValueType($appName, $configName); + if ($type === null || $typeString === null || $type === $currType || !$this->ask($input, $output, $typeString)) { + $type = $currType; + } else { + $updated = $this->appConfig->updateType($appName, $configName, $type); + } + } catch (AppConfigUnknownKeyException) { + $type = $type ?? IAppConfig::VALUE_MIXED; + } + + /** + * if --lazy/--no-lazy option are set, compare with data stored in database. + * If no data in database, or identical, continue. + * If different, ask admin for confirmation. + */ + $lazy = $input->getOption('lazy'); + try { + $currLazy = $this->appConfig->isLazy($appName, $configName); + if ($lazy === null || $lazy === $currLazy || !$this->ask($input, $output, ($lazy) ? 'LAZY' : 'NOT LAZY')) { + $lazy = $currLazy; + } + } catch (AppConfigUnknownKeyException) { + $lazy = $lazy ?? false; + } + + /** + * same with sensitive status + */ + $sensitive = $input->getOption('sensitive'); + try { + $currSensitive = $this->appConfig->isSensitive($appName, $configName, null); + if ($sensitive === null || $sensitive === $currSensitive || !$this->ask($input, $output, ($sensitive) ? 'SENSITIVE' : 'NOT SENSITIVE')) { + $sensitive = $currSensitive; + } + } catch (AppConfigUnknownKeyException) { + $sensitive = $sensitive ?? false; + } + + $value = (string)$input->getOption('value'); + switch ($type) { + case IAppConfig::VALUE_MIXED: + $updated = $this->appConfig->setValueMixed($appName, $configName, $value, $lazy, $sensitive); + break; + + case IAppConfig::VALUE_STRING: + $updated = $this->appConfig->setValueString($appName, $configName, $value, $lazy, $sensitive); + break; + + case IAppConfig::VALUE_INT: + $updated = $this->appConfig->setValueInt($appName, $configName, $this->configManager->convertToInt($value), $lazy, $sensitive); + break; + + case IAppConfig::VALUE_FLOAT: + $updated = $this->appConfig->setValueFloat($appName, $configName, $this->configManager->convertToFloat($value), $lazy, $sensitive); + break; + + case IAppConfig::VALUE_BOOL: + $updated = $this->appConfig->setValueBool($appName, $configName, $this->configManager->convertToBool($value), $lazy); + break; + + case IAppConfig::VALUE_ARRAY: + $updated = $this->appConfig->setValueArray($appName, $configName, $this->configManager->convertToArray($value), $lazy, $sensitive); + break; + } + } + + if ($updated) { + $current = $this->appConfig->getDetails($appName, $configName); + $output->writeln( + sprintf( + "<info>Config value '%s' for app '%s' is now set to '%s', stored as %s in %s</info>", + $configName, + $appName, + $current['sensitive'] ? '<sensitive>' : $current['value'], + $current['typeString'], + $current['lazy'] ? 'lazy cache' : 'fast cache' + ) + ); + $keyDetails = $this->appConfig->getKeyDetails($appName, $configName); + if (($keyDetails['note'] ?? '') !== '') { + $output->writeln('<comment>Note:</comment> ' . $keyDetails['note']); + } + + } else { + $output->writeln('<info>Config value were not updated</info>'); + } + + return 0; + } + + private function ask(InputInterface $input, OutputInterface $output, string $request): bool { + /** @var QuestionHelper $helper */ + $helper = $this->getHelper('question'); + if ($input->getOption('no-interaction')) { + return true; + } + + $output->writeln(sprintf('You are about to set config value %s as <info>%s</info>', + '<info>' . $input->getArgument('app') . '</info>/<info>' . $input->getArgument('name') . '</info>', + strtoupper($request) + )); + $output->writeln(''); + $output->writeln('<comment>This might break thing, affect performance on your instance or its security!</comment>'); + + $result = (strtolower((string)$helper->ask( + $input, + $output, + new Question('<comment>Confirm this action by typing \'yes\'</comment>: '))) === 'yes'); + + $output->writeln(($result) ? 'done' : 'cancelled'); + $output->writeln(''); + + return $result; + } +} diff --git a/core/Command/Config/Import.php b/core/Command/Config/Import.php new file mode 100644 index 00000000000..b58abec3390 --- /dev/null +++ b/core/Command/Config/Import.php @@ -0,0 +1,206 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Core\Command\Config; + +use OCP\IConfig; +use Stecman\Component\Symfony\Console\BashCompletion\Completion; +use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface; +use Stecman\Component\Symfony\Console\BashCompletion\Completion\ShellPathCompletion; +use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class Import extends Command implements CompletionAwareInterface { + protected array $validRootKeys = ['system', 'apps']; + + public function __construct( + protected IConfig $config, + ) { + parent::__construct(); + } + + protected function configure() { + $this + ->setName('config:import') + ->setDescription('Import a list of configs') + ->addArgument( + 'file', + InputArgument::OPTIONAL, + 'File with the json array to import' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $importFile = $input->getArgument('file'); + if ($importFile !== null) { + $content = $this->getArrayFromFile($importFile); + } else { + $content = $this->getArrayFromStdin(); + } + + try { + $configs = $this->validateFileContent($content); + } catch (\UnexpectedValueException $e) { + $output->writeln('<error>' . $e->getMessage() . '</error>'); + return 1; + } + + if (!empty($configs['system'])) { + $this->config->setSystemValues($configs['system']); + } + + if (!empty($configs['apps'])) { + foreach ($configs['apps'] as $app => $appConfigs) { + foreach ($appConfigs as $key => $value) { + if ($value === null) { + $this->config->deleteAppValue($app, $key); + } else { + $this->config->setAppValue($app, $key, $value); + } + } + } + } + + $output->writeln('<info>Config successfully imported from: ' . $importFile . '</info>'); + return 0; + } + + /** + * Get the content from stdin ("config:import < file.json") + * + * @return string + */ + protected function getArrayFromStdin() { + // Read from stdin. stream_set_blocking is used to prevent blocking + // when nothing is passed via stdin. + stream_set_blocking(STDIN, 0); + $content = file_get_contents('php://stdin'); + stream_set_blocking(STDIN, 1); + return $content; + } + + /** + * Get the content of the specified file ("config:import file.json") + * + * @param string $importFile + * @return string + */ + protected function getArrayFromFile($importFile) { + return file_get_contents($importFile); + } + + /** + * @param string $content + * @return array + * @throws \UnexpectedValueException when the array is invalid + */ + protected function validateFileContent($content) { + $decodedContent = json_decode($content, true); + if (!is_array($decodedContent) || empty($decodedContent)) { + throw new \UnexpectedValueException('The file must contain a valid json array'); + } + + $this->validateArray($decodedContent); + + return $decodedContent; + } + + /** + * Validates that the array only contains `system` and `apps` + * + * @param array $array + */ + protected function validateArray($array) { + $arrayKeys = array_keys($array); + $additionalKeys = array_diff($arrayKeys, $this->validRootKeys); + $commonKeys = array_intersect($arrayKeys, $this->validRootKeys); + if (!empty($additionalKeys)) { + throw new \UnexpectedValueException('Found invalid entries in root: ' . implode(', ', $additionalKeys)); + } + if (empty($commonKeys)) { + throw new \UnexpectedValueException('At least one key of the following is expected: ' . implode(', ', $this->validRootKeys)); + } + + if (isset($array['system'])) { + if (is_array($array['system'])) { + foreach ($array['system'] as $name => $value) { + $this->checkTypeRecursively($value, $name); + } + } else { + throw new \UnexpectedValueException('The system config array is not an array'); + } + } + + if (isset($array['apps'])) { + if (is_array($array['apps'])) { + $this->validateAppsArray($array['apps']); + } else { + throw new \UnexpectedValueException('The apps config array is not an array'); + } + } + } + + /** + * @param mixed $configValue + * @param string $configName + */ + protected function checkTypeRecursively($configValue, $configName) { + if (!is_array($configValue) && !is_bool($configValue) && !is_int($configValue) && !is_string($configValue) && !is_null($configValue) && !is_float($configValue)) { + throw new \UnexpectedValueException('Invalid system config value for "' . $configName . '". Only arrays, bools, integers, floats, strings and null (delete) are allowed.'); + } + if (is_array($configValue)) { + foreach ($configValue as $key => $value) { + $this->checkTypeRecursively($value, $configName); + } + } + } + + /** + * Validates that app configs are only integers and strings + * + * @param array $array + */ + protected function validateAppsArray($array) { + foreach ($array as $app => $configs) { + foreach ($configs as $name => $value) { + if (!is_int($value) && !is_string($value) && !is_null($value)) { + throw new \UnexpectedValueException('Invalid app config value for "' . $app . '":"' . $name . '". Only integers, strings and null (delete) are allowed.'); + } + } + } + } + + /** + * @param string $optionName + * @param CompletionContext $context + * @return string[] + */ + public function completeOptionValues($optionName, CompletionContext $context) { + return []; + } + + /** + * @param string $argumentName + * @param CompletionContext $context + * @return string[] + */ + public function completeArgumentValues($argumentName, CompletionContext $context) { + if ($argumentName === 'file') { + $helper = new ShellPathCompletion( + $this->getName(), + 'file', + Completion::TYPE_ARGUMENT + ); + return $helper->run(); + } + return []; + } +} diff --git a/core/Command/Config/ListConfigs.php b/core/Command/Config/ListConfigs.php new file mode 100644 index 00000000000..a7c195276eb --- /dev/null +++ b/core/Command/Config/ListConfigs.php @@ -0,0 +1,145 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Core\Command\Config; + +use OC\Config\ConfigManager; +use OC\Core\Command\Base; +use OC\SystemConfig; +use OCP\IAppConfig; +use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class ListConfigs extends Base { + protected string $defaultOutputFormat = self::OUTPUT_FORMAT_JSON_PRETTY; + + public function __construct( + protected SystemConfig $systemConfig, + protected IAppConfig $appConfig, + protected ConfigManager $configManager, + ) { + parent::__construct(); + } + + protected function configure() { + parent::configure(); + + $this + ->setName('config:list') + ->setDescription('List all configs') + ->addArgument( + 'app', + InputArgument::OPTIONAL, + 'Name of the app ("system" to get the config.php values, "all" for all apps and system)', + 'all' + ) + ->addOption( + 'private', + null, + InputOption::VALUE_NONE, + 'Use this option when you want to include sensitive configs like passwords, salts, ...' + ) + ->addOption('migrate', null, InputOption::VALUE_NONE, 'Rename config keys of all enabled apps, based on ConfigLexicon') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $app = $input->getArgument('app'); + $noSensitiveValues = !$input->getOption('private'); + + if ($input->getOption('migrate')) { + $this->configManager->migrateConfigLexiconKeys(($app === 'all') ? null : $app); + } + + if (!is_string($app)) { + $output->writeln('<error>Invalid app value given</error>'); + return 1; + } + + switch ($app) { + case 'system': + $configs = [ + 'system' => $this->getSystemConfigs($noSensitiveValues), + ]; + break; + + case 'all': + $apps = $this->appConfig->getApps(); + $configs = [ + 'system' => $this->getSystemConfigs($noSensitiveValues), + 'apps' => [], + ]; + foreach ($apps as $appName) { + $configs['apps'][$appName] = $this->getAppConfigs($appName, $noSensitiveValues); + } + break; + + default: + $configs = [ + 'apps' => [$app => $this->getAppConfigs($app, $noSensitiveValues)], + ]; + } + + $this->writeArrayInOutputFormat($input, $output, $configs); + return 0; + } + + /** + * Get the system configs + * + * @param bool $noSensitiveValues + * @return array + */ + protected function getSystemConfigs(bool $noSensitiveValues): array { + $keys = $this->systemConfig->getKeys(); + + $configs = []; + foreach ($keys as $key) { + if ($noSensitiveValues) { + $value = $this->systemConfig->getFilteredValue($key, serialize(null)); + } else { + $value = $this->systemConfig->getValue($key, serialize(null)); + } + + if ($value !== 'N;') { + $configs[$key] = $value; + } + } + + return $configs; + } + + /** + * Get the app configs + * + * @param string $app + * @param bool $noSensitiveValues + * @return array + */ + protected function getAppConfigs(string $app, bool $noSensitiveValues) { + if ($noSensitiveValues) { + return $this->appConfig->getFilteredValues($app); + } else { + return $this->appConfig->getValues($app, false); + } + } + + /** + * @param string $argumentName + * @param CompletionContext $context + * @return string[] + */ + public function completeArgumentValues($argumentName, CompletionContext $context) { + if ($argumentName === 'app') { + return array_merge(['all', 'system'], \OC_App::getAllApps()); + } + return []; + } +} diff --git a/core/Command/Config/Preset.php b/core/Command/Config/Preset.php new file mode 100644 index 00000000000..ebd8aaa5cdf --- /dev/null +++ b/core/Command/Config/Preset.php @@ -0,0 +1,69 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Core\Command\Config; + +use OC\Config\PresetManager; +use OC\Core\Command\Base; +use OCP\Config\Lexicon\Preset as ConfigLexiconPreset; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Preset extends Base { + public function __construct( + private readonly PresetManager $presetManager, + ) { + parent::__construct(); + } + + protected function configure() { + parent::configure(); + $this->setName('config:preset') + ->setDescription('Select a config preset') + ->addArgument('preset', InputArgument::OPTIONAL, 'Preset to use for all unset config values', '') + ->addOption('list', '', InputOption::VALUE_NONE, 'display available preset'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + if ($input->getOption('list')) { + $this->getEnum('', $list); + $this->writeArrayInOutputFormat($input, $output, $list); + return self::SUCCESS; + } + + $presetArg = $input->getArgument('preset'); + if ($presetArg !== '') { + $preset = $this->getEnum($presetArg, $list); + if ($preset === null) { + $output->writeln('<error>Invalid preset: ' . $presetArg . '</error>'); + $output->writeln('Available presets: ' . implode(', ', $list)); + return self::INVALID; + } + + $this->presetManager->setLexiconPreset($preset); + } + + $current = $this->presetManager->getLexiconPreset(); + $this->writeArrayInOutputFormat($input, $output, [$current->name], 'current preset: '); + return self::SUCCESS; + } + + private function getEnum(string $name, ?array &$list = null): ?ConfigLexiconPreset { + $list = []; + foreach (ConfigLexiconPreset::cases() as $case) { + $list[] = $case->name; + if (strtolower($case->name) === strtolower($name)) { + return $case; + } + } + + return null; + } +} diff --git a/core/Command/Config/System/Base.php b/core/Command/Config/System/Base.php new file mode 100644 index 00000000000..088d902b4fd --- /dev/null +++ b/core/Command/Config/System/Base.php @@ -0,0 +1,65 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Core\Command\Config\System; + +use OC\SystemConfig; +use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; + +abstract class Base extends \OC\Core\Command\Base { + public function __construct( + protected SystemConfig $systemConfig, + ) { + parent::__construct(); + } + + /** + * @param string $argumentName + * @param CompletionContext $context + * @return string[] + */ + public function completeArgumentValues($argumentName, CompletionContext $context) { + if ($argumentName === 'name') { + $words = $this->getPreviousNames($context, $context->getWordIndex()); + if (empty($words)) { + $completions = $this->systemConfig->getKeys(); + } else { + $key = array_shift($words); + $value = $this->systemConfig->getValue($key); + $completions = array_keys($value); + + while (!empty($words) && is_array($value)) { + $key = array_shift($words); + if (!isset($value[$key]) || !is_array($value[$key])) { + break; + } + + $value = $value[$key]; + $completions = array_keys($value); + } + } + + return $completions; + } + return parent::completeArgumentValues($argumentName, $context); + } + + /** + * @param CompletionContext $context + * @param int $currentIndex + * @return string[] + */ + protected function getPreviousNames(CompletionContext $context, $currentIndex) { + $word = $context->getWordAtIndex($currentIndex - 1); + if ($word === $this->getName() || $currentIndex <= 0) { + return []; + } + + $words = $this->getPreviousNames($context, $currentIndex - 1); + $words[] = $word; + return $words; + } +} diff --git a/core/Command/Config/System/CastHelper.php b/core/Command/Config/System/CastHelper.php new file mode 100644 index 00000000000..f2b838bdf9b --- /dev/null +++ b/core/Command/Config/System/CastHelper.php @@ -0,0 +1,76 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OC\Core\Command\Config\System; + +class CastHelper { + /** + * @return array{value: mixed, readable-value: string} + */ + public function castValue(?string $value, string $type): array { + switch ($type) { + case 'integer': + case 'int': + if (!is_numeric($value)) { + throw new \InvalidArgumentException('Non-numeric value specified'); + } + return [ + 'value' => (int)$value, + 'readable-value' => 'integer ' . (int)$value, + ]; + + case 'double': + case 'float': + if (!is_numeric($value)) { + throw new \InvalidArgumentException('Non-numeric value specified'); + } + return [ + 'value' => (float)$value, + 'readable-value' => 'double ' . (float)$value, + ]; + + case 'boolean': + case 'bool': + $value = strtolower($value); + return match ($value) { + 'true' => [ + 'value' => true, + 'readable-value' => 'boolean ' . $value, + ], + 'false' => [ + 'value' => false, + 'readable-value' => 'boolean ' . $value, + ], + default => throw new \InvalidArgumentException('Unable to parse value as boolean'), + }; + + case 'null': + return [ + 'value' => null, + 'readable-value' => 'null', + ]; + + case 'string': + $value = (string)$value; + return [ + 'value' => $value, + 'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value, + ]; + + case 'json': + $value = json_decode($value, true); + return [ + 'value' => $value, + 'readable-value' => 'json ' . json_encode($value), + ]; + + default: + throw new \InvalidArgumentException('Invalid type'); + } + } +} diff --git a/core/Command/Config/System/DeleteConfig.php b/core/Command/Config/System/DeleteConfig.php new file mode 100644 index 00000000000..03960136f6f --- /dev/null +++ b/core/Command/Config/System/DeleteConfig.php @@ -0,0 +1,96 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Core\Command\Config\System; + +use OC\SystemConfig; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class DeleteConfig extends Base { + public function __construct( + SystemConfig $systemConfig, + ) { + parent::__construct($systemConfig); + } + + protected function configure() { + parent::configure(); + + $this + ->setName('config:system:delete') + ->setDescription('Delete a system config value') + ->addArgument( + 'name', + InputArgument::REQUIRED | InputArgument::IS_ARRAY, + 'Name of the config to delete, specify multiple for array parameter' + ) + ->addOption( + 'error-if-not-exists', + null, + InputOption::VALUE_NONE, + 'Checks whether the config exists before deleting it' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $configNames = $input->getArgument('name'); + $configName = $configNames[0]; + + if (count($configNames) > 1) { + if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) { + $output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>'); + return 1; + } + + $value = $this->systemConfig->getValue($configName); + + try { + $value = $this->removeSubValue(array_slice($configNames, 1), $value, $input->hasParameterOption('--error-if-not-exists')); + } catch (\UnexpectedValueException $e) { + $output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>'); + return 1; + } + + $this->systemConfig->setValue($configName, $value); + $output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' deleted</info>'); + return 0; + } else { + if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) { + $output->writeln('<error>System config ' . $configName . ' could not be deleted because it did not exist</error>'); + return 1; + } + + $this->systemConfig->deleteValue($configName); + $output->writeln('<info>System config value ' . $configName . ' deleted</info>'); + return 0; + } + } + + protected function removeSubValue($keys, $currentValue, $throwError) { + $nextKey = array_shift($keys); + + if (is_array($currentValue)) { + if (isset($currentValue[$nextKey])) { + if (empty($keys)) { + unset($currentValue[$nextKey]); + } else { + $currentValue[$nextKey] = $this->removeSubValue($keys, $currentValue[$nextKey], $throwError); + } + } elseif ($throwError) { + throw new \UnexpectedValueException('Config parameter does not exist'); + } + } elseif ($throwError) { + throw new \UnexpectedValueException('Config parameter does not exist'); + } + + return $currentValue; + } +} diff --git a/core/Command/Config/System/GetConfig.php b/core/Command/Config/System/GetConfig.php new file mode 100644 index 00000000000..c0a9623a84e --- /dev/null +++ b/core/Command/Config/System/GetConfig.php @@ -0,0 +1,80 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Core\Command\Config\System; + +use OC\SystemConfig; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class GetConfig extends Base { + public function __construct( + SystemConfig $systemConfig, + ) { + parent::__construct($systemConfig); + } + + protected function configure() { + parent::configure(); + + $this + ->setName('config:system:get') + ->setDescription('Get a system config value') + ->addArgument( + 'name', + InputArgument::REQUIRED | InputArgument::IS_ARRAY, + 'Name of the config to get, specify multiple for array parameter' + ) + ->addOption( + 'default-value', + null, + InputOption::VALUE_OPTIONAL, + 'If no default value is set and the config does not exist, the command will exit with 1' + ) + ; + } + + /** + * Executes the current command. + * + * @param InputInterface $input An InputInterface instance + * @param OutputInterface $output An OutputInterface instance + * @return int 0 if everything went fine, or an error code + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $configNames = $input->getArgument('name'); + $configName = array_shift($configNames); + $defaultValue = $input->getOption('default-value'); + + if (!in_array($configName, $this->systemConfig->getKeys()) && !$input->hasParameterOption('--default-value')) { + return 1; + } + + if (!in_array($configName, $this->systemConfig->getKeys())) { + $configValue = $defaultValue; + } else { + $configValue = $this->systemConfig->getValue($configName); + if (!empty($configNames)) { + foreach ($configNames as $configName) { + if (isset($configValue[$configName])) { + $configValue = $configValue[$configName]; + } elseif (!$input->hasParameterOption('--default-value')) { + return 1; + } else { + $configValue = $defaultValue; + break; + } + } + } + } + + $this->writeMixedInOutputFormat($input, $output, $configValue); + return 0; + } +} diff --git a/core/Command/Config/System/SetConfig.php b/core/Command/Config/System/SetConfig.php new file mode 100644 index 00000000000..1b1bdc66a6e --- /dev/null +++ b/core/Command/Config/System/SetConfig.php @@ -0,0 +1,124 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Core\Command\Config\System; + +use OC\SystemConfig; +use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class SetConfig extends Base { + public function __construct( + SystemConfig $systemConfig, + private CastHelper $castHelper, + ) { + parent::__construct($systemConfig); + } + + protected function configure() { + parent::configure(); + + $this + ->setName('config:system:set') + ->setDescription('Set a system config value') + ->addArgument( + 'name', + InputArgument::REQUIRED | InputArgument::IS_ARRAY, + 'Name of the config parameter, specify multiple for array parameter' + ) + ->addOption( + 'type', + null, + InputOption::VALUE_REQUIRED, + 'Value type [string, integer, double, boolean]', + 'string' + ) + ->addOption( + 'value', + null, + InputOption::VALUE_REQUIRED, + 'The new value of the config' + ) + ->addOption( + 'update-only', + null, + InputOption::VALUE_NONE, + 'Only updates the value, if it is not set before, it is not being added' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $configNames = $input->getArgument('name'); + $configName = $configNames[0]; + $configValue = $this->castHelper->castValue($input->getOption('value'), $input->getOption('type')); + $updateOnly = $input->getOption('update-only'); + + if (count($configNames) > 1) { + $existingValue = $this->systemConfig->getValue($configName); + + $newValue = $this->mergeArrayValue( + array_slice($configNames, 1), $existingValue, $configValue['value'], $updateOnly + ); + + $this->systemConfig->setValue($configName, $newValue); + } else { + if ($updateOnly && !in_array($configName, $this->systemConfig->getKeys(), true)) { + throw new \UnexpectedValueException('Config parameter does not exist'); + } + + $this->systemConfig->setValue($configName, $configValue['value']); + } + + $output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' set to ' . $configValue['readable-value'] . '</info>'); + return 0; + } + + /** + * @param array $configNames + * @param mixed $existingValues + * @param mixed $value + * @param bool $updateOnly + * @return array merged value + * @throws \UnexpectedValueException + */ + protected function mergeArrayValue(array $configNames, $existingValues, $value, $updateOnly) { + $configName = array_shift($configNames); + if (!is_array($existingValues)) { + $existingValues = []; + } + if (!empty($configNames)) { + if (isset($existingValues[$configName])) { + $existingValue = $existingValues[$configName]; + } else { + $existingValue = []; + } + $existingValues[$configName] = $this->mergeArrayValue($configNames, $existingValue, $value, $updateOnly); + } else { + if (!isset($existingValues[$configName]) && $updateOnly) { + throw new \UnexpectedValueException('Config parameter does not exist'); + } + $existingValues[$configName] = $value; + } + return $existingValues; + } + + /** + * @param string $optionName + * @param CompletionContext $context + * @return string[] + */ + public function completeOptionValues($optionName, CompletionContext $context) { + if ($optionName === 'type') { + return ['string', 'integer', 'double', 'boolean', 'json', 'null']; + } + return parent::completeOptionValues($optionName, $context); + } +} |