Ado

 view release on metacpan or  search on metacpan

lib/Ado/Command.pm  view on Meta::CPAN

    my $class_file = Mojo::Util::class_to_path(ref($_[0]) || $_[0]);
    my ($root) = $INC{$class_file} =~ m{^(.+?)/[^/]+/$class_file$}x;
    return $root;
};

sub _get_command_config {
    my ($self) = @_;
    state $app = $self->app;
    my $name = $self->name;

    #first try (global config) !!!autovivification
    my $config = $app->config->{commands} && $app->config->{commands}->{$name};
    $config && return $config;

    #second try (command specific configuration file)
    my $conf_file = $app->home->rel_file('/etc/commands/' . decamelize($name) . '.conf');
    if ($config = eval { Mojolicious::Plugin::Config->new->load($conf_file, {}, $app) }) {
        return $config;
    }
    else {
        $app->log->warn(
            "Could not load configuration from file $conf_file! " . decode('UTF-8', $@));
        return {};
    }
}

sub config {
    my ($self, $key) = @_;
    state $config = $self->_get_command_config();
    return $key
      ? $config->{$key}
      : $config;
}

#a default run method
sub run {
    my ($self, @args) = @_;

    #0. initialize
    $self = ref($self) ? $self : $self->new();
    $self->init(@args) || return;

    #1. run
    my $action = $self->args->{do};
    if ($action && $self->can($action)) {
        $self->$action();
    }
    else {
        Carp::croak
          "Command action '$action' was not found! Please implement it! Supported actions should be: "
          . join(', ', @{$self->config->{actions} || []});
    }

    return;
}
1;


=pod

=encoding utf8

=head1 NAME

Ado::Command - Ado namespace for Mojo commands!

=head1 DESCRIPTION

Ado::Command is the base class for eventual functionality that we
can run directly from the commandline or from controllers.
In this class we can put common functionality shared among all the commands.

=head1 ATTRIBUTES

L<Ado::Command> inherits all attributes from L<Mojolicious::Command> and implements the following new ones.

=head2 args

Returns a hash-reference containing all arguments passed to the command
on the command-line or to the method L</run>.
The keys are the long variants of the possible commandline arguments
altough you may have used short variants.

    #if you passed -s or --something
    $self->args->{something} #foo

Also, please note that some options turn out to be "eaten-up" by
L<Mojolicious::Commands>. So, do not use them in your commands and consider them
reserved. The known options are 'C<home=s>' and 'C<m|mode=s>'.
L<Ado::Command::generate::apache2htaccess> worked around this limitation by using
upper case for a short variant of its options - 'C<M|modules=s@>'.


=head2 name

The name of your command - C<(ref $self) =~ /(\w+)$/;>.

=head2 home

Returns current Ado::Command::foo home.

=head1 METHODS

L<Ado::Command> inherits all methods from L<Mojolicious::Command> and implements the following new ones.

=head2 init

Should be implemented by the inheriting command.

Should get options from the commandline and populate C<$self-E<gt>args>.
Must return C<$self>.

=head2 run

A default C<$command-E<gt>run(@args)> method for all Ado::Command commands.
This is the entry point to your mini application.
Looks for subcommands/actions which are looked up in
the C<--do> commands line argument and executed.
Dies with an error message advising you to implement the subcommand
if it is not found in  C<$self-E<gt>config-E<gt>{actions}>.
Override it if you want specific behavior.



( run in 2.008 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )