aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Config
diff options
context:
space:
mode:
authorMarcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>2023-05-16 09:17:37 +0000
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2023-05-16 09:17:37 +0000
commit1de91f34232717544892bfddd3314900edf91f3b (patch)
tree4ec7e15e07499ffe631135c2ae36a7da4da54588 /cli/Commands/Config
parent132e3454d06db698ca5c184050b4a4aadef9d574 (diff)
Add CLI scripts to get and set global configuration.
Closes #2588 Merge request studip/studip!1746
Diffstat (limited to 'cli/Commands/Config')
-rw-r--r--cli/Commands/Config/ConfigList.php53
-rw-r--r--cli/Commands/Config/GetConfigValue.php107
-rw-r--r--cli/Commands/Config/SectionList.php36
-rw-r--r--cli/Commands/Config/SetConfigValue.php36
4 files changed, 232 insertions, 0 deletions
diff --git a/cli/Commands/Config/ConfigList.php b/cli/Commands/Config/ConfigList.php
new file mode 100644
index 0000000..e818ac0
--- /dev/null
+++ b/cli/Commands/Config/ConfigList.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace Studip\Cli\Commands\Config;
+
+use ConfigurationModel;
+use Symfony\Component\Console\Helper\Table;
+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 ConfigList extends Command
+{
+ protected static $defaultName = 'config:list';
+
+ protected function configure()
+ {
+ $this->setDescription('List all Stud.IP configuration.');
+ $this->setHelp('This command shows a list of available configurations.');
+ $this->addArgument('config-section', InputArgument::OPTIONAL, 'Section of the configuration.');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $section = $input->getArgument('config-section');
+ $sections = ConfigurationModel::getConfig($section);
+
+ foreach ($sections as $section => $configs) {
+ $data = [];
+ foreach ($configs as $config) {
+ $data[] = [
+ $config['field'],
+ $config['type'],
+ mila(kill_format((string)$config['value']),40),
+ $config['range'],
+ mila(kill_format((string)$config['description']),40),
+ ];
+ }
+ $table = new Table($output);
+ $table->setColumnMaxWidth(1, 10);
+ $table->setColumnMaxWidth(2, 50);
+ $table->setColumnMaxWidth(3, 10);
+ $table->setColumnMaxWidth(4, 50);
+ $table->setHeaderTitle($section ?? _('Ohne Kategorie'));
+ $table->setHeaders(['Field', 'Type', 'Value', 'Range', 'Description']);
+ $table->setRows($data);
+ $table->setStyle('box');
+ $table->render();
+ }
+
+ return Command::SUCCESS;
+ }
+}
diff --git a/cli/Commands/Config/GetConfigValue.php b/cli/Commands/Config/GetConfigValue.php
new file mode 100644
index 0000000..46fe903
--- /dev/null
+++ b/cli/Commands/Config/GetConfigValue.php
@@ -0,0 +1,107 @@
+<?php
+
+namespace Studip\Cli\Commands\Config;
+
+use Course;
+use Institute;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Helper\Table;
+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 User;
+use RangeFactory;
+
+class GetConfigValue extends Command
+{
+ protected static $defaultName = 'config:get';
+
+ protected function configure(): void
+ {
+ $this->setDescription('Get value of a Stud.IP configuration key.');
+ $this->setHelp('This command will return the value of a Stud.IP configuration key.');
+ $this->addArgument('config-key', InputArgument::REQUIRED, 'Key of the configuration.');
+ $this->addOption(
+ 'user',
+ 'u',
+ InputOption::VALUE_OPTIONAL,
+ 'Read configuration for a user'
+ );
+ $this->addOption(
+ 'course',
+ 'c',
+ InputOption::VALUE_OPTIONAL,
+ 'Read configuration for a course'
+ );
+ $this->addOption(
+ 'inst',
+ 'i',
+ InputOption::VALUE_OPTIONAL,
+ 'Read configuration for a institute'
+ );
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output): int
+ {
+ $configKey = $input->getArgument('config-key');
+ $userId = $input->getOption('user');
+ $courseId = $input->getOption('course');
+ $instituteId = $input->getOption('inst');
+
+ $range = null;
+ if ($userId && $courseId && $instituteId) {
+ $output->writeln('<error>Please select one specific range</error>');
+ return Command::FAILURE;
+ }
+ if ($userId) {
+ $range = User::find($userId);
+ if (empty($range)) {
+ $output->writeln('<error>Could not find user</error>');
+ return Command::FAILURE;
+ }
+
+ } else if ($courseId) {
+ $range = Course::find($courseId);
+ if (empty($range)) {
+ $output->writeln('<error>Could not find course</error>');
+ return Command::FAILURE;
+ }
+ } else if ($instituteId) {
+ $range = Institute::find($instituteId);
+ if (empty($range)) {
+ $output->writeln('<error>Could not find institute</error>');
+ return Command::FAILURE;
+ }
+ }
+
+ if ($range) {
+ $config = $range->getConfiguration();
+ } else {
+ $config = \Config::get();
+ }
+
+ if (empty($config)) {
+ $output->writeln('<error>Could not find config</error>');
+ return Command::FAILURE;
+ }
+
+ $metadata = $config->getMetadata($configKey) ?: [
+ 'field' => $configKey,
+ 'type' => 'string',
+ 'description' => 'missing in table `config`',
+ ];
+ if (isset($metadata['is_default'])) {
+ $metadata['is_default'] = $metadata['is_default'] ? 'true' : 'false';
+ }
+ $metadata['value'] = $config->$configKey;
+ $pairs = array_map(null, array_keys($metadata), array_values($metadata));
+
+ $table = new Table($output);
+ $table->setHeaders(['Field', 'Value'])->setRows($pairs);
+ $table->setStyle('box');
+ $table->render();
+
+ return Command::SUCCESS;
+ }
+}
diff --git a/cli/Commands/Config/SectionList.php b/cli/Commands/Config/SectionList.php
new file mode 100644
index 0000000..28a0ed7
--- /dev/null
+++ b/cli/Commands/Config/SectionList.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace Studip\Cli\Commands\Config;
+
+use ConfigurationModel;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Helper\Table;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class SectionList extends Command
+{
+ protected static $defaultName = 'config:section-list';
+
+ protected function configure(): void
+ {
+ $this->setDescription('List all configuration sections');
+ $this->setHelp('This command shows a list of available configuration sections.');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $ranges = ConfigurationModel::getConfig();
+ $data = [];
+ foreach (array_keys($ranges) as $range) {
+ $data[] = [$range ?? _('Ohne Kategorie')];
+ }
+ $table = new Table($output);
+ $table->setHeaders(['Range'])
+ ->setRows($data)
+ ->setStyle('box')
+ ->render();
+
+ return Command::SUCCESS;
+ }
+}
diff --git a/cli/Commands/Config/SetConfigValue.php b/cli/Commands/Config/SetConfigValue.php
new file mode 100644
index 0000000..9444dc9
--- /dev/null
+++ b/cli/Commands/Config/SetConfigValue.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace Studip\Cli\Commands\Config;
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Helper\Table;
+
+class SetConfigValue extends Command
+{
+ protected static $defaultName = 'config:set';
+
+ protected function configure(): void
+ {
+ $this->setDescription('Set value of a Stud.IP configuration key.');
+ $this->setHelp('This command will set the value of a Stud.IP configuration key.');
+ $this->addArgument('config-key', InputArgument::REQUIRED, 'Key of the configuration.');
+ $this->addArgument('config-value', InputArgument::REQUIRED, 'Value of the configuration.');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output): int
+ {
+ $configKey = $input->getArgument('config-key');
+ $configValue = $input->getArgument('config-value');
+ $metadata = \Config::get()->getMetadata($configKey);
+ if (!$metadata) {
+ throw new \RuntimeException("Unknown config key '{$configKey}");
+ }
+
+ \Config::get()->store($configKey, $configValue);
+
+ return Command::SUCCESS;
+ }
+}