config.pm 5.08 KB
Newer Older
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
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4

# Sympa - SYsteme de Multi-Postage Automatique
#
# Copyright 2022 The Sympa Community. See the
# AUTHORS.md file at the top-level directory of this distribution and at
# <https://github.com/sympa-community/sympa.git>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package Sympa::CLI::config;

use strict;
use warnings;
use English qw(-no_match_vars);
28
use POSIX qw();
29
30
31
32
33

use Conf;
use Sympa::ConfDef;
use Sympa::Constants;
use Sympa::Language;
34
use Sympa::Tools::Data;
35
36
37

use parent qw(Sympa::CLI);

38
use constant _options   => qw(output|o=s@);
39
use constant _args      => qw(keyvalue*);
40
41
42
43
44
45
46
47
48
use constant _need_priv => 0;

my $language = Sympa::Language->instance;

sub _run {
    my $class   = shift;
    my $options = shift;
    my @argv    = @_;

49
    my %newConf;
50
    foreach my $arg (@argv) {
51
52
53
        # Check for key/values settings.
        last unless ref $arg eq 'ARRAY';
        my ($key, $val) = @$arg;
54

55
56
        # FIXME: Resolve parameter aliase names.
        $newConf{$key} = $val;
57
    }
58
59
60
    if (%newConf) {
        my $curConf = _load();
        return undef unless $curConf;
61

62
63
64
65
66
67
        my $out = Sympa::Tools::Data::format_config(
            [@Sympa::ConfDef::params],
            $curConf, \%newConf,
            only_changed => 1,
            filter => ([@{$options->{output} // []}, qw(explicit mandatory)])
        );
68
        die "Not changed.\n" unless defined $out;
69
70

        my $sympa_conf = Sympa::Constants::CONFIG();
71
        my $date = POSIX::strftime('%Y%m%d%H%M%S', localtime time);
72
73
74
75
76
77
78

        ## Keep old config file
        unless (rename $sympa_conf, $sympa_conf . '.' . $date) {
            warn $language->gettext_sprintf('Unable to rename %s : %s',
                $sympa_conf, $ERRNO);
        }

79
        # Write new config file.
80
81
82
83
84
85
86
87
88
89
90
        my $umask = umask 037;
        my $ofh;
        unless (open $ofh, '>', $sympa_conf) {
            umask $umask;
            die "$0: "
                . $language->gettext_sprintf('Unable to open %s : %s',
                $sympa_conf, $ERRNO)
                . "\n";
        }
        umask $umask;
        chown [getpwnam(Sympa::Constants::USER)]->[2],
91
92
            [getgrnam(Sympa::Constants::GROUP)]->[2], $sympa_conf
            if $UID == 0;
93

94
        print $ofh $out;
95
96
97
98
99
100
        close $ofh;

        print $language->gettext_sprintf(
            "%s have been updated.\nPrevious versions have been saved as %s.\n",
            $sympa_conf, "$sympa_conf.$date"
        );
101
        return 1;
102
    }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

    Sympa::CLI->run(qw(help config));
}

# Old name: edit_configuragion() in sympa_wizard.pl.
# Obsoleted: Use Sympa::Tools::Data::format_config().
#sub _edit_configuration;

sub _load {
    my $sympa_conf = shift || Sympa::Constants::CONFIG();

    #FIXME: Refactor Conf.
    my $res = Conf::_load_config_file_to_hash($sympa_conf);
    return undef unless $res;
    return $res->{config};
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
}

1;
__END__

=encoding utf-8

=head1 NAME

sympa-config - Manipulate configuration of Sympa

=head1 SYNOPSIS

C<sympa config> I<name>=I<value> ...

C<sympa config> I<sub-command> [ I<options> ... ]

=head1 DESCRIPTION

=over

=item C<sympa> C<config> I<name>C<=>I<value> ...

Edit configuration file in batch mode.
Arguments would include pairs of parameter name and value.

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
If no explicit changes given, configuration file won't be rewritten.

Options:

=over

=item C<-o>, C<--output=>I<set> ...

Specify set(s) of parameters to be output.
I<set> may be either C<omittable>, C<optional>, C<mandatory>,
C<full> (synonym for the former three), C<explicit> or C<minimal>.
This option can be specified more than once.

With this command, C<explicit> and C<mandatory> sets,
i.e. those defined in the configuration file explicitly,
specified in command line arguments or defined as mandatory,
are always included.

=back

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
=back

=head2 SUB-COMMANDS

Currently following sub-commands are available.
To see detail of each sub-command,
run 'C<sympa help config> I<sub-command>'.

=over

=item L<C<sympa config create> ...|sympa-config-create(1)>

Create configuration file.

=item L<C<sympa config show> ...|sympa-config-show(1)>

Show the content of configuration file.

=back

=head1 HISTORY

L<sympa_wizard.pl> appeared on Sympa 3.3.4b.9.
It was originally written by:

=over 4

=item Serge Aumont <sa@cru.fr>

=item Olivier SalaE<252>n <os@cru.fr>

=back

C<--batch> and C<--display> options are added on Sympa 6.1.25 and 6.2.15.

On Sympa 6.2.69b, the most of functions provided by this program was
reorganized and was integrated into C<sympa config> command line,
therefore sympa_wizard.pl was deprecated.

=cut