File Coverage

blib/lib/Ado/Command.pm
Criterion Covered Total %
statement 6 33 18.1
branch 0 14 0.0
condition 0 11 0.0
subroutine 2 6 33.3
pod 3 3 100.0
total 11 67 16.4


line stmt bran cond sub pod time code
1             package Ado::Command;
2 2     2   915 use Mojo::Base 'Mojolicious::Command';
  2         5  
  2         10  
3 2     2   250 use Mojo::Util qw(decamelize decode);
  2         3  
  2         1060  
4              
5             has args => sub { {} };
6             has name => sub { (ref $_[0]) =~ /(\w+)$/ && return $1 };
7              
8             #default initialization for each Ado command
9             sub init {
10 0     0 1   my $self = shift;
11 0   0       $self->args->{do} ||= $self->name;
12 0   0       $self->config->{actions} ||= [$self->name];
13 0           return 1;
14             }
15              
16             # Current Ado::Command home
17             has home => sub {
18              
19             my $class_file = Mojo::Util::class_to_path(ref($_[0]) || $_[0]);
20             my ($root) = $INC{$class_file} =~ m{^(.+?)/[^/]+/$class_file$}x;
21             return $root;
22             };
23              
24             sub _get_command_config {
25 0     0     my ($self) = @_;
26 0           state $app = $self->app;
27 0           my $name = $self->name;
28              
29             #first try (global config) !!!autovivification
30 0   0       my $config = $app->config->{commands} && $app->config->{commands}->{$name};
31 0 0         $config && return $config;
32              
33             #second try (command specific configuration file)
34 0           my $conf_file = $app->home->rel_file('/etc/commands/' . decamelize($name) . '.conf');
35 0 0         if ($config = eval { Mojolicious::Plugin::Config->new->load($conf_file, {}, $app) }) {
  0            
36 0           return $config;
37             }
38             else {
39 0           $app->log->warn(
40             "Could not load configuration from file $conf_file! " . decode('UTF-8', $@));
41 0           return {};
42             }
43             }
44              
45             sub config {
46 0     0 1   my ($self, $key) = @_;
47 0           state $config = $self->_get_command_config();
48             return $key
49 0 0         ? $config->{$key}
50             : $config;
51             }
52              
53             #a default run method
54             sub run {
55 0     0 1   my ($self, @args) = @_;
56              
57             #0. initialize
58 0 0         $self = ref($self) ? $self : $self->new();
59 0 0         $self->init(@args) || return;
60              
61             #1. run
62 0           my $action = $self->args->{do};
63 0 0 0       if ($action && $self->can($action)) {
64 0           $self->$action();
65             }
66             else {
67             Carp::croak
68             "Command action '$action' was not found! Please implement it! Supported actions should be: "
69 0 0         . join(', ', @{$self->config->{actions} || []});
  0            
70             }
71              
72 0           return;
73             }
74             1;
75              
76              
77             =pod
78              
79             =encoding utf8
80              
81             =head1 NAME
82              
83             Ado::Command - Ado namespace for Mojo commands!
84              
85             =head1 DESCRIPTION
86              
87             Ado::Command is the base class for eventual functionality that we
88             can run directly from the commandline or from controllers.
89             In this class we can put common functionality shared among all the commands.
90              
91             =head1 ATTRIBUTES
92              
93             L inherits all attributes from L and implements the following new ones.
94              
95             =head2 args
96              
97             Returns a hash-reference containing all arguments passed to the command
98             on the command-line or to the method L.
99             The keys are the long variants of the possible commandline arguments
100             altough you may have used short variants.
101              
102             #if you passed -s or --something
103             $self->args->{something} #foo
104              
105             Also, please note that some options turn out to be "eaten-up" by
106             L. So, do not use them in your commands and consider them
107             reserved. The known options are 'C' and 'C'.
108             L worked around this limitation by using
109             upper case for a short variant of its options - 'C'.
110              
111              
112             =head2 name
113              
114             The name of your command - C<(ref $self) =~ /(\w+)$/;>.
115              
116             =head2 home
117              
118             Returns current Ado::Command::foo home.
119              
120             =head1 METHODS
121              
122             L inherits all methods from L and implements the following new ones.
123              
124             =head2 init
125              
126             Should be implemented by the inheriting command.
127              
128             Should get options from the commandline and populate C<$self-Eargs>.
129             Must return C<$self>.
130              
131             =head2 run
132              
133             A default C<$command-Erun(@args)> method for all Ado::Command commands.
134             This is the entry point to your mini application.
135             Looks for subcommands/actions which are looked up in
136             the C<--do> commands line argument and executed.
137             Dies with an error message advising you to implement the subcommand
138             if it is not found in C<$self-Econfig-E{actions}>.
139             Override it if you want specific behavior.
140              
141             # as bin/ado alabala --do action --param1 value
142             Ado::Command::alabala->run(@ARGV);
143             #or from a controller
144             Ado::Command::alabala->run(
145             --do => action => --param1 => 'value' );
146              
147             =head2 config
148              
149             Returns the configuration portion specific for a command.
150              
151             #Somewhere in Ado::Command::alabala
152             $self->config('username')
153             #Same as $self->app->config('alabala')->{username}
154              
155              
156             =head1 SUBCOMANDS
157              
158             Subcommands shared by all command classes inheriting this class.
159              
160             ...
161              
162              
163             =head1 SEE ALSO
164              
165             L, L, L
166              
167             =head1 AUTHOR
168              
169             Красимир Беров (Krasimir Berov)
170              
171             =head1 COPYRIGHT AND LICENSE
172              
173             Copyright 2013-2014 Красимир Беров (Krasimir Berov).
174              
175             This program is free software, you can redistribute it and/or
176             modify it under the terms of the
177             GNU Lesser General Public License v3 (LGPL-3.0).
178             You may copy, distribute and modify the software provided that
179             modifications are open source. However, software that includes
180             the license may release under a different license.
181              
182             See http://opensource.org/licenses/lgpl-3.0.html for more information.
183              
184             =cut