From c964cec646419193efa59096d72270c8363ab875 Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Sat, 6 Feb 2016 12:22:10 +0000 Subject: [PATCH] Finish cli (Closes: #921) --- Makefile | 10 +-- lemonldap-ng-common/MANIFEST | 1 + .../lib/Lemonldap/NG/Common/Cli.pm | 77 +++++++++++++++++++ lemonldap-ng-common/scripts/lemonldap-ng-cli | 20 ++++- .../t/14-bad-changes-in-conf.t | 1 - 5 files changed, 97 insertions(+), 12 deletions(-) create mode 100644 lemonldap-ng-common/lib/Lemonldap/NG/Common/Cli.pm diff --git a/Makefile b/Makefile index 96fef7be1..3b2482033 100644 --- a/Makefile +++ b/Makefile @@ -427,21 +427,17 @@ install_bin: install_conf_dir ${SRCCOMMONDIR}/scripts/lmMigrateConfFiles2ini \ ${SRCCOMMONDIR}/scripts/rotateOidcKeys \ ${SRCMANAGERDIR}/scripts/lmConfigEditor \ - ${SRCMANAGERDIR}/scripts/lemonldap-ng-cli \ + ${SRCCOMMONDIR}/scripts/lemonldap-ng-cli \ $(RBINDIR) @if [ ! "$(APACHEUSER)" ]; then \ $(PERL) -i -pe 's#__APACHEUSER__#nobody#g;' $(RBINDIR)/lmConfigEditor; \ - $(PERL) -i -pe 's#__APACHEUSER__#nobody#g;' $(RBINDIR)/lemonldap-ng-cli; \ else \ $(PERL) -i -pe 's#__APACHEUSER__#$(APACHEUSER)#g;' $(RBINDIR)/lmConfigEditor; \ - $(PERL) -i -pe 's#__APACHEUSER__#$(APACHEUSER)#g;' $(RBINDIR)/lemonldap-ng-cli; \ fi @if [ ! "$(APACHEGROUP)" ]; then \ $(PERL) -i -pe 's#__APACHEGROUP__#nobody#g;' $(RBINDIR)/lmConfigEditor; \ - $(PERL) -i -pe 's#__APACHEGROUP__#nobody#g;' $(RBINDIR)/lemonldap-ng-cli; \ else \ $(PERL) -i -pe 's#__APACHEGROUP__#$(APACHEGROUP)#g;' $(RBINDIR)/lmConfigEditor; \ - $(PERL) -i -pe 's#__APACHEGROUP__#$(APACHEGROUP)#g;' $(RBINDIR)/lemonldap-ng-cli; \ fi @chmod +x $(RBINDIR)/* @@ -894,7 +890,7 @@ debian-diff: @$(DIFF) $(SRCMANAGERDIR)/site/static $(DIFFPREFIX)/var/lib/lemonldap-ng/manager/static ||true @$(DIFF) $(SRCMANAGERDIR)/site/templates $(DIFFPREFIX)/var/lib/lemonldap-ng/manager/templates ||true @$(DIFF) --ignore-matching-lines='set.*get.*\[2\]' $(SRCMANAGERDIR)/scripts/lmConfigEditor $(DIFFPREFIX)/usr/share/lemonldap-ng/bin/lmConfigEditor ||true - @$(DIFF) --ignore-matching-lines='giveUpPrivileges' $(SRCMANAGERDIR)/scripts/lemonldap-ng-cli $(DIFFPREFIX)/usr/share/lemonldap-ng/bin/lemonldap-ng-cli ||true + @$(DIFF) --ignore-matching-lines='giveUpPrivileges' $(SRCCOMMONDIR)/scripts/lemonldap-ng-cli $(DIFFPREFIX)/usr/share/lemonldap-ng/bin/lemonldap-ng-cli ||true default-diff: @# Portal @@ -924,7 +920,7 @@ default-diff: @$(DIFF) $(SRCMANAGERDIR)/site/static $(LMPREFIX)/htdocs/manager/static ||true @$(DIFF) $(SRCMANAGERDIR)/site/templates $(LMPREFIX)/htdocs/manager/templates ||true @$(DIFF) --ignore-matching-lines='set.*get.*\[2\]' $(SRCMANAGERDIR)/scripts/lmConfigEditor $(LMPREFIX)/bin/lmConfigEditor ||true - @$(DIFF) --ignore-matching-lines='giveUpPrivileges' $(SRCMANAGERDIR)/scripts/lemonldap-ng-cli $(LMPREFIX)/bin/lemonldap-ng-cli ||true + @$(DIFF) --ignore-matching-lines='giveUpPrivileges' $(SRCCOMMONDIR)/scripts/lemonldap-ng-cli $(LMPREFIX)/bin/lemonldap-ng-cli ||true test-diff: @for file in `find lemonldap-ng-*/lib -type f`; do \ diff --git a/lemonldap-ng-common/MANIFEST b/lemonldap-ng-common/MANIFEST index 4a0498397..57d799d49 100644 --- a/lemonldap-ng-common/MANIFEST +++ b/lemonldap-ng-common/MANIFEST @@ -11,6 +11,7 @@ lib/Lemonldap/NG/Common/Captcha.pm lib/Lemonldap/NG/Common/CGI.pm lib/Lemonldap/NG/Common/CGI/SOAPServer.pm lib/Lemonldap/NG/Common/CGI/SOAPService.pm +lib/Lemonldap/NG/Common/Cli.pm lib/Lemonldap/NG/Common/Conf.pm lib/Lemonldap/NG/Common/Conf/_DBI.pm lib/Lemonldap/NG/Common/Conf/CDBI.pm diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Cli.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Cli.pm new file mode 100644 index 000000000..ebe69d242 --- /dev/null +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Cli.pm @@ -0,0 +1,77 @@ +package Lemonldap::NG::Common::Cli; + +use strict; +use Mouse; +use Data::Dumper; +use Lemonldap::NG::Common::Conf; + +has confAccess => ( + is => 'rw', + isa => 'HashRef', + builder => sub { + my $res = + Lemonldap::NG::Common::Conf->new( { confFile => $_[0]->{iniFile} } ); + die $Lemonldap::NG::Common::Conf::msg unless ($res); + return $res; + }, +); + +has cfgNum => ( + is => 'rw', + isa => 'Int', +); + +sub info { + my ($self) = @_; + my $conf = $self->confAccess( { cfgNum => $self->cfgNum, raw => 1 } ) + or die $Lemonldap::NG::Common::Conf::msg; + print qq{ +Num : $conf->{cfgNum} +Author : $conf->{cfgAuthor} +Author IP: $conf->{cfgAuthorIP} +Date : $conf->{cfgDate} +Log : $conf->{cfgLog} +}; +} + +sub updateCache { + my $self = shift; + my $conf = $self->confAccess( { noCache => 1, raw => 1 } ); + 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 + my $args; + 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"; + } + $self->$action(@_); +} + +1; diff --git a/lemonldap-ng-common/scripts/lemonldap-ng-cli b/lemonldap-ng-common/scripts/lemonldap-ng-cli index ae50a4ff9..da3ce7da3 100755 --- a/lemonldap-ng-common/scripts/lemonldap-ng-cli +++ b/lemonldap-ng-common/scripts/lemonldap-ng-cli @@ -3,14 +3,25 @@ use warnings; use strict; -if ( $ARGV[0] =~ /^(?:[gs]et|(?:add|del)Key)$/ ) { +my $action; + +for ( my $i = 0 ; $i < @ARGV ; $i++ ) { + if ( $ARGV[$i] =~ /^-/ ) { + $i++; + next; + } + $action = $ARGV[$i]; + last; +} + +if ( $action =~ /^(?:[gs]et|(?:add|del)Key)$/ ) { eval { require Lemonldap::NG::Manager::Cli; }; die 'Manager libraries not available, aborting' if ($@); Lemonldap::NG::Manager::Cli->run(@ARGV); } -elsif ( $ARGV[0] =~ /^(?:info|update-cache)$/ ) { +elsif ( $action =~ /^(?:info|update-cache)$/ ) { eval { require Lemonldap::NG::Common::Cli; }; - die 'Lemonldap::NG common libraries not available, aborting' if ($@); + die "Lemonldap::NG common libraries not available, aborting ($@)" if ($@); Lemonldap::NG::Common::Cli->run(@ARGV); } else { @@ -29,6 +40,7 @@ Available actions: - addKey : add or set a subkey in a parameter - delKey : delete subkey of a parameter -See Lemonldap::NG::Common::Cli(3) or Lemonldap::NG::Manager::CLi(3) for more}; +See Lemonldap::NG::Common::Cli(3) or Lemonldap::NG::Manager::CLi(3) for more +}; } diff --git a/lemonldap-ng-manager/t/14-bad-changes-in-conf.t b/lemonldap-ng-manager/t/14-bad-changes-in-conf.t index 85f0f7179..d05d83642 100644 --- a/lemonldap-ng-manager/t/14-bad-changes-in-conf.t +++ b/lemonldap-ng-manager/t/14-bad-changes-in-conf.t @@ -26,7 +26,6 @@ ok( $resBody->{result} == 0, "JSON response contains \"result:0\"" ) ok( @{ $resBody->{details}->{__errors__} } == 1, '1 error detected' ); count(5); -print STDERR Dumper($resBody); done_testing( count() );