Skip to content

Commit 6832d54

Browse files
committed
feat(lexicon): occ config:preset
Signed-off-by: Maxence Lange <[email protected]>
1 parent c4284a4 commit 6832d54

File tree

6 files changed

+72
-0
lines changed

6 files changed

+72
-0
lines changed

core/Command/Config/Preset.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

core/register_command.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OC\Core\Command\Config\App\SetConfig;
2828
use OC\Core\Command\Config\Import;
2929
use OC\Core\Command\Config\ListConfigs;
30+
use OC\Core\Command\Config\Preset;
3031
use OC\Core\Command\Db\AddMissingColumns;
3132
use OC\Core\Command\Db\AddMissingIndices;
3233
use OC\Core\Command\Db\AddMissingPrimaryKeys;
@@ -149,6 +150,7 @@
149150
$application->add(Server::get(SetConfig::class));
150151
$application->add(Server::get(Import::class));
151152
$application->add(Server::get(ListConfigs::class));
153+
$application->add(Server::get(Preset::class));
152154
$application->add(Server::get(Command\Config\System\DeleteConfig::class));
153155
$application->add(Server::get(Command\Config\System\GetConfig::class));
154156
$application->add(Server::get(Command\Config\System\SetConfig::class));

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,7 @@
12451245
'OC\\Core\\Command\\Config\\App\\SetConfig' => $baseDir . '/core/Command/Config/App/SetConfig.php',
12461246
'OC\\Core\\Command\\Config\\Import' => $baseDir . '/core/Command/Config/Import.php',
12471247
'OC\\Core\\Command\\Config\\ListConfigs' => $baseDir . '/core/Command/Config/ListConfigs.php',
1248+
'OC\\Core\\Command\\Config\\Preset' => $baseDir . '/core/Command/Config/Preset.php',
12481249
'OC\\Core\\Command\\Config\\System\\Base' => $baseDir . '/core/Command/Config/System/Base.php',
12491250
'OC\\Core\\Command\\Config\\System\\CastHelper' => $baseDir . '/core/Command/Config/System/CastHelper.php',
12501251
'OC\\Core\\Command\\Config\\System\\DeleteConfig' => $baseDir . '/core/Command/Config/System/DeleteConfig.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
12861286
'OC\\Core\\Command\\Config\\App\\SetConfig' => __DIR__ . '/../../..' . '/core/Command/Config/App/SetConfig.php',
12871287
'OC\\Core\\Command\\Config\\Import' => __DIR__ . '/../../..' . '/core/Command/Config/Import.php',
12881288
'OC\\Core\\Command\\Config\\ListConfigs' => __DIR__ . '/../../..' . '/core/Command/Config/ListConfigs.php',
1289+
'OC\\Core\\Command\\Config\\Preset' => __DIR__ . '/../../..' . '/core/Command/Config/Preset.php',
12891290
'OC\\Core\\Command\\Config\\System\\Base' => __DIR__ . '/../../..' . '/core/Command/Config/System/Base.php',
12901291
'OC\\Core\\Command\\Config\\System\\CastHelper' => __DIR__ . '/../../..' . '/core/Command/Config/System/CastHelper.php',
12911292
'OC\\Core\\Command\\Config\\System\\DeleteConfig' => __DIR__ . '/../../..' . '/core/Command/Config/System/DeleteConfig.php',

lib/private/Config/ConfigManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public function migrateConfigLexiconKeys(?string $appId = null): void {
8282
$this->userConfig->ignoreLexiconAliases(false);
8383
}
8484

85+
/**
86+
* store in config.php the new preset
87+
* refresh cached preset
88+
*/
8589
public function setLexiconPreset(ConfigLexiconPreset $preset): void {
8690
$this->config->setSystemValue(self::PRESET_CONFIGKEY, $preset->value);
8791
self::$preset = null;

lib/unstable/Config/Lexicon/ConfigLexiconEntry.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public function __construct(
5959
}
6060

6161
/**
62+
* set a new default based on selected config preset
63+
*
6264
* @param ConfigLexiconPreset|ConfigLexiconPreset[] $preset
6365
* @experimental 32.0.0
6466
*/

0 commit comments

Comments
 (0)