Commit 9e050343 authored by IKEDA Soji's avatar IKEDA Soji
Browse files

Refactoring and some improvements.

  * sympa config: Only necessary parameters may be output by default.
parent d554c047
......@@ -180,6 +180,10 @@ sub run {
if ($arg eq '*') {
$val = $arg;
}
} elsif ($def eq 'keyvalue') {
if ($arg =~ /\A(\w+)=(.*)\z/) {
$val = [$1 => $2];
}
} else {
$val = $arg;
}
......
......@@ -25,17 +25,18 @@ package Sympa::CLI::config;
use strict;
use warnings;
use English qw(-no_match_vars);
use Sys::Hostname qw();
use POSIX qw();
use Conf;
use Sympa::ConfDef;
use Sympa::Constants;
use Sympa::Language;
use Sympa::Tools::Data;
use parent qw(Sympa::CLI);
use constant _options => qw();
use constant _args => qw();
use constant _args => qw(keyvalue*);
use constant _need_priv => 0;
my $language = Sympa::Language->instance;
......@@ -45,152 +46,25 @@ sub _run {
my $options = shift;
my @argv = @_;
my %user_param;
my %newConf;
foreach my $arg (@argv) {
# Check for key/values settings
if ($arg =~ /\A(\w+)=(.+)/) {
$user_param{$1} = $2;
} else {
die "$0: Invalid commandline argument: $arg\n";
}
}
if (%user_param) {
return _edit_configuration($options, %user_param);
}
# Check for key/values settings.
last unless ref $arg eq 'ARRAY';
my ($key, $val) = @$arg;
Sympa::CLI->run(qw(help config));
}
# Old name: edit_configuragion() in sympa_wizard.pl.
sub _edit_configuration {
my $options = shift;
my %user_param = @_;
die "$0: You must run as superuser.\n"
if $UID;
# complement required fields.
foreach my $param (@Sympa::ConfDef::params) {
next if $param->{obsolete};
next unless $param->{'name'};
if ($param->{'name'} eq 'domain') {
$param->{'default'} = Sys::Hostname::hostname();
} elsif ($param->{'name'} eq 'wwsympa_url') {
$param->{'default'} = sprintf 'http://%s/sympa',
Sys::Hostname::hostname();
} elsif ($param->{'name'} eq 'listmaster') {
$param->{'default'} = sprintf 'your_email_address@%s',
Sys::Hostname::hostname();
}
# FIXME: Resolve parameter aliase names.
$newConf{$key} = $val;
}
if (%newConf) {
my $curConf = _load();
return undef unless $curConf;
## Load sympa config (but not using database)
unless (defined Conf::load(undef, 1)) {
die sprintf
"%s: Unable to load sympa configuration, file %s or one of the virtual host robot.conf files contain errors. Exiting.\n",
$PROGRAM_NAME, Sympa::Constants::CONFIG();
}
my $somechange = 0;
my @new_sympa_conf;
my $title = undef;
# dynamic defaults
my $domain = Sys::Hostname::hostname();
my $http_host = "http://$domain";
## Edition mode
foreach my $param (@Sympa::ConfDef::params) {
next if $param->{obsolete};
unless ($param->{'name'}) {
$title = $language->gettext($param->{'gettext_id'})
if $param->{'gettext_id'};
next;
}
#my $file = $param->{'file'};
my $name = $param->{'name'};
my $query = $param->{'gettext_id'} || '';
$query = $language->gettext($query) if $query;
my $advice = $param->{'gettext_comment'};
$advice = $language->gettext($advice) if $advice;
my $sample = $param->{'sample'};
my $current_value;
#next unless $file;
#if ($file eq 'sympa.conf' or $file eq 'wwsympa.conf') {
# $current_value = $Conf::Conf{$name};
# $current_value = '' unless defined $current_value;
#} else {
# next;
#}
$current_value = $Conf::Conf{$name} // '';
if ($title) {
## write to conf file
push @new_sympa_conf,
sprintf "###\\\\\\\\ %s ////###\n\n", $title;
}
my $new_value = '';
if (exists $user_param{$name}) {
$new_value = $user_param{$name};
}
if ($new_value eq '') {
$new_value = $current_value;
}
undef $title;
## Skip empty parameters
next if $new_value eq '' and !$sample;
my $out = Sympa::Tools::Data::format_config([@Sympa::ConfDef::params],
$curConf, \%newConf, only_changed => 1);
die "Not changed.\n" unless defined $out;
## param is an ARRAY
if (ref($new_value) eq 'ARRAY') {
$new_value = join ',', @{$new_value};
}
#unless ($file eq 'sympa.conf' or $file eq 'wwsympa.conf') {
# warn $language->gettext_sprintf("Incorrect parameter definition: %s\n",
# $file);
#}
if ($new_value eq '') {
next unless $sample;
push @new_sympa_conf,
Sympa::Tools::Text::wrap_text($query, '## ', '## ');
if (defined $advice and length $advice) {
push @new_sympa_conf,
Sympa::Tools::Text::wrap_text($advice, '## ', '## ');
}
push @new_sympa_conf, "# $name\t$sample\n\n";
} else {
push @new_sympa_conf,
Sympa::Tools::Text::wrap_text($query, '## ', '## ');
if (defined $advice and length $advice) {
push @new_sympa_conf,
Sympa::Tools::Text::wrap_text($advice, '## ', '## ');
}
if ($current_value ne $new_value) {
push @new_sympa_conf, "# was $name $current_value\n";
$somechange = 1;
}
push @new_sympa_conf, "$name\t$new_value\n\n";
}
}
if ($somechange) {
my @time = localtime time;
my $date = sprintf '%d%02d%02d%02d%02d%02d',
$time[5] + 1900, $time[4] + 1, @time[3, 2, 1, 0];
my $sympa_conf = Sympa::Constants::CONFIG();
my $date = POSIX::strftime('%Y%m%d%H%M%S', localtime time);
## Keep old config file
unless (rename $sympa_conf, $sympa_conf . '.' . $date) {
......@@ -198,7 +72,7 @@ sub _edit_configuration {
$sympa_conf, $ERRNO);
}
## Write new config file
# Write new config file.
my $umask = umask 037;
my $ofh;
unless (open $ofh, '>', $sympa_conf) {
......@@ -212,14 +86,30 @@ sub _edit_configuration {
chown [getpwnam(Sympa::Constants::USER)]->[2],
[getgrnam(Sympa::Constants::GROUP)]->[2], $sympa_conf;
print $ofh @new_sympa_conf;
print $ofh $out;
close $ofh;
print $language->gettext_sprintf(
"%s have been updated.\nPrevious versions have been saved as %s.\n",
$sympa_conf, "$sympa_conf.$date"
);
return 1;
}
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};
}
1;
......
......@@ -29,7 +29,7 @@ use English qw(-no_match_vars);
use Sympa::ConfDef;
use Sympa::Constants;
use Sympa::Language;
use Sympa::Tools::Text;
use Sympa::Tools::Data;
use parent qw(Sympa::CLI::config);
......@@ -39,18 +39,12 @@ use constant _need_priv => 0;
my $language = Sympa::Language->instance;
# Old name: create_configuration() in sympa_wizard.pl.
sub _run {
my $class = shift;
my $options = shift;
my @argv = @_;
_create_configuration($options);
exit 0;
}
sub _create_configuration {
my $options = shift;
my $conf = $options->{config} // Sympa::Constants::CONFIG();
if (-f $conf) {
......@@ -69,56 +63,12 @@ sub _create_configuration {
}
umask $umask;
my $title;
foreach my $param (@Sympa::ConfDef::params) {
next if $param->{obsolete};
unless ($param->{'name'}) {
$title = $language->gettext($param->{'gettext_id'})
if $param->{'gettext_id'};
next;
}
#next unless $param->{'file'};
#next unless defined $param->{'default'} or defined $param->{'sample'};
if ($title) {
printf $ofh "###\\\\\\\\ %s ////###\n\n", $title;
undef $title;
}
printf $ofh "## %s\n", $param->{'name'};
if ($param->{'gettext_id'}) {
print $ofh Sympa::Tools::Text::wrap_text(
$language->gettext($param->{'gettext_id'}),
'## ', '## ');
}
print $ofh Sympa::Tools::Text::wrap_text(
$language->gettext($param->{'gettext_comment'}),
'## ', '## ')
if $param->{'gettext_comment'};
if (defined $param->{'sample'}) {
printf $ofh '## ' . $language->gettext('Example: ') . "%s\t%s\n",
$param->{'name'}, $param->{'sample'};
}
if (defined $param->{'default'}) {
printf $ofh "#%s\t%s\n", $param->{'name'}, $param->{'default'};
} elsif ($param->{'optional'}) {
printf $ofh "#%s\t\n", $param->{'name'};
} else {
printf $ofh "#%s\t%s\n", $param->{'name'},
$language->gettext("(You must define this parameter)");
}
print $ofh "\n";
}
print $ofh Sympa::Tools::Data::format_config([@Sympa::ConfDef::params]);
close $ofh;
warn $language->gettext_sprintf('The file %s has been created', $conf)
print $language->gettext_sprintf('The file %s has been created', $conf)
. "\n";
return 1;
}
1;
......@@ -132,7 +82,7 @@ sympa-config-create - Create configuration file
=head1 SYNOPSIS
C<sympa config create> [ C<--config=>I</path/to/new/sympa.conf> ]
C<sympa config create> S<[ C<--config=>I</path/to/new/sympa.conf> ]>
=head1 DESCRIPTION
......
......@@ -8,8 +8,8 @@
# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
# 2006, 2007, 2008, 2009, 2010, 2011 Comite Reseau des Universites
# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 GIP RENATER
# Copyright 2018 The Sympa Community. See the AUTHORS.md file at the
# top-level directory of this distribution and at
# Copyright 2018, 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
......@@ -35,6 +35,7 @@ use POSIX qw();
use XML::LibXML qw();
BEGIN { eval 'use Clone qw()'; }
use Sympa::Language;
use Sympa::Tools::Text;
## This applies recursively to a data structure
......@@ -503,4 +504,109 @@ sub encode_custom_attribute {
return $XMLstr;
}
my $language = Sympa::Language->instance;
# Old name: edit_configuragion() in sympa_wizard.pl.
sub format_config {
my $params = shift;
my $curConf = shift if ref $_[0];
my $newConf = shift if ref $_[0];
my %options = @_;
my $out = '';
my $changed = 0;
my $title;
foreach my $param (@$params) {
next if $param->{obsolete};
unless ($param->{name}) {
$title = $language->gettext($param->{gettext_id})
if $param->{gettext_id};
next;
}
$out .=
_format_config_ent($param, $curConf, $newConf, \$title, \$changed,
%options);
}
return ($options{only_changed} and $newConf and not $changed)
? undef
: $out;
}
sub _format_config_ent {
my $param = shift;
my $curConf = shift;
my $newConf = shift;
my $title_ref = shift;
my $changed_ref = shift;
my %options = @_;
my $name = $param->{name};
my $value;
if ($curConf and exists $curConf->{$name}) {
my $cur = $curConf->{$name};
$cur = join ',', @$cur if ref $cur eq 'ARRAY';
if ($newConf and exists $newConf->{$name}) {
$value = $newConf->{$name};
$$changed_ref++ unless $cur eq $value;
} else {
$value = $cur;
}
} elsif ($newConf and exists $newConf->{$name}) {
$value = $newConf->{$name};
$$changed_ref++;
}
my @filter = @{$options{filter} // []};
@filter = qw(explicit mandatory) unless @filter;
my %specs = (
explicit => length($value // ''),
omittable => (defined $param->{default}),
optional => $param->{optional},
mandatory => not(defined $param->{default} or $param->{optional}),
);
return '' unless grep { $specs{$_} } @filter;
my $out = '';
$out .= sprintf "###\\\\\\\\ %s ////###\n\n", $$title_ref
if $$title_ref;
$out .= sprintf "## %s\n", $name;
$out .= Sympa::Tools::Text::wrap_text(
$language->gettext($param->{gettext_id}),
'## ', '## ')
if $param->{gettext_id};
$out .= Sympa::Tools::Text::wrap_text(
$language->gettext($param->{gettext_comment}),
'## ', '## ')
if $param->{gettext_comment};
$out .= sprintf '## ' . $language->gettext('Example: ') . "%s\t%s\n",
$name, $param->{sample}
if defined $param->{sample};
if ($specs{explicit}) {
$out .= sprintf "%s\t%s\n", $name, $value;
} elsif ($specs{omittable}) {
$out .= sprintf "#%s\t%s\n", $name, $param->{default};
} elsif ($specs{optional}) {
;
} else {
$out .= sprintf "#%s\t%s\n", $name,
$language->gettext("(You must define this parameter)");
}
$out .= "\n";
undef $$title_ref;
return $out;
}
1;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment