diff options
Diffstat (limited to 'core/Command/Config')
-rw-r--r-- | core/Command/Config/App/DeleteConfig.php | 10 | ||||
-rw-r--r-- | core/Command/Config/App/GetConfig.php | 36 | ||||
-rw-r--r-- | core/Command/Config/App/SetConfig.php | 206 | ||||
-rw-r--r-- | core/Command/Config/Import.php | 6 | ||||
-rw-r--r-- | core/Command/Config/ListConfigs.php | 21 | ||||
-rw-r--r-- | core/Command/Config/System/Base.php | 7 | ||||
-rw-r--r-- | core/Command/Config/System/DeleteConfig.php | 4 | ||||
-rw-r--r-- | core/Command/Config/System/GetConfig.php | 4 | ||||
-rw-r--r-- | core/Command/Config/System/SetConfig.php | 4 |
9 files changed, 249 insertions, 49 deletions
diff --git a/core/Command/Config/App/DeleteConfig.php b/core/Command/Config/App/DeleteConfig.php index 0da1e965bd0..b77f27ccd07 100644 --- a/core/Command/Config/App/DeleteConfig.php +++ b/core/Command/Config/App/DeleteConfig.php @@ -28,14 +28,10 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class DeleteConfig extends Base { - protected IConfig $config; - - /** - * @param IConfig $config - */ - public function __construct(IConfig $config) { + public function __construct( + protected IConfig $config, + ) { parent::__construct(); - $this->config = $config; } protected function configure() { diff --git a/core/Command/Config/App/GetConfig.php b/core/Command/Config/App/GetConfig.php index 7fdff2be732..f85f978cc61 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,18 +24,18 @@ */ 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; use Symfony\Component\Console\Output\OutputInterface; class GetConfig extends Base { - protected IConfig $config; - - public function __construct(IConfig $config) { + public function __construct( + protected IAppConfig $appConfig, + ) { parent::__construct(); - $this->config = $config; } protected function configure() { @@ -52,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, @@ -72,14 +81,21 @@ 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); + $details['type'] = $details['typeString']; + unset($details['typeString']); + $this->writeArrayInOutputFormat($input, $output, $details); + 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 89a5f6ba5d1..ae6f24e71d4 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,18 +24,21 @@ */ 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 { - protected IConfig $config; - - public function __construct(IConfig $config) { + public function __construct( + protected IAppConfig $appConfig, + ) { parent::__construct(); - $this->config = $config; } protected function configure() { @@ -58,6 +64,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, @@ -70,15 +95,176 @@ class SetConfig extends Base { $appName = $input->getArgument('app'); $configName = $input->getArgument('name'); - 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>'); + 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; } - $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($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->isLazy($appName, $configName); + if ($sensitive === null || $sensitive === $currSensitive || !$this->ask($input, $output, ($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 (in_array(strtolower($value), ['true', '1', 'on', 'yes'])) { + $valueBool = true; + } elseif (in_array(strtolower($value), ['false', '0', 'off', 'no'])) { + $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(InputInterface $input, OutputInterface $output, string $request): bool { + $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 index 227c909038c..b8431c8c295 100644 --- a/core/Command/Config/Import.php +++ b/core/Command/Config/Import.php @@ -35,11 +35,11 @@ use Symfony\Component\Console\Output\OutputInterface; class Import extends Command implements CompletionAwareInterface { protected array $validRootKeys = ['system', 'apps']; - protected IConfig $config; - public function __construct(IConfig $config) { + public function __construct( + protected IConfig $config, + ) { parent::__construct(); - $this->config = $config; } protected function configure() { diff --git a/core/Command/Config/ListConfigs.php b/core/Command/Config/ListConfigs.php index a0fa9a84ea8..51a17eb2097 100644 --- a/core/Command/Config/ListConfigs.php +++ b/core/Command/Config/ListConfigs.php @@ -33,13 +33,12 @@ use Symfony\Component\Console\Output\OutputInterface; class ListConfigs extends Base { protected string $defaultOutputFormat = self::OUTPUT_FORMAT_JSON_PRETTY; - protected SystemConfig $systemConfig; - protected IAppConfig $appConfig; - public function __construct(SystemConfig $systemConfig, IAppConfig $appConfig) { + public function __construct( + protected SystemConfig $systemConfig, + protected IAppConfig $appConfig, + ) { parent::__construct(); - $this->systemConfig = $systemConfig; - $this->appConfig = $appConfig; } protected function configure() { @@ -77,7 +76,7 @@ class ListConfigs extends Base { $configs = [ 'system' => $this->getSystemConfigs($noSensitiveValues), ]; - break; + break; case 'all': $apps = $this->appConfig->getApps(); @@ -88,13 +87,11 @@ class ListConfigs extends Base { foreach ($apps as $appName) { $configs['apps'][$appName] = $this->getAppConfigs($appName, $noSensitiveValues); } - break; + break; default: $configs = [ - 'apps' => [ - $app => $this->getAppConfigs($app, $noSensitiveValues), - ], + 'apps' => [$app => $this->getAppConfigs($app, $noSensitiveValues)], ]; } @@ -108,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 = []; @@ -134,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/Config/System/Base.php b/core/Command/Config/System/Base.php index 18bc9cb7ca0..09ec456f6a4 100644 --- a/core/Command/Config/System/Base.php +++ b/core/Command/Config/System/Base.php @@ -26,11 +26,10 @@ use OC\SystemConfig; use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; abstract class Base extends \OC\Core\Command\Base { - protected SystemConfig $systemConfig; - - public function __construct(SystemConfig $systemConfig) { + public function __construct( + protected SystemConfig $systemConfig, + ) { parent::__construct(); - $this->systemConfig = $systemConfig; } /** diff --git a/core/Command/Config/System/DeleteConfig.php b/core/Command/Config/System/DeleteConfig.php index f4d49ba8f51..f6650e7d6d3 100644 --- a/core/Command/Config/System/DeleteConfig.php +++ b/core/Command/Config/System/DeleteConfig.php @@ -30,7 +30,9 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class DeleteConfig extends Base { - public function __construct(SystemConfig $systemConfig) { + public function __construct( + SystemConfig $systemConfig, + ) { parent::__construct($systemConfig); } diff --git a/core/Command/Config/System/GetConfig.php b/core/Command/Config/System/GetConfig.php index 01bbf82d5d1..ab5b884fda4 100644 --- a/core/Command/Config/System/GetConfig.php +++ b/core/Command/Config/System/GetConfig.php @@ -29,7 +29,9 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class GetConfig extends Base { - public function __construct(SystemConfig $systemConfig) { + public function __construct( + SystemConfig $systemConfig, + ) { parent::__construct($systemConfig); } diff --git a/core/Command/Config/System/SetConfig.php b/core/Command/Config/System/SetConfig.php index 01a1999bcf9..ba5265a84d7 100644 --- a/core/Command/Config/System/SetConfig.php +++ b/core/Command/Config/System/SetConfig.php @@ -32,7 +32,9 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class SetConfig extends Base { - public function __construct(SystemConfig $systemConfig) { + public function __construct( + SystemConfig $systemConfig, + ) { parent::__construct($systemConfig); } |