diff --git a/App-Nrepo/lib/App/Nrepo.pm b/App-Nrepo/lib/App/Nrepo.pm index 6f211f5..1bc6bae 100644 --- a/App-Nrepo/lib/App/Nrepo.pm +++ b/App-Nrepo/lib/App/Nrepo.pm @@ -25,13 +25,11 @@ sub go { $self->logger->log_and_croak(level => 'error', message => 'ERROR: action not supplied.') unless $action; - if ($action eq 'add-file'){ - $self->add_file(@o) - } - elsif($action eq 'del-file'){ - $self->del_file(@o) - } - elsif($action eq 'clean'){ + my %actions; + + $actions{'add-file'} = sub { $self->add_file(@o) }; + $actions{'del-file'} = sub { $self->del_file(@o) }; + $actions{'clean'} = sub { %options = validate( @o, { @@ -49,12 +47,10 @@ sub go { else { $self->clean(%options); } - } - elsif($action eq 'init'){$self->init(@o)} - elsif($action eq 'list'){ - $self->list(); - } - elsif($action eq 'mirror'){ + }; + $actions{'init'} = sub { $self->init(@o) }; + $actions{'list'} = sub { $self->list() }; + $actions{'mirror'} = sub { %options = validate( @o, { @@ -74,8 +70,8 @@ sub go { else { $self->mirror(%options); } - } - elsif($action eq 'tag'){ + }; + $actions{'tag'} = sub { %options = validate( @o, { @@ -87,6 +83,10 @@ sub go { }, ); $self->tag(%options); + }; + + if ($actions{$action}) { + $actions{$action}->(); } else { $self->logger->log_and_croak(level => 'error', message => "ERROR: ${action} not supported."); diff --git a/App-Nrepo/lib/App/Nrepo/Logger.pm b/App-Nrepo/lib/App/Nrepo/Logger.pm new file mode 100644 index 0000000..4340dca --- /dev/null +++ b/App-Nrepo/lib/App/Nrepo/Logger.pm @@ -0,0 +1,56 @@ +#!/bin/false + +use strict; +use warnings; + +package App::Nrepo::Logger; + +# VERSION + +my $logger; + +sub load { + die "Already loaded!\n" if $logger; + my $package = shift; + $logger = shift; + return 1 +} + +sub new { + return $logger; +} + +1; + +__END__ + +=head1 NAME + + App::Nrepo::Logger + +=head1 SYNOPSIS + +In bin/yourapp.pl + + use App::Nrepo::Logger; + # do stuff + App::Nrepo::Logger->load($logobject); + +Then in your lib/YourApp/Base.pm + + use Moo; + use App::Nrepo::Logger; + + has 'logger' => ( + default => sub { App::Nrepo::Logger->new() }, + ); + +=head1 METHODS + +=head2 load($obj) + +Saves $obj for later + +=head2 new() + +Returns $obj every time