First Cli : get base keys only for now

This commit is contained in:
Xavier Guimard 2016-01-01 19:55:48 +00:00
parent 2b35cb6e04
commit 2c512dbcfa
3 changed files with 96 additions and 10 deletions

View File

@ -0,0 +1,75 @@
package Lemonldap::NG::Manager::Cli;
use strict;
use Mouse;
use 5.14.0;
use Data::Dumper;
extends('Lemonldap::NG::Manager::Cli::Lib');
has cfgNum => (
is => 'rw',
isa => 'Int',
trigger => sub {
$_[0]->{req} =
Lemonldap::NG::Manager::Cli::Request->new(
cfgNum => $_[0]->{cfgNum} );
}
);
has req => ( is => 'ro' );
sub get {
my ( $self, @values ) = @_;
$self->cfgNum( $self->lastCfg ) unless ( $self->cfgNum );
die 'get requires at least one key' unless (@values);
L: foreach my $key (@values) {
unless ( $key =~ /^\w+(?:\/[\w\.]+)*$/ ) {
warn "Unknown key $key";
next L;
}
my $value = $self->mgr->getConfKey( $self->req, $key );
#$value = Dumper($value);
#$value =~ s/\$VAR1/$key/;
#print $value;
if(ref $value eq 'HASH') {
print "$key has the following keys:\n";
print " $_\n" foreach(sort keys %$value);
}
else {
print "$key = $value\n";
}
}
}
sub lastCfg {
my ($self) = @_;
return $self->jsonResponse('/confs/latest')->{cfgNum};
}
sub run {
my $self = shift;
unless (@_) {
die 'nothing to do, aborting';
}
my $action = shift;
unless ( $action =~ /^(?:get|set)$/ ) {
die "unknown action $action";
}
$self->$action(@_);
}
package Lemonldap::NG::Manager::Cli::Request;
use Mouse;
has cfgNum => ( is => 'rw' );
has error => ( is => 'rw' );
sub params {
my ( $self, $key ) = @_;
return $self->{$key};
}
1;

View File

@ -4,18 +4,20 @@ use JSON::MaybeXS;
use Mouse;
use Lemonldap::NG::Manager;
has iniFile => ( is => 'ro', isa => 'Str', required => 1 );
has iniFile => ( is => 'ro', isa => 'Str' );
has mgr => ( is => 'ro', isa => 'Lemonldap::NG::Manager' );
has app => (
is => 'ro',
isa => 'CodeRef',
builder => sub {
return Lemonldap::NG::Manager->run(
{
configStorage => { confFile => $_[0]->{iniFile} },
protection => 'none',
}
);
is => 'ro',
isa => 'CodeRef',
builder => sub {
my $args = { protection => 'none' };
$args->{configStorage} = { confFile => $_[0]->{iniFile} }
if ( $_[0]->{iniFile} );
$_[0]->{mgr} = Lemonldap::NG::Manager->new($args);
$_[0]->{mgr}->init($args);
return $_[0]->{mgr}->run();
}
);

View File

@ -0,0 +1,9 @@
#!/usr/bin/env perl
use warnings;
use strict;
use Lemonldap::NG::Manager::Cli;
my $cli = Lemonldap::NG::Manager::Cli->new(iniFile=>'t/lemonldap-ng.ini');
$cli->run(@ARGV);