aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Config/Preset.php
blob: 4f0278896db49349d78fb4689565900462361c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OC\Core\Command\Config;

use OC\Config\ConfigManager;
use OC\Core\Command\Base;
use OCP\Config\Lexicon\Preset as ConfigLexiconPreset;
use OCP\IConfig;
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 Preset extends Base {
	public function __construct(
		private readonly IConfig $config,
		private readonly ConfigManager $configManager,
	) {
		parent::__construct();
	}

	protected function configure() {
		parent::configure();
		$this->setName('config:preset')
			->setDescription('Select a config preset')
			->addArgument('preset', InputArgument::OPTIONAL, 'Preset to use for all unset config values', '')
			->addOption('list', '', InputOption::VALUE_NONE, 'display available preset');
	}

	protected function execute(InputInterface $input, OutputInterface $output): int {
		if ($input->getOption('list')) {
			$this->getEnum('', $list);
			$this->writeArrayInOutputFormat($input, $output, $list);
			return self::SUCCESS;
		}

		$presetArg = $input->getArgument('preset');
		if ($presetArg !== '') {
			$preset = $this->getEnum($presetArg, $list);
			if ($preset === null) {
				$output->writeln('<error>Invalid preset: ' . $presetArg . '</error>');
				$output->writeln('Available presets: ' . implode(', ', $list));
				return self::INVALID;
			}

			$this->configManager->setLexiconPreset($preset);
		}

		$current = ConfigLexiconPreset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)) ?? ConfigLexiconPreset::NONE;
		$this->writeArrayInOutputFormat($input, $output, [$current->name], 'current preset: ');
		return self::SUCCESS;
	}

	private function getEnum(string $name, ?array &$list = null): ?ConfigLexiconPreset {
		$list = [];
		foreach (ConfigLexiconPreset::cases() as $case) {
			$list[] = $case->name;
			if (strtolower($case->name) === strtolower($name)) {
				return $case;
			}
		}

		return null;
	}
}