aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Config/GetConfigValue.php
blob: 46fe903b6a29da14f40ed193675145cac9018a36 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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;
    }
}