Cme/Command/run.pm

filter: Statementstmt 87.0% 249 / 286 Branchbran 67.2% 82 / 122 Conditioncond 67.4% 29 / 43 MC/DCmcdc 41.6% 10 / 24 Subroutinesub 94.2% 33 / 35 Podpod 27.7% 5 / 18 totaltotal 77.2% 408 / 528 CC 89 SCAR 24.2
CC
89
Cov
80%
check_script_arguments
47.6
parse_script
16.8
run_script
15.8
CRAP 11.3
« Cme/Command/fix.pm ↑ Summary Cme/Command/dump.pm »
1 #
2 # This file is part of App-Cme
3 #
4 # This software is Copyright (c) 2014-2022 by Dominique Dumont <ddumont@cpan.org>.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 # ABSTRACT: Run a cme script
11  
12 package App::Cme::Command::run ;
13 2 $App::Cme::Command::run::VERSION = '1.049';
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Config::Model 2.156; # for delete_instance
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Config::Model::Lister;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use App::Cme -command ;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 ▶use base qw/App::Cme::Common/;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 my $__test_home = '';
33 # used only by tests
34 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
35 0 ▶sub _set_test_home { $__test_home = shift; return;}
Subroutine _set_test_home: not called CC=1 SCAR=6.9
36  
37 2 ▶my $home = $__test_home || File::HomeDir->my_home;
Condition33%1/3
$__test_home || 'File::HomeDir'->my_home
l!l&&r!l&&!r
020
MC/DC0%0/2
$__test_home || 'File::HomeDir'->my_home
$__test_home 'File::HomeDir'->my_home
Truth table33%1/3
A || B
A= $__test_homeB= 'File::HomeDir'->my_home
ABresult
1X1
011
000
38  
39 2 my @script_paths = map {path($_)} (
40 "$home/.cme/scripts",
41 "/etc/cme/scripts/",
42 );
43  
44 2 push @script_paths, path($INC{"Config/Model.pm"})->parent->child("Model/scripts") ;
45  
46 19 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
47 19 my ( $class, $app ) = @_;
48 return (
49 19 [ "arg=s@" => "script argument. run 'cme run <script> -doc' for possible arguments" ],
50 [ "backup:s" => "Create a backup of configuration files before saving." ],
51 [ "foreach=s" => "Run script in several directories. The list of directories "
52 . "must be passed as a single argument, i.e. --foreach 'foo bar'. If this "
53 . "argument is '-', the list is taken from STDIN."],
54 [ "commit|c:s" => "commit change with passed message" ],
55 [ "cat" => "Show the script file" ],
56 [ "no-commit|nc!" => "skip commit to git" ],
57 [ "doc!" => "show documention of script" ],
58 [ "list!" => "list available scripts" ],
59 $class->cme_global_options,
60 );
61 }
62  
63 19 ▶sub validate_args {
Subroutine validate_args: called CC=1 SCAR=0.0
Pod documented
64 19 my ($self, $opt, $args) = @_;
65  
66 19 $self->check_unknown_args($args);
67 19 return;
68 }
69  
70 19 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
71 19 my ($self) = @_;
72 19 my $desc = $self->SUPER::usage_desc; # "%c COMMAND %o"
73 19 return "$desc [ script ] [ -args foo=12 [ -args bar=13 ]";
74 }
75  
76 0 ▶sub description {
Subroutine description: not called CC=1 SCAR=6.9
Pod documented
77 0 my ($self) = @_;
78 0 return $self->get_documentation;
79 }
80  
81 19 ▶sub check_script_arguments ($self, $opt, $script_name) {
Subroutine check_script_arguments: called CC=8 SCAR=38.6
Pod undocumented
82 19 ▶ if ($opt->{list} or not $script_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"list"} or not $script_name) 0 19 19
Condition33%1/3
$opt->{"list"} or not $script_name
l!l&&r!l&&!r
0019
MC/DC0%0/2
$opt->{"list"} or not $script_name
$opt->{"list"} not $script_name
Truth table33%1/3
A or not(B)
A= $opt->{"list"}B= not $script_name
ABresult
1X1
011
000
83 0 my @scripts;
84 0 foreach my $path ( @script_paths ) {
85 0 ▶ next unless $path->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $path->is_dir 0 0 0
86 0 push @scripts, map {$_->basename} grep { ! /~$/ } $path->children();
87 }
88 0 ▶ say $opt->{list} ? "Available scripts:" : "Missing script argument. Choose one of:";
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'list'} ? : 0 0 0
89 0 foreach my $script_path (sort @scripts) {
90 0 my ($file, $data) = $self->get_script_data($script_path);
91 0 ▶ my $app_info = $data->{app} ? sprintf(" (app %s)", $data->{app}) : "";
Branch: 1 branch
BranchTrueFalseTotal
$data->{'app'} ? : 0 0 0
92 0 printf("- %s%s\n",$script_path, $app_info );
93 }
94 0 say "";
95 0 say "Run 'cme run <script> -doc' to get more details on a script.";
96 0 return 0;
97 }
98 19 return 1;
99 }
100  
101 19 ▶sub find_script_file ($self, $script_name) {
Subroutine find_script_file: called CC=6 SCAR=25.9
Pod undocumented
102 19 my $script;
103 19 ▶ if ($script_name =~ m!/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($script_name =~ m[/]u) { } 19 0 19
104 19 $script = path($script_name);
105 }
106 else {
107 # check script in known locations
108 0 foreach my $path ( @script_paths ) {
109 0 ▶ next unless $path->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $path->is_dir 0 0 0
110 0 $script = $path->child($script_name);
111 0 ▶ last if $script->is_file;
Branch: 1 branch
BranchTrueFalseTotal
if $script->is_file 0 0 0
112 }
113 }
114  
115 19 ▶ die "Error: cannot find script $script_name\n" unless $script->is_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $script->is_file 0 19 19
116  
117 19 return $script;
118 }
119  
120 ## no critic (Subroutines::ProhibitManyArgs)
121 20 ▶sub replace_var_in_value ($user_args, $script_var, $data, $value) {
Subroutine replace_var_in_value: called CC=6 SCAR=17.9
Pod undocumented
122 20 state $var_pattern = qr~(?<!\\) \$([a-zA-Z]\w+) (?!\s*{)~x;
123  
124 # change $var but not \$var, not $var{} and not $1
125 20 $value =~ s~ $var_pattern
126 21 ▶ ~ $user_args->{$1} // $script_var->{$1} // $ENV{$1} // $data->{default}{$1} // '$'.$1 ~xeg;
Condition100%11/11
((($user_args->{$1} // $script_var->{$1}) // $ENV{$1}) // $data->{"default"}{$1}) // '$' . $1
l!l
183
(($user_args->{$1} // $script_var->{$1}) // $ENV{$1}) // $data->{"default"}{$1}
l!l&&r!l&&!r
1713
($user_args->{$1} // $script_var->{$1}) // $ENV{$1}
l!l&&r!l&&!r
1434
$user_args->{$1} // $script_var->{$1}
l!l&&r!l&&!r
777
MC/DC100%4/4
((($user_args->{$1} // $script_var->{$1}) // $ENV{$1}) // $data->{"default"}{$1}) // '$' . $1
$user_args->{$1} $script_var->{$1} $ENV{$1} $data->{"default"}{$1}
Truth table100%5/5
A // B // C // D
A= $user_args->{$1}B= $script_var->{$1}C= $ENV{$1}D= $data->{"default"}{$1}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
127  
128 # register vars without replacements
129 20 foreach my $var ($value =~ m~ $var_pattern ~xg) {
130 3 $data->{missing}{$var} = 1 ;
131 }
132  
133 # now change \$var in $var
134 20 $value =~ s!\\\$!\$!g;
135  
136 20 return $value;
137 }
138  
139 # replace variables with command arguments or eval'ed variables or env variables
140 ## no critic (Subroutines::ProhibitManyArgs)
141 17 ▶sub replace_vars ($user_args, $script_var, $data, @items) {
Subroutine replace_vars: called CC=5 SCAR=16.1
Pod undocumented
142 17 foreach my $item (@items) {
143 51 ▶ if (ref $data->{$item} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data->{$item} eq 'ARRAY') { } 32 19 51
elsif ($data->{$item}) { } 1 18 19
144 32 my @new;
145 32 foreach my $value ($data->{$item}->@*) {
146 19 push @new, replace_var_in_value ($user_args, $script_var, $data, $value);
147 }
148 32 $data->{$item} = \@new;
149 }
150 elsif ($data->{$item}) {
151 1 $data->{$item} = replace_var_in_value ($user_args, $script_var, $data, $data->{$item});
152 }
153 }
154 17 return;
155 }
156  
157 15 ▶sub parse_script_lines ($script, $lines) {
Subroutine parse_script_lines: called CC=7 SCAR=19.5
Pod undocumented
158 15 my $parsed_data = {
159 app => '',
160 doc => [],
161 code => [],
162 commit_msg => '',
163 # provide default values
164 default => {},
165 load => [],
166 var => [],
167 };
168  
169 15 my $line_nb = 0;
170  
171 # check content, store app
172 15 ▶ while ($lines->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 43 14 57
173 43 my $line = shift $lines->@*;
174 43 $line_nb++;
175 43 $line =~ s/#.*//; # remove comments
176 43 $line =~ s/^\s+//;
177 43 $line =~ s/\s+$//;
178 43 my ($key,@value);
179  
180 43 ▶ if ($line =~ /^---\s*(\w+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^---\s*(\w+)$/u) { } 3 40 43
elsif ($line eq '---') { } 3 37 40
181 3 $key = $1;
182 3 ▶ while ($lines->[0] !~ /^---/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lines->[0] =~ /^---/u) 9 3 12
183 9 $lines->[0] =~ s/#.*//; # remove comments
184 9 push @value, shift $lines->@*;
185 }
186 }
187 elsif ($line eq '---') {
188 3 next;
189 }
190 else {
191 37 ($key,@value) = split /[\s:]+/, $line, 2;
192 }
193  
194 40 ▶ next unless $key ; # empty line
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 40 40
195  
196 40 ▶ parse_command($key, $parsed_data, $line_nb, \@value)
Branch: 1 branch
BranchTrueFalseTotal
parse_command($key, $parsed_data, $line_nb, \@value) or die "Error in cme DSL file $script line ${line_nb}: unexpected '${key}' instruction\n" 39 0 39
197 or die "Error in cme DSL file $script line $line_nb: unexpected '$key' instruction\n";
198 }
199  
200 14 return $parsed_data;
201 }
202  
203 40 ▶sub parse_command ($key, $parsed_data, $line_nb, $value) {
Subroutine parse_command: called CC=10 SCAR=23.7
Pod undocumented
204 ## no critic (ControlStructures::ProhibitCascadingIfElse)
205 40 ▶ if ( $key =~ /^app/ ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($key =~ /^app/u) { } 14 26 40
elsif ($key eq 'var') { } 6 20 26
elsif ($key eq 'default') { } 2 18 20
elsif ($key eq 'code') { } 2 16 18
elsif ($key eq 'doc') { } 2 14 16
elsif ($key eq 'load') { } 14 0 14
elsif ($key eq 'commit') { } 0 0 0
206 14 $parsed_data->{app} = $value->[0];
207 }
208 elsif ( $key eq 'var' ) {
209 6 push $parsed_data->{var}->@*, [ $line_nb, $value->@* ];
210 }
211 elsif ( $key eq 'default' ) {
212 # multi-line default value is not supported
213 2 my ( $dk, $dv ) = split /[\s:=]+/, $value->[0], 2;
214 2 $parsed_data->{default}{$dk} = $dv;
215 }
216 elsif ( $key eq 'code' ) {
217 2 ▶ die "Error line $line_nb: Cannot mix code and load section\n" if $parsed_data->{load}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$parsed_data->{"load"}} 1 1 2
218 1 push $parsed_data->{code}->@*, $value->@*;
219 }
220 elsif ( $key eq 'doc' ) {
221 2 push $parsed_data->{doc}->@*, $value->@*;
222 }
223 elsif ( $key eq 'load' ) {
224 14 ▶ die "Error line $line_nb: Cannot mix code and load section\n" if $parsed_data->{code}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$parsed_data->{"code"}} 0 14 14
225 14 push $parsed_data->{load}->@*, $value->@*;
226 }
227 elsif ( $key eq 'commit' ) {
228 0 $parsed_data->{commit_msg} = join "\n", $value->@*;
229 }
230 else {
231 0 return 0;
232 }
233 39 return 1;
234 }
235  
236 17 ▶sub process_script_vars ($user_args, $data) {
Subroutine process_script_vars: called CC=6 SCAR=18.0
Pod undocumented
237 # $var is used in eval'ed strings
238 17 my %var;
239  
240 # find if all variables are accounted for
241 17 $data->{missing} = {};
242  
243 # %args can be used in var section of a script. A new entry in
244 # added in %missing if the script tries to read an undefined value
245 17 tie my %args, 'App::Cme::Run::Var',$data->{missing}, $data->{default};
246 17 %args = $user_args->%*;
247  
248 17 ▶ my $var = delete $data->{var} // [];
Condition100%2/2
delete $data->{"var"} // []
l!l
152
MC/DC100%1/1
delete $data->{"var"} // []
delete $data->{"var"}
Truth table100%2/2
A
A= delete $data->{"var"}
Aresult
11
00
249 17 foreach my $eval_data ($var->@*) {
250 7 my ($line_nb, @value);
251 7 ▶ if (ref $eval_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $eval_data) { } 6 1 7
252 # coming from text format
253 6 ($line_nb, @value) = $eval_data->@*;
254 # eval'ed string comes from system file, not from user data
255 6 my $res = eval ("@value") ; ## no critic (ProhibitStringyEval)
256 6 ▶ die "Error in var specification line $line_nb: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
257 }
258 else {
259 # coming from YAML format
260 1 my $res = eval ($eval_data) ; ## no critic (ProhibitStringyEval)
261 1 ▶ die "Error in var specification: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
262 }
263 }
264  
265 17 replace_vars($user_args, \%var, $data, 'doc', 'load', 'commit_msg');
266  
267 17 $data->{values} = {$data->{default}->%*, %var, $user_args->%*};
268  
269 17 return $data;
270 }
271  
272 18 ▶sub parse_script ($script, $content, $user_args) {
Subroutine parse_script: called CC=15 SCAR=28.2
Pod undocumented
273 18 my $lines->@* = split /\n/,$content;
274  
275 18 ▶ if ($lines->[0] =~ /Format:\s*perl/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines->[0] =~ /Format:\s*perl/iu) 1 17 18
276 ## no critic (ProhibitStringyEval)
277 1 my $data = eval($content);
278 1 ▶ die "Error in script $script (Perl format): $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
279 1 foreach my $forbidden (qw/load var default/) {
280 3 ▶ die "Unexpected '$forbidden\ section in Perl format script $script\n" if $data->{$forbidden};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{$forbidden} 0 3 3
281 }
282 1 ▶ die "Unexpected 'code' section in Perl format script $script. Please use a sub section.\n" if $data->{code};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"code"} 0 1 1
283 1 return $data;
284 }
285  
286 17 my $data;
287 17 ▶ if ($lines->[0] =~ /Format:\s*yaml/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines->[0] =~ /Format:\s*yaml/iu) { } 2 15 17
288 2 my $ypp = YAML::PP->new;
289 2 $data = $ypp->load_string($content);
290 2 foreach my $key (qw/doc code load var/) {
291 8 ▶ next unless defined $data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$key} 5 3 8
292 3 ▶ next if ref $data->{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data->{$key} eq "ARRAY" 0 3 3
293 3 $data->{$key} = [ $data->{$key} ]
294 }
295 2 ▶ $data->{default} //= {};
Condition100%2/2
$data->{"default"} //= {}
l!l
11
MC/DC100%1/1
$data->{"default"} //= {}
$data->{"default"}
Truth table100%2/2
A
A= $data->{"default"}
Aresult
11
00
296 2 $data->{commit_msg} = delete $data->{commit};
297 2 ▶ if ($data->{default} and ref $data->{default} ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"default"} and ref $data->{"default"} ne "HASH") 0 2 2
Condition33%1/3
$data->{"default"} and ref $data->{"default"} ne "HASH"
!ll&&!rl&&r
020
MC/DC0%0/2
$data->{"default"} and ref $data->{"default"} ne "HASH"
$data->{"default"} ref $data->{"default"} ne "HASH"
Truth table33%1/3
A and B
A= $data->{"default"}B= ref $data->{"default"} ne "HASH"
ABresult
0X0
100
111
298 0 ▶ die "default spec must be a hash ref, not a ", ref $data->{default} // 'scalar', "\n";
Condition0%0/2
ref $data->{"default"} // "scalar"
l!l
00
MC/DC0%0/1
ref $data->{"default"} // "scalar"
ref $data->{"default"}
Truth table0%0/2
A
A= ref $data->{"default"}
Aresult
11
00
299 }
300 }
301 else {
302 15 $data = parse_script_lines ($script, $lines);
303 }
304  
305 16 my $processed_data = process_script_vars ($user_args, $data);
306  
307 16 ▶ if (not $processed_data->{app}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($processed_data->{"app"}) 1 15 16
308 1 die "Missing 'app' parameter in script $script.\n"
309 }
310  
311 15 return $processed_data;
312 }
313  
314 1 ▶sub process_commit_message($self, $root, $values, $msg) {
Subroutine process_commit_message: called CC=1 SCAR=0.0
Pod undocumented
315 # replace strings like "{{ config_load_path }}" in $msg
316 1 $msg =~ s/\{\{(.*?)\}\}/$root->grab_value($1)/e;
317  
318 # replace strings like "$value" in $msg
319 1 $msg =~ s/\$(\w+)/$values->{$1}/eg;
320  
321 1 return $msg;
322 }
323  
324 # returns: script file name, script data if script is *not* Perl code
325 19 ▶sub get_script_data ($self, $script_name, $opt = {}) {
Subroutine get_script_data: called CC=4 SCAR=13.9
Pod undocumented
326 19 my $script_file = $self->find_script_file($script_name);
327  
328 19 my $content = $script_file->slurp_utf8;
329  
330 19 ▶ if ($content =~ m/^#!/ and -x $script_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^#!/u and -x $script_file) 2 17 19
Condition66%2/3
$content =~ /^#!/u and -x $script_file
!ll&&!rl&&r
1702
MC/DC50%1/2
$content =~ /^#!/u and -x $script_file
$content =~ /^#!/u -x $script_file
Truth table66%2/3
A and B
A= $content =~ /^#!/uB= -x $script_file
ABresult
0X0
100
111
331 2 my ($app) = $content =~ /# app:\s?(.*?)\n/;
332 2 my @doc = grep {s/^##\s?//;} split /\n/, $content;
333 2 return $script_file, {doc => \@doc, app => $app};
334 }
335  
336 # parse variables passed on command line
337 17 my %user_args = map { split '=',$_,2; } @{ $opt->{arg} };
338  
339 17 return ($script_file, parse_script($script_file, $content, \%user_args));
340 }
341  
342 1 ▶sub run_script_as_code ($self, $script_name, $script_file) {
Subroutine run_script_as_code: called CC=3 SCAR=11.3
Pod undocumented
343 1 splice @ARGV, 0,2; # remove 'run script' arguments
344 1 ▶ my $done = eval $script_file->slurp_utf8."\n1;\n"; ## no critic (BuiltinFunctions::ProhibitStringyEval)
Subroutine BEGIN: called CC=1 SCAR=0.0
345 1 ▶ if (ref $done eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $done eq "HASH") 0 1 1
346 0 warn "script $script_name returns a hash but it's processed as a plain script.",
347 " This may not be what you want\n";
348 }
349 1 ▶ die "Error in script $script_name: $@\n" unless $done;
Branch: 1 branch
BranchTrueFalseTotal
unless $done 0 1 1
350 1 return;
351 }
352  
353 19 ▶sub execute {
Subroutine execute: called CC=12 SCAR=25.4
Pod documented
354 19 my ($self, $opt, $app_args) = @_;
355  
356 # cannot use logger until Config::Model is initialised
357  
358 # see Debian #839593 and perlunicook(1) section X 13
359 19 @$app_args = map { decode_utf8($_, 1) } @$app_args;
360  
361 19 my $script_name = shift @$app_args;
362  
363 19 ▶ return unless $self->check_script_arguments($opt, $script_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_script_arguments($opt, $script_name) 0 19 19
364  
365 19 my ($script_file, $script_data) = $self->get_script_data($script_name, $opt);
366  
367 17 ▶ if ($opt->{cat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"cat"}) 0 17 17
368 0 print $script_file->slurp_utf8;
369 0 return;
370 }
371  
372 17 my $commit_msg = $script_data->{commit_msg};
373  
374 17 ▶ if ($opt->doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->doc) 1 16 17
375 1 say join "\n", $script_data->{doc}->@*;
376 1 ▶ say "will commit with message: '$commit_msg'" if $commit_msg;
Branch: 1 branch
BranchTrueFalseTotal
if $commit_msg 0 1 1
377 1 return;
378 }
379  
380 16 ▶ if (not defined $script_data->{app}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $script_data->{"app"}) 1 15 16
381 1 $self->run_script_as_code ($script_name, $script_file);
382 1 return;
383 }
384  
385 15 ▶ if (my @missing = sort keys $script_data->{missing}->%*) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing) = sort(keys %{$$script_data{"missing"}})) 3 12 15
386 die "Error: Missing variables '". join("', '",@missing)
387 ."' in command arguments for script $script_file\n"
388 3 ."Please use option '".join(' ', map { "-arg $_=xxx"} @missing)."'\n";
389 }
390  
391 12 $self->process_args($opt, [ $script_data->{app}, $app_args->@* ]);
392  
393 # override commit message. may also trigger a commit even if none
394 # is specified in script
395 12 ▶ if ($opt->{commit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"commit"}) 0 12 12
396 0 $commit_msg = $opt->{commit};
397 }
398  
399 12 ▶ $opt->{_verbose} = 'Loader' if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 1 11 12
400  
401 12 my ( $categories, $appli_info, $appli_map ) = Config::Model::Lister::available_models;
402 12 my $app = $script_data->{app};
403 12 ▶ if ($opt->{foreach} and $appli_info->{$app}{_category} ne 'application') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"foreach"} and $appli_info->{$app}{"_category"} ne "application") 0 12 12
Condition66%2/3
$opt->{"foreach"} and $appli_info->{$app}{"_category"} ne "application"
!ll&&!rl&&r
1110
MC/DC0%0/2
$opt->{"foreach"} and $appli_info->{$app}{"_category"} ne "application"
$opt->{"foreach"} $appli_info->{$app}{"_category"} ne "application"
Truth table66%2/3
A and B
A= $opt->{"foreach"}B= $appli_info->{$app}{"_category"} ne "application"
ABresult
0X0
100
111
404 die "Cannot use --foreach option with $app. This option can only be used with ".
405 0 join(' ', $categories->{application}->@*). ". Check your cme script.\n";
406 }
407  
408 # parse variables passed on command line
409  
410 12 ▶ if ($opt->{foreach}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'foreach'}) { } 1 11 12
411 1 $self->run_foreach_loop($opt,$app_args, $script_data);
412 }
413 else {
414 11 my %user_args = map { split '=',$_,2; } @{ $opt->{arg} };
415 11 $self->run_script ($opt, $app_args, $script_data, \%user_args);
416 }
417  
418 12 return;
419 }
420  
421 1 ▶sub run_foreach_loop($self, $opt,$app_args, $script_data ) {
Subroutine run_foreach_loop: called CC=4 SCAR=14.1
Pod undocumented
422 0 my %user_args = map { split '=',$_,2; } @{ $opt->{arg} };
423  
424 ## no critic (BuiltinFunctions::ProhibitComplexMappings)
425 1 ▶ my @dirs = map { chomp; split /\s+/; }
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'foreach'} eq '-' ? : 0 1 1
426 1 ($opt->{foreach} eq '-' ? <STDIN> : ($opt->{foreach}));
427  
428 1 my $start = path('.')->absolute;
429  
430 1 foreach my $d (@dirs) {
431 3 my $t_dir = $start->child($d);
432 3 ▶ if (not $t_dir->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t_dir->is_dir) 0 3 3
433 0 die "Cannot run script in $d: not a directory\n";
434 }
435 3 say "Running script in $t_dir ...";
436 # instance is stored in Config::Model, so the name must be changed
437 3 $opt->{instance_name} = $d;
438 # instance is persisted in $self, so its ref must be removed
439 3 delete $self->{_instance};
440 3 chdir $t_dir->stringify;
441 3 $self->run_script ($opt, $app_args, $script_data, {%user_args});
442 # once we're done, remove instance from Model to avoid memory leaks
443 3 $self->{_model}->delete_instance($d);
444 }
445  
446 1 chdir $start->stringify;
447 1 return;
448 }
449  
450 14 ▶sub run_script ($self, $opt, $app_args, $script_data, $user_args){
Subroutine run_script: called CC=12 SCAR=27.6
Pod undocumented
451 14 my $commit_msg = $script_data->{commit_msg};
452 14 my $stashed;
453  
454 # stash pending work
455 14 ▶ if ($commit_msg and not $opt->{no_commit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commit_msg and not $opt->{"no_commit"}) 0 14 14
Condition33%1/3
$commit_msg and not $opt->{"no_commit"}
!ll&&!rl&&r
1400
MC/DC0%0/2
$commit_msg and not $opt->{"no_commit"}
$commit_msg not $opt->{"no_commit"}
Truth table33%1/3
A and not(B)
A= $commit_msgB= not $opt->{"no_commit"}
ABresult
0X0
100
111
456 0 $stashed = $self->autostash;
457 }
458  
459 # call loads
460 14 my ($model, $inst, $root) = $self->init_cme($opt,$app_args);
461 14 foreach my $load_str ($script_data->{load}->@*) {
462 12 $root->load($load_str);
463 }
464  
465 14 ▶ if ($script_data->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($script_data->{"code"}) 11 3 14
466 11 my $to_run = '';
467 11 ▶ while (my ($name, $value) = each $script_data->{values}->%*) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$script_data{"values"}}) 11 11 22
468 11 $to_run .= "my \$$name = '$value';\n";
469 }
470 11 $to_run .= join("\n",$script_data->{code}->@*);
471 11 my $res = eval($to_run); ## no critic (ProhibitStringyEval)
472 11 ▶ die "Error in code specification: $@\ncode is: \n$to_run\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
473 }
474  
475 14 ▶ if ($script_data->{sub}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($script_data->{"sub"}) 1 13 14
476 1 $script_data->{sub}->($root, $user_args);
477 }
478  
479 14 ▶ if ($inst->needs_save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inst->needs_save) { } 14 0 14
480 14 $self->save($inst,$opt) ;
481  
482 # commit if needed
483 14 ▶ if ($commit_msg and not $opt->{no_commit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commit_msg and not $opt->{"no_commit"}) 0 14 14
Condition33%1/3
$commit_msg and not $opt->{"no_commit"}
!ll&&!rl&&r
1400
MC/DC0%0/2
$commit_msg and not $opt->{"no_commit"}
$commit_msg not $opt->{"no_commit"}
Truth table33%1/3
A and not(B)
A= $commit_msgB= not $opt->{"no_commit"}
ABresult
0X0
100
111
484 my $processed_msg = $self->process_commit_message(
485 0 $root, $script_data->{values}, $commit_msg
486 );
487 0 $self->commit($processed_msg);
488 }
489 } else {
490 0 say "No change were applied";
491 }
492  
493  
494 14 ▶ if ($stashed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stashed) 0 14 14
495 0 $self->pop_stash;
496 }
497  
498 14 return;
499 }
500  
501 package App::Cme::Run::Var; ## no critic (Modules::ProhibitMultiplePackages)
502 2 $App::Cme::Run::Var::VERSION = '1.049';
503 2 require Tie::Hash;
504  
505 ## no critic (ClassHierarchies::ProhibitExplicitISA)
506 2 our @ISA = qw(Tie::ExtraHash);
507  
508 7 ▶sub FETCH {
Subroutine FETCH: called CC=4 SCAR=13.9
509 7 my ($self, $key) = @_ ;
510 7 my ($h, $missing, $default) = @$self;
511 7 ▶ my $res = $h->{$key} // $default->{$key} ;
Condition100%3/3
$h->{$key} // $default->{$key}
l!l&&r!l&&!r
322
MC/DC100%2/2
$h->{$key} // $default->{$key}
$h->{$key} $default->{$key}
Truth table100%3/3
A // B
A= $h->{$key}B= $default->{$key}
ABresult
1X1
011
000
512 7 ▶ $missing->{$key} = 1 unless defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 2 5 7
513 7 ▶ return $res // '';
Condition100%2/2
$res // ""
l!l
52
MC/DC100%1/1
$res // ""
$res
Truth table100%2/2
A
A= $res
Aresult
11
00
514 }
515  
516 2 1;
517  
518 __END__
« Cme/Command/fix.pm ↑ Summary Cme/Command/dump.pm »