Cli.pm 1.97 KB
Newer Older
Xavier Guimard's avatar
Xavier Guimard committed
1 2 3 4 5 6
package Lemonldap::NG::Common::Cli;

use strict;
use Mouse;
use Lemonldap::NG::Common::Conf;

Xavier Guimard's avatar
Xavier Guimard committed
7 8
our $VERSION = '2.0.0';

Xavier Guimard's avatar
Xavier Guimard committed
9 10 11
has confAccess => (
    is      => 'rw',
    builder => sub {
Xavier Guimard's avatar
Xavier Guimard committed
12
        my $res = Lemonldap::NG::Common::Conf->new( { (
13 14 15 16 17 18
                    ref $_[0] && $_[0]->{iniFile}
                    ? ( confFile => $_[0]->{iniFile} )
                    : ()
                )
            }
        );
Xavier Guimard's avatar
Xavier Guimard committed
19 20 21 22 23 24 25 26 27 28 29 30
        die $Lemonldap::NG::Common::Conf::msg unless ($res);
        return $res;
    },
);

has cfgNum => (
    is  => 'rw',
    isa => 'Int',
);

sub info {
    my ($self) = @_;
31 32
    my $conf =
      $self->confAccess->getConf( { cfgNum => $self->cfgNum, raw => 1 } )
Xavier Guimard's avatar
Xavier Guimard committed
33 34 35 36 37
      or die $Lemonldap::NG::Common::Conf::msg;
    print qq{
Num      : $conf->{cfgNum}
Author   : $conf->{cfgAuthor}
Author IP: $conf->{cfgAuthorIP}
38
Date     : } . localtime( $conf->{cfgDate} ) . qq{
39
Version  : $conf->{cfgVersion}
Xavier Guimard's avatar
Xavier Guimard committed
40 41 42 43 44 45
Log      : $conf->{cfgLog}
};
}

sub updateCache {
    my $self = shift;
Xavier Guimard's avatar
Xavier Guimard committed
46
    my $conf = $self->confAccess->getConf( { noCache => 2 } );
Xavier Guimard's avatar
Xavier Guimard committed
47 48 49 50 51 52 53 54 55
    die "Must not be launched as root" unless ($>);
    print STDERR
      qq{Cache updated to configuration $conf->{cfgNum} for user $>\n};
}

sub run {
    my $self = shift;

    # Options simply call corresponding accessor
56
    my $args = {};
Xavier Guimard's avatar
Xavier Guimard committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    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;
    unless ( $action =~ /^(?:info|update-cache)$/ ) {
        die "unknown action $action. Only info or update are accepted";
    }
81
    $action =~ s/\-([a-z])/uc($1)/e;
Xavier Guimard's avatar
Xavier Guimard committed
82 83 84 85
    $self->$action(@_);
}

1;