1# --
2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
3# --
4# This software comes with ABSOLUTELY NO WARRANTY. For details, see
5# the enclosed file COPYING for license information (GPL). If you
6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
7# --
8
9package Kernel::System::Console::Command::Admin::Config::Read;
10
11use strict;
12use warnings;
13
14use parent qw(Kernel::System::Console::BaseCommand);
15
16our @ObjectDependencies = (
17    'Kernel::System::SysConfig',
18    'Kernel::System::Main',
19    'Kernel::System::YAML',
20);
21
22sub Configure {
23    my ( $Self, %Param ) = @_;
24
25    $Self->Description('Gather the value of a setting.');
26    $Self->AddOption(
27        Name        => 'setting-name',
28        Description => "The name of the setting.",
29        Required    => 1,
30        HasValue    => 1,
31        ValueRegex  => qr/.*/,
32    );
33    $Self->AddOption(
34        Name        => 'target-path',
35        Description => "Specify the output location of the setting value YAML file.",
36        Required    => 0,
37        HasValue    => 1,
38        ValueRegex  => qr/.*/smx,
39    );
40
41    return;
42}
43
44sub Run {
45    my ( $Self, %Param ) = @_;
46
47    $Self->Print("<yellow>Gathering setting value...</yellow>\n");
48
49    my $SettingName = $Self->GetOption('setting-name');
50
51    my %Setting = $Kernel::OM->Get('Kernel::System::SysConfig')->SettingGet(
52        Name => $SettingName,
53    );
54
55    # Return if there was no setting.
56    if ( !%Setting ) {
57        $Self->Print("<red>Fail.</red>\n");
58        return $Self->ExitCodeError();
59    }
60
61    # Return if setting is invalid or not visible
62    if ( !$Setting{IsValid} || $Setting{IsInvisible} ) {
63        $Self->PrintError("Setting is invalid!\nFail.");
64        return $Self->ExitCodeError();
65    }
66
67    # Return if not effectiveValue.
68    if ( !defined $Setting{EffectiveValue} ) {
69        $Self->PrintError("No effective value found for setting: $SettingName!.\nFail.");
70        return $Self->ExitCodeError();
71    }
72
73    # Dump config as string.
74    my $TargetPath         = $Self->GetOption('target-path');
75    my $EffectiveValueYAML = $Kernel::OM->Get('Kernel::System::YAML')->Dump(
76        Data => $Setting{EffectiveValue},
77    );
78
79    if ($TargetPath) {
80
81        # Write configuration in a file.
82        my $FileLocation = $Kernel::OM->Get('Kernel::System::Main')->FileWrite(
83            Location => $TargetPath,
84            Content  => \$EffectiveValueYAML,
85            Mode     => 'utf8',
86        );
87
88        # Check if target file exists.
89        if ( !$FileLocation ) {
90            $Self->PrintError("Could not write file $TargetPath!\nFail.\n");
91            return $Self->ExitCodeError();
92        }
93
94        $Self->Print("<green>Done.</green>\n");
95        return $Self->ExitCodeOk();
96    }
97
98    # Send value to standard output
99    $Self->Print("\nSetting: <yellow>$SettingName</yellow>");
100    if ( !ref $Setting{EffectiveValue} ) {
101        $Self->Print("\n$Setting{EffectiveValue}\n\n");
102    }
103    else {
104        $Self->Print(" (YAML)\n$EffectiveValueYAML\n");
105    }
106
107    $Self->Print("<green>Done.</green>\n");
108    return $Self->ExitCodeOk();
109}
110
1111;
112
113=head1 TERMS AND CONDITIONS
114
115This software is part of the OTRS project (L<https://otrs.org/>).
116
117This software comes with ABSOLUTELY NO WARRANTY. For details, see
118the enclosed file COPYING for license information (GPL). If you
119did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
120
121=cut
122