123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- 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;
- }
-
- protected function getArrayFromStdin() {
-
-
- stream_set_blocking(STDIN, 0);
- $content = file_get_contents('php://stdin');
- stream_set_blocking(STDIN, 1);
- return $content;
- }
-
- protected function getArrayFromFile($importFile) {
- return file_get_contents($importFile);
- }
-
- 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;
- }
-
- 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');
- }
- }
- }
-
- 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);
- }
- }
- }
-
- 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.');
- }
- }
- }
- }
-
- public function completeOptionValues($optionName, CompletionContext $context) {
- return [];
- }
-
- public function completeArgumentValues($argumentName, CompletionContext $context) {
- if ($argumentName === 'file') {
- $helper = new ShellPathCompletion(
- $this->getName(),
- 'file',
- Completion::TYPE_ARGUMENT
- );
- return $helper->run();
- }
- return [];
- }
- }
|