lemonldap-ng/lemonldap-ng-common/lib/Lemonldap/NG/Common/Cli.pm

108 lines
2.6 KiB
Perl
Raw Normal View History

2016-02-06 13:22:10 +01:00
package Lemonldap::NG::Common::Cli;
use strict;
use Mouse;
use Lemonldap::NG::Common::Conf;
2020-09-16 16:56:06 +02:00
use Lemonldap::NG::Common::EmailTransport;
2016-02-06 13:22:10 +01:00
2020-01-14 22:46:43 +01:00
extends 'Lemonldap::NG::Common::PSGI::Cli::Lib';
2017-02-28 21:53:19 +01:00
2019-02-12 18:21:38 +01:00
our $VERSION = '2.1.0';
2017-02-28 21:53:19 +01:00
2016-02-06 13:22:10 +01:00
has confAccess => (
is => 'rw',
builder => sub {
2019-02-07 09:27:56 +01:00
my $res = Lemonldap::NG::Common::Conf->new( { (
2016-02-25 09:40:25 +01:00
ref $_[0] && $_[0]->{iniFile}
? ( confFile => $_[0]->{iniFile} )
: ()
)
}
);
2016-02-06 13:22:10 +01:00
die $Lemonldap::NG::Common::Conf::msg unless ($res);
return $res;
},
);
has cfgNum => (
is => 'rw',
isa => 'Int',
);
sub info {
my ($self) = @_;
2016-02-25 09:40:25 +01:00
my $conf =
$self->confAccess->getConf( { cfgNum => $self->cfgNum, raw => 1 } )
2016-02-06 13:22:10 +01:00
or die $Lemonldap::NG::Common::Conf::msg;
$conf->{cfgAuthorIP} ||= "No IP provided";
$conf->{cfgDate} ||= 0;
$conf->{cfgLog} ||= "No log provided";
2016-02-06 13:22:10 +01:00
print qq{
Num : $conf->{cfgNum}
Author : $conf->{cfgAuthor}
Author IP: $conf->{cfgAuthorIP}
2016-02-25 09:40:25 +01:00
Date : } . localtime( $conf->{cfgDate} ) . qq{
Version : $conf->{cfgVersion}
2016-02-06 13:22:10 +01:00
Log : $conf->{cfgLog}
};
}
sub updateCache {
my $self = shift;
2018-03-14 06:53:53 +01:00
my $conf = $self->confAccess->getConf( { noCache => 2 } );
2016-02-06 13:22:10 +01:00
die "Must not be launched as root" unless ($>);
print STDERR
qq{Cache updated to configuration $conf->{cfgNum} for user $>\n};
}
2020-09-16 16:56:06 +02:00
sub testEmail {
my $self = shift;
my $dest = shift;
die "Must specify destination" unless ($dest);
my $conf = $self->confAccess->getConf();
eval {
Lemonldap::NG::Common::EmailTransport::sendTestMail( $conf, $dest );
};
my $error = $@;
if ($error) {
die $error;
} else {
print STDERR "Test email successfully sent to $dest\n";
}
}
2016-02-06 13:22:10 +01:00
sub run {
my $self = shift;
# Options simply call corresponding accessor
2016-02-25 09:40:25 +01:00
my $args = {};
2016-02-06 13:22:10 +01:00
while ( $_[0] =~ s/^--?// ) {
my $k = shift;
my $v = shift;
if ( ref $self ) {
eval { $self->$k($v) };
if ($@) {
die "Unknown option -$k or bad value ($@)";
}
}
else {
$args->{$k} = $v;
}
}
unless ( ref $self ) {
$self = $self->new($args);
}
unless (@_) {
die 'nothing to do, aborting';
}
$self->confAccess()->lastCfg() unless ( $self->cfgNum );
my $action = shift;
2020-09-16 16:56:06 +02:00
unless ( $action =~ /^(?:info|update-cache|test-email)$/ ) {
2016-02-06 13:22:10 +01:00
die "unknown action $action. Only info or update are accepted";
}
2016-02-25 13:56:06 +01:00
$action =~ s/\-([a-z])/uc($1)/e;
2016-02-06 13:22:10 +01:00
$self->$action(@_);
}
1;