lib/CLI/Driver.pm

filter: Statementstmt 87.2% 219 / 251 Branchbran 45.8% 44 / 96 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 95.1% 39 / 41 Podpod n/a totaltotal 75.6% 305 / 403 CC 60 SCAR 21.9
CC
60
Cov
75%
set_argv_map
20.6
get_actions
20.4
get_action
13.9
CRAP 8.9
« script/clidriver.pl ↑ Summary lib/CLI/Driver/Method.pm »
1 package CLI::Driver;
2  
3 17 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Kavorka '-all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Data::Printer alias => 'pdump';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use CLI::Driver::Action;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 with 'CLI::Driver::CommonRole';
12  
13 17 our $VERSION = 0.67;
14  
15 =head1 NAME
16  
17 CLI::Driver
18  
19 =cut
20  
21 =head1 SYNOPSIS
22  
23 This is a module to drive your cli tool from a yaml config file.
24  
25 use CLI::Driver;
26
27 my $cli = CLI::Driver->new;
28
29 my $action = $cli->get_action( name => $ActionFromArgv );
30 if ($action) {
31 $action->do;
32 }
33 else {
34 $Driver->fatal("failed to find action in config file");
35 }
36  
37 ### cli-driver.yml example
38
39 do-something:
40 desc: "Action description"
41 class:
42 name: My::App
43 attr:
44 required:
45 hard:
46 f: foo
47 soft:
48 h: home
49 a: '@array_arg'
50 optional:
51 flags:
52 dry-run: dry_run_flag
53 method:
54 name: my_method
55 args:
56 required:
57 hard:
58 soft:
59 optional:
60 flags:
61 help:
62 args:
63 f: "Additional help info for argument 'f'"
64 examples:
65 - "-f foo -a val1 -a val2 --dry-run"
66 =cut
67  
68 ##############################################################################
69 ### CONSTANTS
70 ##############################################################################
71  
72 17 ▶use constant DEFAULT_CLI_DRIVER_PATH => ( '.', 'etc', '/etc' );
Subroutine BEGIN: called CC=1 SCAR=0.0
73 17 ▶use constant DEFAULT_CLI_DRIVER_FILE => 'cli-driver.yml';
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 ##############################################################################
76 ### REQUIRED ATTRIBUTES
77 ##############################################################################
78  
79 ##############################################################################
80 ### OPTIONAL ATTRIBUTES
81 ##############################################################################
82  
83 17 has path => (
84 is => 'rw',
85 isa => 'Str',
86 );
87  
88 17 has file => (
89 is => 'ro',
90 isa => 'Str',
91 lazy => 1,
92 builder => '_build_file'
93 );
94  
95 #
96 # Overrides @ARGV for fetching command arguments. Contents example:
97 #
98 # {
99 # classAttrName1 => 'abc',
100 # classAttrName2 => 'def',
101 # methodArgName1 => 'ghi'
102 # }
103 #
104 # Notice the cli switches are not part of the map.
105 #
106 17 has argv_map => (
107 is => 'rw',
108 isa => 'HashRef',
109 predicate => 'has_argv_map',
110 writer => '_set_argv_map',
111 );
112  
113 ##############################################################################
114 ### ACCESSOR ATTRIBUTES
115 ##############################################################################
116  
117 17 has actions => (
118 is => 'rw',
119 isa => 'ArrayRef[CLI::Driver::Action]',
120 lazy => 1,
121 builder => '_build_actions',
122 );
123  
124 ##############################################################################
125 ### PRIVATE ATTRIBUTES
126 ##############################################################################
127  
128 ##############################################################################
129 ### PUBLIC METHODS
130 ##############################################################################
131  
132 17 ▶method BUILD (@argv) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BUILD: called CC=4 SCAR=14.0
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 16 0 16
$#_ >= 0 ? : 16 0 16
133  
134 16 ▶ if ( $self->has_argv_map ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_argv_map) 1 15 16
135 1 $self->_build_global_argv_map( $self->argv_map );
136 }
137 }
138  
139 17 ▶method set_argv_map (HashRef $argv_map) {
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine BEGIN: called CC=1 SCAR=3.2
Subroutine set_argv_map: not called CC=6 SCAR=30.3
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ != 1 0 0 0
$#_ >= 0 ? : 0 0 0
if ($#_ >= 0) 0 0 0
Ref::Util::XS::is_plain_hashref($argv_map) or _failed_check(22, "HashRef", $argv_map, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
140  
141 0 $self->_set_argv_map( {%$argv_map} );
142 0 $self->_build_global_argv_map( $self->argv_map );
143 }
144  
145 17 ▶method get_action (Str :$name!) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine get_action: called CC=13 SCAR=26.3
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 20 0 20
if ($#_ == 0 and ref $_[0] eq 'HASH') { } 0 20 20
if ($slice_length % 2 != 0) 0 20 20
if ($i <= $#_) 20 20 40
$OK{$_} or Carp::croak("Unknown named parameter: $_") 0 20 20
exists $_{'name'} ? : 20 0 20
if (exists $_{"name"}) 20 0 20
Type::Tiny::XS::Str($name) or _failed_check(9, "Str", $name, "exception_class", "Error::TypeTiny::Assertion") 0 20 20
Condition33%1/3
$#_ == 0 and ref $_[0] eq "HASH"
!ll&&!rl&&r
2000
MC/DC0%0/2
$#_ == 0 and ref $_[0] eq "HASH"
$#_ == 0 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= $#_ == 0B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
146  
147 20 my $actions = $self->get_actions;
148  
149 20 foreach my $action (@$actions) {
150 146 ▶ if ( $action->name eq $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action->name eq $name) 20 126 146
151 20 return $action;
152 }
153 }
154 }
155  
156 17 ▶method get_actions (Bool :$want_hashref = 0) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine get_actions: called CC=13 SCAR=30.2
Branch: 7 branches
BranchTrueFalseTotal
@_ ? : 20 0 20
if ($#_ == 0 and ref $_[0] eq 'HASH') { } 0 20 20
if ($slice_length % 2 != 0) 0 20 20
if ($i <= $#_) 0 20 20
$OK{$_} or Carp::croak("Unknown named parameter: $_") 0 0 0
exists $_{'want_hashref'} ? : 0 20 20
Type::Tiny::XS::Bool($want_hashref) or _failed_check(3, "Bool", $want_hashref, "exception_class", "Error::TypeTiny::Assertion") 0 20 20
Condition33%1/3
$#_ == 0 and ref $_[0] eq "HASH"
!ll&&!rl&&r
2000
MC/DC0%0/2
$#_ == 0 and ref $_[0] eq "HASH"
$#_ == 0 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= $#_ == 0B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
157  
158 20 my @ret = @{ $self->actions };
159  
160 20 ▶ if ($want_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_hashref) 0 20 20
161  
162 0 my %actions;
163 0 foreach my $action (@ret) {
164 0 my $name = $action->name;
165 0 ▶ next if $name =~ /dummy/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /dummy/iu 0 0 0
166 0 $actions{$name} = $action;
167 }
168  
169 0 return \%actions;
170 }
171  
172 20 return \@ret;
173 }
174  
175 ##############################################################################
176 ### PRIVATE METHODS
177 ##############################################################################
178  
179 17 ▶method _find_file {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _find_file: called CC=5 SCAR=16.7
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 16 0 16
180  
181 16 my @path;
182 16 ▶ if ( $self->path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->path) 16 0 16
183 16 push @path, split( /:/, $self->path );
184 }
185  
186 16 push @path, DEFAULT_CLI_DRIVER_PATH;
187  
188 16 foreach my $path (@path) {
189 16 my $fullpath = sprintf "%s/%s", $path, $self->file;
190 16 ▶ if ( -f $fullpath ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fullpath) 16 0 16
191 16 return $fullpath;
192 }
193 }
194  
195 0 my $msg = sprintf "unable to find %s in: %s", $self->file,
196 join( ', ', @path );
197 0 confess $msg;
198 }
199  
200 17 ▶method _build_actions {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_actions: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 16 0 16
201  
202 16 my @actions;
203  
204 16 my $driver_file = $self->_find_file;
205 16 my $actions = $self->_parse_yaml( path => $driver_file );
206  
207 16 foreach my $action_name ( keys %$actions ) {
208  
209 240 ▶ my $action = CLI::Driver::Action->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_argv_map ? : 15 225 240
210 name => $action_name,
211 use_argv_map => $self->has_argv_map ? 1 : 0
212 );
213
214 240 my $success = $action->parse( href => $actions->{$action_name} );
215 240 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) 240 0 240
216 240 push @actions, $action;
217 }
218 }
219  
220 16 return \@actions;
221 }
222  
223 17 ▶method _parse_yaml (Str :$path!) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _parse_yaml: called CC=12 SCAR=25.6
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 16 0 16
if ($#_ == 0 and ref $_[0] eq 'HASH') { } 0 16 16
if ($slice_length % 2 != 0) 0 16 16
if ($i <= $#_) 16 16 32
$OK{$_} or Carp::croak("Unknown named parameter: $_") 0 16 16
exists $_{'path'} ? : 16 0 16
if (exists $_{"path"}) 16 0 16
Type::Tiny::XS::Str($path) or _failed_check(9, "Str", $path, "exception_class", "Error::TypeTiny::Assertion") 0 16 16
Condition33%1/3
$#_ == 0 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1600
MC/DC0%0/2
$#_ == 0 and ref $_[0] eq "HASH"
$#_ == 0 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= $#_ == 0B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
224  
225 16 my $actions;
226 16 eval {
227 16 my $yaml = YAML::Tiny->read($path);
228 16 $actions = $yaml->[0];
229 };
230 16 ▶ confess $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 16 16
231  
232 16 return $actions;
233 }
234  
235 17 ▶method _build_file {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine _build_file: not called CC=3 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
236  
237 0 ▶ if ( $ENV{CLI_DRIVER_FILE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"CLI_DRIVER_FILE"}) 0 0 0
238 0 return $ENV{CLI_DRIVER_FILE};
239 }
240  
241 0 return DEFAULT_CLI_DRIVER_FILE;
242 }
243  
244 17 ▶method _build_global_argv_map (HashRef $argv_map) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _build_global_argv_map: called CC=7 SCAR=19.8
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 1 0 1
if @_ != 1 0 1 1
$#_ >= 0 ? : 1 0 1
if ($#_ >= 0) 1 0 1
Ref::Util::XS::is_plain_hashref($argv_map) or _failed_check(22, "HashRef", $argv_map, "exception_class", "Error::TypeTiny::Assertion") 0 1 1
245  
246 1 %ARGV = ();
247  
248 1 foreach my $key ( keys %$argv_map ) {
249 4 $ARGV{$key} = $argv_map->{$key};
250 }
251 }
252  
253 17 1;
« script/clidriver.pl ↑ Summary lib/CLI/Driver/Method.pm »