App-BCSSH

 view release on metacpan or  search on metacpan

lib/App/BCSSH.pm  view on Meta::CPAN

package App::BCSSH;
use strictures 1;

our $VERSION = '0.002002';
$VERSION = eval $VERSION;

use Try::Tiny;
use Module::Runtime qw(require_module);
use App::BCSSH::Util qw(command_to_package rc_dir);
use Module::Find ();

sub run_script { exit($_[0]->new(@ARGV)->run ? 0 : 1) }

sub new { bless { args => [@_[1..$#_]] }, $_[0] }

sub run {
    my $self = shift;
    my @args = @{ $self->{args} };
    $self->load_plugins(rc_dir);
    my $command = shift @args
        or die "Command required.\n" . $self->_commands_msg;
    $command =~ /^[a-z]+(?:-[a-z]+)+$/
        or $self->invalid_command($command);
    return try {
        my $pack = command_to_package($command);
        require_module($pack);
        return($pack->can('new') ? $pack->new(@args)->run : $pack->run(@args));
    }
    catch {
        if (/Can't locate .+? in \@INC/ && tr/\n// < 2 ) {
            $self->invalid_command($command);
        }
        else {
            die $_;
        }
    };
}

sub invalid_command {
    my $self = shift;
    my $command = shift;
    die "Invalid command $command!\n" . $self->_commands_msg;
}

sub _commands_msg {
    require App::BCSSH::Command::commands;
    App::BCSSH::Command::commands->new->commands_message;
}

sub load_plugins {
    my $self = shift;
    my $dir = shift;
    return unless -d $dir;
    require File::Find;
    File::Find::find({ no_chdir => 1, wanted => sub {
        return unless -f;
        return unless /\.pm$/;
        require $_;
    }}, $dir);
}

1;

__END__

=head1 NAME



( run in 0.636 second using v1.01-cache-2.11-cpan-39bf76dae61 )