diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2024-01-11 15:32:58 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2024-01-15 15:45:13 -0100 |
commit | f7d0c74b1003af6c47dd6ec74f4ef904a2681d62 (patch) | |
tree | a0bfe1934fbec0ccf6651824589a0e67ae4f9d9f /core/Command | |
parent | e5ef58b7b9b8f7a232666df17d286d43fb4d1201 (diff) | |
download | nextcloud-server-f7d0c74b1003af6c47dd6ec74f4ef904a2681d62.tar.gz nextcloud-server-f7d0c74b1003af6c47dd6ec74f4ef904a2681d62.zip |
lazy AppConfig
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'core/Command')
-rw-r--r-- | core/Command/Config/App/GetConfig.php | 42 | ||||
-rw-r--r-- | core/Command/Config/App/SetConfig.php | 206 | ||||
-rw-r--r-- | core/Command/Config/ListConfigs.php | 8 | ||||
-rw-r--r-- | core/Command/Upgrade.php | 13 |
4 files changed, 239 insertions, 30 deletions
diff --git a/core/Command/Config/App/GetConfig.php b/core/Command/Config/App/GetConfig.php index 96078b63e90..6052b88c013 100644 --- a/core/Command/Config/App/GetConfig.php +++ b/core/Command/Config/App/GetConfig.php @@ -1,8 +1,11 @@ <?php + +declare(strict_types=1); /** * @copyright Copyright (c) 2016, ownCloud, Inc. * * @author Joas Schilling <coding@schilljs.com> + * @author Maxence Lange <maxence@artificial-owl.com> * * @license AGPL-3.0 * @@ -21,7 +24,8 @@ */ namespace OC\Core\Command\Config\App; -use OCP\IConfig; +use OCP\Exceptions\AppConfigUnknownKeyException; +use OCP\IAppConfig; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -29,7 +33,7 @@ use Symfony\Component\Console\Output\OutputInterface; class GetConfig extends Base { public function __construct( - protected IConfig $config, + protected IAppConfig $appConfig, ) { parent::__construct(); } @@ -51,6 +55,12 @@ class GetConfig extends Base { 'Name of the config to get' ) ->addOption( + 'details', + null, + InputOption::VALUE_NONE, + 'returns complete details about the app config value' + ) + ->addOption( 'default-value', null, InputOption::VALUE_OPTIONAL, @@ -71,14 +81,32 @@ class GetConfig extends Base { $configName = $input->getArgument('name'); $defaultValue = $input->getOption('default-value'); - if (!in_array($configName, $this->config->getAppKeys($appName)) && !$input->hasParameterOption('--default-value')) { - return 1; + if ($input->getOption('details')) { + $details = $this->appConfig->getDetails($appName, $configName); + $format = $input->getOption('output') ?? 'plain'; + if ($format === 'json') { + $output->writeln(json_encode($details)); + } elseif ($format === 'json_pretty') { + $output->writeln(json_encode($details, JSON_PRETTY_PRINT)); + } else { + $output->writeln('App: ' . $details['app'] ?? ''); + $output->writeln('Config Key: ' . $details['key'] ?? ''); + $output->writeln('Config Value: ' . $details['value'] ?? ''); + $output->writeln('Value type: ' . $details['typeString'] ?? ''); + $output->writeln('Lazy loaded: ' . (($details['lazy'] ?? false) ? 'Yes' : 'No')); + $output->writeln('Sensitive: ' . (($details['sensitive'] ?? false) ? 'Yes' : 'No')); + } + + return 0; } - if (!in_array($configName, $this->config->getAppKeys($appName))) { + try { + $configValue = $this->appConfig->getDetails($appName, $configName)['value']; + } catch (AppConfigUnknownKeyException $e) { + if (!$input->hasParameterOption('--default-value')) { + return 1; + } $configValue = $defaultValue; - } else { - $configValue = $this->config->getAppValue($appName, $configName); } $this->writeMixedInOutputFormat($input, $output, $configValue); diff --git a/core/Command/Config/App/SetConfig.php b/core/Command/Config/App/SetConfig.php index 99746246b85..ac2c8fce6e9 100644 --- a/core/Command/Config/App/SetConfig.php +++ b/core/Command/Config/App/SetConfig.php @@ -1,8 +1,11 @@ <?php + +declare(strict_types=1); /** * @copyright Copyright (c) 2016, ownCloud, Inc. * * @author Joas Schilling <coding@schilljs.com> + * @author Maxence Lange <maxence@artificial-owl.com> * * @license AGPL-3.0 * @@ -21,15 +24,22 @@ */ namespace OC\Core\Command\Config\App; -use OCP\IConfig; +use OC\AppConfig; +use OCP\Exceptions\AppConfigIncorrectTypeException; +use OCP\Exceptions\AppConfigUnknownKeyException; +use OCP\IAppConfig; 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 { + private InputInterface $input; + private OutputInterface $output; + public function __construct( - protected IConfig $config, + protected IAppConfig $appConfig, ) { parent::__construct(); } @@ -57,6 +67,25 @@ class SetConfig extends Base { '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, @@ -68,16 +97,179 @@ class SetConfig extends Base { protected function execute(InputInterface $input, OutputInterface $output): int { $appName = $input->getArgument('app'); $configName = $input->getArgument('name'); + $this->input = $input; + $this->output = $output; + + 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>' + ); - if (!in_array($configName, $this->config->getAppKeys($appName)) && $input->hasParameterOption('--update-only')) { - $output->writeln('<comment>Config value ' . $configName . ' for app ' . $appName . ' not updated, as it has not been set before.</comment>'); return 1; } - $configValue = $input->getOption('value'); - $this->config->setAppValue($appName, $configName, $configValue); + $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('NOT LAZY')) { + $updated = $this->appConfig->updateLazy($appName, $configName, false); + } + if ($input->getOption('lazy') && !$this->appConfig->isLazy($appName, $configName) && $this->ask('LAZY')) { + $updated = $this->appConfig->updateLazy($appName, $configName, true) || $updated; + } + if (!$input->getOption('sensitive') && $this->appConfig->isSensitive($appName, $configName) && $this->ask('NOT SENSITIVE')) { + $updated = $this->appConfig->updateSensitive($appName, $configName, false) || $updated; + } + if ($input->getOption('sensitive') && !$this->appConfig->isSensitive($appName, $configName) && $this->ask('SENSITIVE')) { + $updated = $this->appConfig->updateSensitive($appName, $configName, true) || $updated; + } + if ($typeString !== null && $type !== $this->appConfig->getValueType($appName, $configName) && $this->ask($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 || $type === $currType || !$this->ask($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(($lazy) ? 'LAZY' : 'NOT LAZY')) { + $lazy = $currLazy; + } + } catch (AppConfigUnknownKeyException) { + $lazy = $lazy ?? false; + } + + /** + * same with sensitive status + */ + $sensitive = $input->getOption('sensitive'); + try { + $currSensitive = $this->appConfig->isLazy($appName, $configName); + if ($sensitive === null || $sensitive === $currSensitive || !$this->ask(($sensitive) ? 'LAZY' : 'NOT LAZY')) { + $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: + if ($value !== ((string) ((int) $value))) { + throw new AppConfigIncorrectTypeException('Value is not an integer'); + } + $updated = $this->appConfig->setValueInt($appName, $configName, (int)$value, $lazy, $sensitive); + break; + + case IAppConfig::VALUE_FLOAT: + if ($value !== ((string) ((float) $value))) { + throw new AppConfigIncorrectTypeException('Value is not a float'); + } + $updated = $this->appConfig->setValueFloat($appName, $configName, (float)$value, $lazy, $sensitive); + break; + + case IAppConfig::VALUE_BOOL: + if (strtolower($value) === 'true') { + $valueBool = true; + } elseif (strtolower($value) === 'false') { + $valueBool = false; + } else { + throw new AppConfigIncorrectTypeException('Value is not a boolean, please use \'true\' or \'false\''); + } + $updated = $this->appConfig->setValueBool($appName, $configName, $valueBool, $lazy); + break; + + case IAppConfig::VALUE_ARRAY: + $valueArray = json_decode($value, true, flags: JSON_THROW_ON_ERROR); + $valueArray = (is_array($valueArray)) ? $valueArray : throw new AppConfigIncorrectTypeException('Value is not an array'); + $updated = $this->appConfig->setValueArray($appName, $configName, $valueArray, $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['value'], + $current['typeString'], + $current['lazy'] ? 'lazy cache' : 'fast cache' + ) + ); + } else { + $output->writeln('<info>Config value were not updated</info>'); + } - $output->writeln('<info>Config value ' . $configName . ' for app ' . $appName . ' set to ' . $configValue . '</info>'); return 0; } + + private function ask(string $request): bool { + $helper = $this->getHelper('question'); + if ($this->input->getOption('no-interaction')) { + return true; + } + + $this->output->writeln(sprintf('You are about to set config value %s as <info>%s</info>', + '<info>' . $this->input->getArgument('app') . '</info>/<info>' . $this->input->getArgument('name') . '</info>', + strtoupper($request) + )); + $this->output->writeln(''); + $this->output->writeln('<comment>This might break thing, affect performance on your instance or its security!</comment>'); + + $result = (strtolower((string)$helper->ask( + $this->input, + $this->output, + new Question('<comment>Confirm this action by typing \'yes\'</comment>: '))) === 'yes'); + + $this->output->writeln(($result) ? 'done' : 'cancelled'); + $this->output->writeln(''); + + return $result; + } } diff --git a/core/Command/Config/ListConfigs.php b/core/Command/Config/ListConfigs.php index 4adb0a9df5b..51a17eb2097 100644 --- a/core/Command/Config/ListConfigs.php +++ b/core/Command/Config/ListConfigs.php @@ -91,9 +91,7 @@ class ListConfigs extends Base { default: $configs = [ - 'apps' => [ - $app => $this->getAppConfigs($app, $noSensitiveValues), - ], + 'apps' => [$app => $this->getAppConfigs($app, $noSensitiveValues)], ]; } @@ -107,7 +105,7 @@ class ListConfigs extends Base { * @param bool $noSensitiveValues * @return array */ - protected function getSystemConfigs($noSensitiveValues) { + protected function getSystemConfigs(bool $noSensitiveValues): array { $keys = $this->systemConfig->getKeys(); $configs = []; @@ -133,7 +131,7 @@ class ListConfigs extends Base { * @param bool $noSensitiveValues * @return array */ - protected function getAppConfigs($app, $noSensitiveValues) { + protected function getAppConfigs(string $app, bool $noSensitiveValues) { if ($noSensitiveValues) { return $this->appConfig->getFilteredValues($app, false); } else { diff --git a/core/Command/Upgrade.php b/core/Command/Upgrade.php index 30acd8f7d4d..c74b8d27049 100644 --- a/core/Command/Upgrade.php +++ b/core/Command/Upgrade.php @@ -35,7 +35,6 @@ namespace OC\Core\Command; use OC\Console\TimestampFormatter; use OC\DB\MigratorExecuteSqlEvent; -use OC\Installer; use OC\Repair\Events\RepairAdvanceEvent; use OC\Repair\Events\RepairErrorEvent; use OC\Repair\Events\RepairFinishEvent; @@ -48,7 +47,6 @@ use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\Util; -use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputInterface; @@ -63,9 +61,7 @@ class Upgrade extends Command { public const ERROR_FAILURE = 5; public function __construct( - private IConfig $config, - private LoggerInterface $logger, - private Installer $installer, + private IConfig $config ) { parent::__construct(); } @@ -91,12 +87,7 @@ class Upgrade extends Command { } $self = $this; - $updater = new Updater( - $this->config, - \OC::$server->getIntegrityCodeChecker(), - $this->logger, - $this->installer - ); + $updater = \OCP\Server::get(Updater::class); /** @var IEventDispatcher $dispatcher */ $dispatcher = \OC::$server->get(IEventDispatcher::class); |