blib/lib/Lemonldap/NG/Common/Cli.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 139 CC 13 SCAR 36.9
CC
13
Cov
0%
run
90.0
testEmail
12.0
info
6.0
CRAP 40.1
1 package Lemonldap::NG::Common::Cli;
2  
3 use strict;
4 use JSON;
5 use Mouse;
6 use Lemonldap::NG::Common::Conf;
7 use Lemonldap::NG::Common::EmailTransport;
8  
9 use constant booleanOptions => qr/^(?:json)$/;
10  
11 our $VERSION = '2.22.0';
12  
13 extends 'Lemonldap::NG::Common::PSGI::Cli::Lib';
14  
15 has confAccess => (
16 is => 'rw',
17 builder => sub {
18 my $res = Lemonldap::NG::Common::Conf->new( { (
19 ref $_[0] && $_[0]->{iniFile}
20 ? ( confFile => $_[0]->{iniFile} )
21 : ()
22 )
23 }
24 );
25 die $Lemonldap::NG::Common::Conf::msg unless ($res);
26 return $res;
27 },
28 );
29  
30 has cfgNum => (
31 is => 'rw',
32 isa => 'Int',
33 );
34  
35 has json => ( is => 'rw' );
36  
37 sub info {
38 my ($self) = @_;
39 my $conf =
40 $self->confAccess->getConf( { cfgNum => $self->cfgNum, raw => 1 } )
41 or die $Lemonldap::NG::Common::Conf::msg;
42 $conf->{cfgAuthorIP} ||= "No IP provided";
43 $conf->{cfgDate} ||= 0;
44 $conf->{cfgLog} ||= "No log provided";
45 if ( $self->json ) {
46 print JSON->new->pretty->allow_nonref->encode( {
47 num => $conf->{cfgNum},
48 author => $conf->{cfgAuthor},
49 authorIP => $conf->{cfgAuthorIP},
50 date => $conf->{cfgDate},
51 version => $conf->{cfgVersion},
52 log => $conf->{cfgLog},
53 }
54 );
55 }
56 else {
57 print qq{
58 Num : $conf->{cfgNum}
59 Author : $conf->{cfgAuthor}
60 Author IP: $conf->{cfgAuthorIP}
61 Date : } . localtime( $conf->{cfgDate} ) . qq{
62 Version : $conf->{cfgVersion}
63 Log : $conf->{cfgLog}
64 };
65 }
66 }
67  
68 sub updateCache {
69 my $self = shift;
70 my $conf = $self->confAccess->getConf( { noCache => 2 } );
71 die "Must not be launched as root" unless ($>);
72 print STDERR
73 qq{Cache updated to configuration $conf->{cfgNum} for user $>\n};
74 }
75  
76 sub testEmail {
77 my $self = shift;
78 my $dest = shift;
79 die "Must specify destination" unless ($dest);
80 my $conf = $self->confAccess->getConf();
81 eval {
82 Lemonldap::NG::Common::EmailTransport::sendTestMail( $conf, $dest );
83 };
84 die $@ if $@;
85 print STDERR "Test email successfully sent to $dest\n";
86 }
87  
88 sub run {
89 my $self = shift;
90  
91 # Options simply call corresponding accessor
92 my $args = {};
93 my @ARGS2;
94 for ( my $i = 0 ; $i < @_ ; $i++ ) {
95 if ( $_[$i] =~ s/^--?(\w)/$1/ ) {
96 my $k = $_[$i];
97 my $bool = ( $k =~ booleanOptions );
98 my $v = $bool ? 1 : $_[ ++$i ];
99 if ( ref $self ) {
100 eval { $self->$k($v) };
101 if ($@) {
102 die "Unknown option -$k or bad value ($@)";
103 }
104 }
105 else {
106 $args->{$k} = $v;
107 }
108 }
109 else {
110 push @ARGS2, $_[$i];
111 }
112 }
113 @_ = @ARGS2;
114 unless ( ref $self ) {
115 $self = $self->new($args);
116 }
117 unless (@_) {
118 die 'nothing to do, aborting';
119 }
120 $self->confAccess()->lastCfg() unless ( $self->cfgNum );
121 my $action = shift;
122 unless ( $action =~ /^(?:info|update-cache|test-email)$/ ) {
123 die "unknown action $action";
124 }
125 $action =~ s/\-([a-z])/uc($1)/e;
126 $self->$action(@_);
127 }
128  
129 1;