lib/CLI/Driver.pm

filter: Statementstmt 89.4% 186 / 208 Branchbran 48.5% 33 / 68 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 97.0% 33 / 34 Podpod n/a totaltotal 78.4% 255 / 325 CC 45 SCAR 21.8
CC
45
Cov
78%
get_actions
20.4
get_action
13.9
_parse_yaml
12.9
CRAP 8.8
« script/clidriver.pl ↑ Summary lib/CLI/Driver/Method.pm »
1 package CLI::Driver;
2  
3 15 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Kavorka '-all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Data::Printer alias => 'pdump';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use CLI::Driver::Action;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 with 'CLI::Driver::CommonRole';
12  
13 15 our $VERSION = 0.63;
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 15 ▶use constant DEFAULT_CLI_DRIVER_PATH => ( '.', 'etc', '/etc' );
Subroutine BEGIN: called CC=1 SCAR=0.0
73 15 ▶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 15 has path => (
84 is => 'rw',
85 isa => 'Str',
86 );
87  
88 15 has file => (
89 is => 'ro',
90 isa => 'Str',
91 lazy => 1,
92 builder => '_build_file'
93 );
94  
95 ##############################################################################
96 ### ACCESSOR ATTRIBUTES
97 ##############################################################################
98  
99 15 has actions => (
100 is => 'rw',
101 isa => 'ArrayRef[CLI::Driver::Action]',
102 lazy => 1,
103 builder => '_build_actions',
104 );
105  
106 ##############################################################################
107 ### PRIVATE ATTRIBUTES
108 ##############################################################################
109  
110 has _orig_argv => (
111 is => 'rw',
112 isa => 'ArrayRef',
113 14 ▶ default => sub {[ @ARGV ]}
Subroutine __ANON__: called CC=1 SCAR=0.0
114 15 );
115  
116 ##############################################################################
117 ### PUBLIC METHODS
118 ##############################################################################
119  
120 15 ▶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
@_ ? : 18 0 18
if ($#_ == 0 and ref $_[0] eq 'HASH') { } 0 18 18
if ($slice_length % 2 != 0) 0 18 18
if ($i <= $#_) 18 18 36
$OK{$_} or Carp::croak("Unknown named parameter: $_") 0 18 18
exists $_{'name'} ? : 18 0 18
if (exists $_{"name"}) 18 0 18
Type::Tiny::XS::Str($name) or _failed_check(9, "Str", $name, "exception_class", "Error::TypeTiny::Assertion") 0 18 18
Condition33%1/3
$#_ == 0 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1800
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
121  
122 18 my $actions = $self->get_actions;
123  
124 18 foreach my $action (@$actions) {
125 129 ▶ if ( $action->name eq $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action->name eq $name) 18 111 129
126 18 return $action;
127 }
128 }
129 }
130  
131 15 ▶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
@_ ? : 18 0 18
if ($#_ == 0 and ref $_[0] eq 'HASH') { } 0 18 18
if ($slice_length % 2 != 0) 0 18 18
if ($i <= $#_) 0 18 18
$OK{$_} or Carp::croak("Unknown named parameter: $_") 0 0 0
exists $_{'want_hashref'} ? : 0 18 18
Type::Tiny::XS::Bool($want_hashref) or _failed_check(3, "Bool", $want_hashref, "exception_class", "Error::TypeTiny::Assertion") 0 18 18
Condition33%1/3
$#_ == 0 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1800
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
132  
133 18 my @ret = @{ $self->actions };
134  
135 18 ▶ if ($want_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_hashref) 0 18 18
136  
137 0 my %actions;
138 0 foreach my $action (@ret) {
139 0 my $name = $action->name;
140 0 ▶ next if $name =~ /dummy/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /dummy/iu 0 0 0
141 0 $actions{$name} = $action;
142 }
143  
144 0 return \%actions;
145 }
146  
147 18 return \@ret;
148 }
149  
150 ##############################################################################
151 ### PRIVATE METHODS
152 ##############################################################################
153  
154 15 ▶method _find_file {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _find_file: called CC=5 SCAR=16.7
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 14 0 14
155  
156 14 my @path;
157 14 ▶ if ($self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->path) 14 0 14
158 14 push @path, split(/:/, $self->path);
159 }
160
161 14 push @path, DEFAULT_CLI_DRIVER_PATH;
162  
163 14 foreach my $path (@path) {
164 14 my $fullpath = sprintf "%s/%s", $path, $self->file;
165 14 ▶ if ( -f $fullpath ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fullpath) 14 0 14
166 14 return $fullpath;
167 }
168 }
169  
170 0 my $msg = sprintf "unable to find %s in: %s", $self->file,
171 join( ', ', @path );
172 0 confess $msg;
173 }
174  
175 15 ▶method _build_actions {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_actions: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 14 0 14
176  
177 14 my @actions;
178  
179 14 my $driver_file = $self->_find_file;
180 14 my $actions = $self->_parse_yaml( path => $driver_file );
181  
182 14 foreach my $action_name ( keys %$actions ) {
183  
184 196 my $action = CLI::Driver::Action->new( name => $action_name );
185 196 my $success = $action->parse( href => $actions->{$action_name} );
186 196 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) 196 0 196
187 196 push @actions, $action;
188 }
189 }
190  
191 14 return \@actions;
192 }
193  
194 15 ▶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
@_ ? : 14 0 14
if ($#_ == 0 and ref $_[0] eq 'HASH') { } 0 14 14
if ($slice_length % 2 != 0) 0 14 14
if ($i <= $#_) 14 14 28
$OK{$_} or Carp::croak("Unknown named parameter: $_") 0 14 14
exists $_{'path'} ? : 14 0 14
if (exists $_{"path"}) 14 0 14
Type::Tiny::XS::Str($path) or _failed_check(9, "Str", $path, "exception_class", "Error::TypeTiny::Assertion") 0 14 14
Condition33%1/3
$#_ == 0 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1400
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
195  
196 14 my $actions;
197 14 eval {
198 14 my $yaml = YAML::Tiny->read($path);
199 14 $actions = $yaml->[0];
200 };
201 14 ▶ confess $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 14 14
202  
203 14 return $actions;
204 }
205  
206 15 ▶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
207  
208 0 ▶ if ( $ENV{CLI_DRIVER_FILE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"CLI_DRIVER_FILE"}) 0 0 0
209 0 return $ENV{CLI_DRIVER_FILE};
210 }
211  
212 0 return DEFAULT_CLI_DRIVER_FILE;
213 }
214  
215 15 1;
« script/clidriver.pl ↑ Summary lib/CLI/Driver/Method.pm »