|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | +namespace OC\Core\Command\Config; |
| 8 | + |
| 9 | +use NCU\Config\Lexicon\ConfigLexiconPreset; |
| 10 | +use OC\Config\ConfigManager; |
| 11 | +use OCP\IConfig; |
| 12 | +use Symfony\Component\Console\Command\Command; |
| 13 | +use Symfony\Component\Console\Input\InputArgument; |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | +use Symfony\Component\Console\Input\InputOption; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | + |
| 18 | +class Preset extends Command { |
| 19 | + public function __construct( |
| 20 | + private readonly IConfig $config, |
| 21 | + private readonly ConfigManager $configManager, |
| 22 | + ) { |
| 23 | + parent::__construct(); |
| 24 | + } |
| 25 | + |
| 26 | + protected function configure() { |
| 27 | + $this |
| 28 | + ->setName('config:preset') |
| 29 | + ->setDescription('Select a config preset') |
| 30 | + ->addArgument('preset', InputArgument::OPTIONAL, 'selected preset', '') |
| 31 | + ->addOption('current', '', InputOption::VALUE_NONE, 'display current preset'); |
| 32 | + } |
| 33 | + |
| 34 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 35 | + // if --current, only read current value |
| 36 | + if (!$input->getOption('current')) { |
| 37 | + $preset = $this->getEnum($input->getArgument('preset'), $list); |
| 38 | + |
| 39 | + if ($preset === null) { |
| 40 | + throw new \Exception('invalid preset. please choose one from the list: ' . implode(', ', $list)); |
| 41 | + } |
| 42 | + |
| 43 | + $this->configManager->setLexiconPreset($preset); |
| 44 | + } |
| 45 | + |
| 46 | + $current = ConfigLexiconPreset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)) ?? ConfigLexiconPreset::NONE; |
| 47 | + $output->writeln('current preset: ' . $current->name); |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + private function getEnum(string $name, ?array &$list = null): ?ConfigLexiconPreset { |
| 52 | + $list = []; |
| 53 | + foreach (ConfigLexiconPreset::cases() as $case) { |
| 54 | + $list[] = $case->name; |
| 55 | + if (strtolower($case->name) === strtolower($name)) { |
| 56 | + return $case; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return null; |
| 61 | + } |
| 62 | +} |
0 commit comments