Basis.pm

filter: Statementstmt 57.8% 191 / 330 Branchbran 41.3% 67 / 162 Conditioncond 64.7% 11 / 17 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 58.7% 27 / 46 Podpod 88.2% 15 / 17 totaltotal 54.3% 317 / 583 CC 93 SCAR 35.7
CC
93
Cov
53%
_get_color
156.0
_validate_options
67.9
daemonise
42.0
CRAP 35.7
↑ Summary Basis/Config.pm »
1 # ABSTRACT: Simple way to create applications
2  
3  
4 package App::Basis ;
5 2 $App::Basis::VERSION = '1.2';
6 2 ▶use 5.014 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Getopt::Long ;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Exporter ;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::HomeDir ;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Path::Tiny ;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IPC::Cmd qw(run run_forked) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use List::Util qw(max) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use POSIX qw(strftime) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use utf8::all ;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Digest::MD5 qw(md5_base64) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use YAML::Tiny::Color ;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use vars qw( @EXPORT @ISA) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 @ISA = qw(Exporter) ;
23  
24 # this is the list of things that will get imported into the loading packages
25 # namespace
26 2 @EXPORT = qw(
27 init_app
28 show_usage
29 msg_exit
30 get_program
31 debug set_debug
32 daemonise
33 execute_cmd run_cmd
34 set_log_file
35 fix_filename
36 set_test_mode
37 saymd
38 set_verbose
39 verbose
40 verbose_data
41 ) ;
42  
43 # ----------------------------------------------------------------------------
44  
45 2 my $PROGRAM = path($0)->basename ;
46 2 my $LOG_FILE = fix_filename("~/$PROGRAM.log") ;
47  
48 # these variables are held available throughout the life of the app
49 2 my $_app_simple_ctrlc_count = 0 ;
50 2 my $_app_simple_ctrlc_handler ;
51 2 my $_app_simple_help_text = 'Application has not defined help_text yet.' ;
52 2 my $_app_simple_help_options = '' ;
53 2 my $_app_simple_cleanup_func ;
54 2 my $_app_simple_help_cmdline = '' ;
55  
56 2 my %_app_simple_objects = () ;
57 2 my %_cmd_line_options = () ;
58  
59 # we may want to die rather than exiting, helps with testing!
60 2 my $_test_mode = 0 ;
61  
62  
63 # ----------------------------------------------------------------------------
64 # control how we output things to help with testing
65 sub _output
66 3 ▶{
Subroutine _output: called CC=3 SCAR=17.3
67 3 my ( $where, $msg ) = @_ ;
68  
69 3 ▶ if ( !$_test_mode ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_test_mode) 0 3 3
70 0 ▶ if ( $where =~ /stderr/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where =~ /stderr/iu) { } 0 0 0
71 0 say STDERR $msg ;
72 } else {
73 0 say $msg ;
74 }
75 }
76 }
77  
78 # ----------------------------------------------------------------------------
79  
80  
81 sub set_log_file
82 1 ▶{
Subroutine set_log_file: called CC=1 SCAR=0.0
Pod documented
83 1 my ($file) = @_ ;
84 1 $LOG_FILE = $file ;
85 }
86  
87 # ----------------------------------------------------------------------------
88  
89  
90 sub debug
91 0 ▶{
Subroutine debug: not called CC=3 SCAR=24.8
Pod documented
92 0 my ( $level, @debug ) = @_ ;
93  
94 # we may want to undef the debug object, so no debug comes out
95  
96 0 ▶ if ( exists $_app_simple_objects{logger} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_app_simple_objects{'logger'}) { } 0 0 0
97  
98 # run the coderef for the logger
99 $_app_simple_objects{logger}->( $level, @debug )
100 0 ▶ if ( defined $_app_simple_objects{logger} ) ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_app_simple_objects{"logger"} 0 0 0
101 } else {
102 0 path($LOG_FILE)
103 ->append_utf8( strftime( '%Y-%m-%d %H:%M:%S', gmtime( time() ) )
104 . " [$level] "
105 . join( ' ', @debug )
106 . "\n" ) ;
107 }
108 }
109  
110 # ----------------------------------------------------------------------------
111  
112  
113 sub set_debug
114 0 ▶{
Subroutine set_debug: not called CC=3 SCAR=24.8
Pod documented
115 0 my $func = shift ;
116 0 ▶ if ( !$func || ref($func) ne "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $func or ref $func ne 'CODE') { } 0 0 0
Condition0%0/3
not $func or ref $func ne "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $func or ref $func ne "CODE"
not $func ref $func ne "CODE"
Truth table0%0/3
not(A) or B
A= not $funcB= ref $func ne "CODE"
ABresult
1X1
011
000
117 0 warn "set_debug function expects a CODE, got a " . ref($func) ;
118 } else {
119 0 $_app_simple_objects{logger} = $func ;
120 }
121 }
122  
123 # -----------------------------------------------------------------------------
124 2 my $verbose = 1 ;
125  
126  
127 sub set_verbose
128 6 ▶{
Subroutine set_verbose: called CC=1 SCAR=0.0
Pod documented
129 6 $verbose = shift ;
130 }
131  
132  
133 sub verbose
134 0 ▶{
Subroutine verbose: not called CC=2 SCAR=17.9
Pod documented
135 0 my ($msg) = @_ ;
136 0 ▶ say STDERR $msg if ($verbose) ;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
137 }
138  
139  
140 sub verbose_data
141 0 ▶{
Subroutine verbose_data: not called CC=4 SCAR=30.0
Pod undocumented
142 0 ▶ if ( @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) { } 0 0 0
143 0 ▶ say STDERR Dump(@_) if ($verbose) ;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
144  
145 } else {
146 0 my ($data) = @_ ;
147 0 ▶ say STDERR Dump($data) if ($verbose) ;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
148 }
149 }
150  
151 # ----------------------------------------------------------------------------
152 # check that the option structure does not have repeated things in it
153 # returns string of any issue
154  
155 sub _validate_options
156 12 ▶{
Subroutine _validate_options: called CC=16 SCAR=42.2
157 12 my ($options) = @_ ;
158 12 my %seen ;
159 12 my $result = "" ;
160  
161 12 foreach my $opt ( keys %{$options} ) {
162 # options are long|short=, or thing=, or flags long|sort, or long
163 28 my ( $long, $short ) ;
164 28 ▶ if ( $opt =~ /^(.*?)\|(.*?)=/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($opt =~ /^(.*?)\|(.*?)=/u) { } 0 28 28
elsif ($opt =~ /^(.*?)\|(.*?)$/u) { } 13 15 28
elsif ($opt =~ /^(.*?)=/u) { } 4 11 15
elsif ($opt =~ /^(.*?)$/u) { } 11 0 11
elsif ($opt =~ /^(.*?)\|(.*?)\|(.*?)\$/u) { } 0 0 0
165 0 $long = $1 ;
166 0 $short = $2 ;
167 0 ▶ if ( $seen{$long} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($seen{$long}) { } 0 0 0
elsif ($seen{$short}) { } 0 0 0
168 0 $result
169 = "Long option '$long' has already been used. option line '$opt' is at fault"
170 ;
171 0 last ;
172 } elsif ( $seen{$short} ) {
173 0 $result
174 = "Short option '$short' has already been used. option line '$opt' is at fault"
175 ;
176 0 last ;
177 }
178 0 $seen{$short} = 1 ;
179 0 $seen{$long} = 1 ;
180 } elsif ( $opt =~ /^(.*?)\|(.*?)$/ ) {
181 13 $long = $1 ;
182 13 $short = $2 ;
183 13 ▶ if ( $seen{$long} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$long}) 1 12 13
184 1 $result
185 = "Long flag '$long' has already been used. option line '$opt' is at fault"
186 ;
187 1 last ;
188 }
189  
190 12 ▶ if ( $seen{$short} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$short}) 0 12 12
191 0 $result
192 = "short flag '$short' has already been used. option line '$opt' is at fault"
193 ;
194 0 last ;
195 }
196 12 $seen{$short} = 1 ;
197 12 $seen{$long} = 1 ;
198 } elsif ( $opt =~ /^(.*?)=/ ) {
199 4 $long = $1 ;
200 4 ▶ if ( $seen{$long} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$long}) 0 4 4
201 0 $result
202 = "Option '$long' has already been used. option line '$opt' is at fault"
203 ;
204  
205 0 last ;
206 }
207 4 $seen{$long} = 1 ;
208 } elsif ( $opt =~ /^(.*?)$/ ) {
209 11 $long = $1 ;
210 11 ▶ if ( $seen{$long} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$long}) 0 11 11
211 0 $result
212 = "flag '$long' has already been used. option line '$opt' is at fault"
213 ;
214 0 last ;
215 }
216 11 $seen{$long} = 1 ;
217 } elsif ( $opt =~ /^(.*?)\|(.*?)\|(.*?)\$/ ) {
218 0 $long = $1 ;
219 0 $short = $2 ;
220 0 my $extra = $3 ;
221 0 ▶ if ( $seen{$long} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$long}) 0 0 0
222 0 $result
223 = "flag '$long' has already been used. option line '$opt' is at fault"
224 ;
225 0 last ;
226 }
227 0 ▶ if ( $seen{$short} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$short}) 0 0 0
228 0 $result
229 = "flag '$short' has already been used. option line '$opt' is at fault"
230 ;
231 0 last ;
232 }
233 0 ▶ if ( $seen{$extra} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$extra}) 0 0 0
234 0 $result
235 = "flag '$extra' has already been used. option line '$opt' is at fault"
236 ;
237 0 last ;
238 }
239 0 $seen{$long} = 1 ;
240 0 $seen{$short} = 1 ;
241 0 $seen{$extra} = 1 ;
242 }
243 }
244 12 return $result ;
245 }
246  
247 # ----------------------------------------------------------------------------
248  
249  
250 sub init_app
251 12 ▶{
Subroutine init_app: called CC=35 SCAR=36.6
Pod documented
252 12 ▶ my %args
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 12 12
253 = @_ % 2
254 ? die("Odd number of values passed where even is expected.")
255 : @_ ;
256 12 my @options ;
257 12 my $has_required = 0 ;
258 12 my %full_options ;
259  
260 12 ▶ if ( $args{log_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"log_file"}) 0 12 12
261 0 $LOG_FILE = fix_filename( $args{log_file} ) ;
262 }
263  
264 12 ▶ if ( $args{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"debug"}) 0 12 12
265 0 set_debug( $args{debug} ) ;
266 }
267  
268 # get program description
269 12 ▶ $_app_simple_help_text = $args{help_text} if ( $args{help_text} ) ;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"help_text"} 12 0 12
270 $_app_simple_help_cmdline = $args{help_cmdline}
271 12 ▶ if ( $args{help_cmdline} ) ;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"help_cmdline"} 0 12 12
272  
273 12 ▶ die "options must be a hashref" if ( ref( $args{options} ) ne 'HASH' ) ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"options"} ne "HASH" 0 12 12
274  
275 12 $args{options}->{'help|h|?'} = 'Show help' ;
276  
277 12 my @keys = sort keys %{ $args{options} } ;
278 12 my %dnames = _desc_names(@keys) ;
279 12 my $max_desc_len = max( map length, values %dnames ) + 1 ;
280 12 my $help_fmt = " %-${max_desc_len}s %s\n" ;
281  
282 # add help text for 'help' first.
283 12 $_app_simple_help_options .= sprintf $help_fmt, $dnames{'help|h|?'},
284 'Show help' ;
285  
286 #
287 12 my $msg = _validate_options( $args{options} ) ;
288 12 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 1 11 12
289 1 die "$msg" ;
290 }
291  
292 # get options and their descriptions
293 11 foreach my $o (@keys) {
294  
295 # save the option
296 25 push @options, $o ;
297  
298 25 my $name = $o ;
299  
300 # we want the long version of the name if its provided
301 25 $name =~ s/.*?(\w+).*/$1/ ;
302  
303 # remove any type data
304 25 $name =~ s/=(.*)// ;
305  
306 25 ▶ if ( ref( $args{options}->{$o} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'options'}{$o} eq 'HASH') { } 15 10 25
307 die "parameterised option '$name' require a desc option"
308 15 ▶ if ( !$args{options}->{$o}->{desc} ) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"options"}{$o}{"desc"} 1 14 15
309 14 $full_options{$name} = $args{options}->{$o} ;
310 14 ▶ $has_required++ if ( $full_options{$name}->{required} ) ;
Branch: 1 branch
BranchTrueFalseTotal
if $full_options{$name}{"required"} 8 6 14
311 } else {
312 $full_options{$name} = {
313 10 desc => $args{options}->{$o},
314  
315 # possible options that can be passed
316 # depends => '',
317 # default => '',
318 # required => 0,
319 # validate => sub {}
320 } ;
321 }
322  
323 # save the option string too
324 24 $full_options{$name}->{options} = $o ;
325  
326 # build the entry for the help text
327 24 my $desc = $full_options{$name}->{desc} ;
328 24 ▶ if ( $name ne 'help' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name ne "help") 14 10 24
329 14 my $desc = $full_options{$name}->{desc} ;
330  
331 # show the right way to use the options
332 14 my $dname = $dnames{$o} ;
333 14 ▶ $dname .= '*' if ( $full_options{$name}->{required} ) ;
Branch: 1 branch
BranchTrueFalseTotal
if $full_options{$name}{"required"} 8 6 14
334  
335 $desc .= " [DEFAULT: $full_options{$name}->{default}]"
336 14 ▶ if ( $full_options{$name}->{default} ) ;
Branch: 1 branch
BranchTrueFalseTotal
if $full_options{$name}{"default"} 4 10 14
337 14 $_app_simple_help_options .= sprintf $help_fmt, $dname, $desc ;
338 }
339 }
340  
341 # show required options
342 10 ▶ if ($has_required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_required) 8 2 10
343 8 ▶ $_app_simple_help_options
Branch: 1 branch
BranchTrueFalseTotal
$has_required > 1 ? : 0 8 8
344 .= "* required option" . ( $has_required > 1 ? 's' : '' ) . "\n" ;
345 }
346  
347 # catch control-c, user provided or our default
348 $_app_simple_ctrlc_handler
349 10 ▶ = $args{ctrl_c} ? $args{ctrl_c} : \&_app_simple_ctrlc_func ;
Branch: 1 branch
BranchTrueFalseTotal
$args{'ctrl_c'} ? : 0 10 10
350 10 $SIG{'INT'} = $_app_simple_ctrlc_handler ;
351  
352 # get an cleanup function handler
353 10 ▶ $_app_simple_cleanup_func = $args{cleanup} if ( $args{cleanup} ) ;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"cleanup"} 0 10 10
354  
355 # check command line args
356 10 GetOptions( \%_cmd_line_options, @options ) ;
357  
358 # help is a built in
359 10 ▶ show_usage() if ( $_cmd_line_options{help} ) ;
Branch: 1 branch
BranchTrueFalseTotal
if $_cmd_line_options{"help"} 0 10 10
360  
361 # now if we have the extended version we can do some checking
362 10 foreach my $name ( sort keys %full_options ) {
363 warn "Missing desc field for $name"
364 20 ▶ if ( !$full_options{$name}->{desc} ) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $full_options{$name}{"desc"} 0 20 20
365 20 ▶ if ( $full_options{$name}->{required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_options{$name}{"required"}) 8 12 20
366 show_usage( "Required option '$name' is missing", 1 )
367 if (
368 !( $_cmd_line_options{$name}
369 || $full_options{$name}->{default}
370 )
371 8 ▶ ) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $_cmd_line_options{$name} or $full_options{$name}{"default"} 1 7 8
Condition66%2/3
$_cmd_line_options{$name} or $full_options{$name}{"default"}
l!l&&r!l&&!r
701
MC/DC50%1/2
$_cmd_line_options{$name} or $full_options{$name}{"default"}
$_cmd_line_options{$name} $full_options{$name}{"default"}
Truth table66%2/3
A or B
A= $_cmd_line_options{$name}B= $full_options{$name}{"default"}
ABresult
1X1
011
000
372 }
373 19 ▶ if ( $full_options{$name}->{depends} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_options{$name}{"depends"}) 6 13 19
374 6 ▶ if ( !$_cmd_line_options{ $full_options{$name}->{depends} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_cmd_line_options{$full_options{$name}{"depends"}}) 1 5 6
375 1 show_usage(
376 "Option '$name' depends on option '$full_options{$name}->{depends}' but it is missing",
377 1
378 ) ;
379 }
380 }
381  
382 # set a default if there is no value
383 18 ▶ if ( $full_options{$name}->{default} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_options{$name}{"default"}) 4 14 18
384 $_cmd_line_options{$name} = $full_options{$name}->{default}
385 4 ▶ if ( !$_cmd_line_options{$name} ) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $_cmd_line_options{$name} 0 4 4
386 }
387  
388 # call the validation routine if we have one
389 18 ▶ if ( $_cmd_line_options{$name} && $full_options{$name}->{validate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_cmd_line_options{$name} and $full_options{$name}{"validate"}) 3 15 18
Condition100%3/3
$_cmd_line_options{$name} and $full_options{$name}{"validate"}
!ll&&!rl&&r
783
MC/DC100%2/2
$_cmd_line_options{$name} and $full_options{$name}{"validate"}
$_cmd_line_options{$name} $full_options{$name}{"validate"}
Truth table100%3/3
A and B
A= $_cmd_line_options{$name}B= $full_options{$name}{"validate"}
ABresult
0X0
100
111
390 die "need to pass a coderef to validate for option '$name'"
391 3 ▶ if ( !ref( $full_options{$name}->{validate} ) eq 'CODE' ) ;
Branch: 1 branch
BranchTrueFalseTotal
if !ref($full_options{$name}{"validate"}) eq "CODE" 0 3 3
392 die
393 "Option '$name' has validate and should either also have a default or be required"
394 if (
395 !( $full_options{$name}->{required}
396 || $full_options{$name}->{default}
397 )
398 3 ▶ ) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $full_options{$name}{"required"} or $full_options{$name}{"default"} 0 3 3
Condition33%1/3
$full_options{$name}{"required"} or $full_options{$name}{"default"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$full_options{$name}{"required"} or $full_options{$name}{"default"}
$full_options{$name}{"required"} $full_options{$name}{"default"}
Truth table33%1/3
A or B
A= $full_options{$name}{"required"}B= $full_options{$name}{"default"}
ABresult
1X1
011
000
399 3 my $coderef = $full_options{$name}->{validate} ;
400 3 my $result = $coderef->( $_cmd_line_options{$name} ) ;
401 2 ▶ show_usage("Option '$name' does not pass validation")
Branch: 1 branch
BranchTrueFalseTotal
unless $result 1 1 2
402 if ( !$result ) ;
403 }
404 }
405  
406 # auto set verbose if it has been used
407 6 set_verbose( $_cmd_line_options{verbose} ) ;
408  
409 6 return %_cmd_line_options ;
410 }
411  
412 # ----------------------------------------------------------------------------
413  
414  
415 sub get_program
416 1 ▶{
Subroutine get_program: called CC=1 SCAR=0.0
Pod documented
417 1 return $PROGRAM ;
418 }
419  
420 # ----------------------------------------------------------------------------
421  
422  
423 sub get_options
424 0 ▶{
Subroutine get_options: not called CC=1 SCAR=6.9
Pod documented
425 0 return %_cmd_line_options ;
426 }
427  
428 # ----------------------------------------------------------------------------
429 # handle the ctrl-c presses
430  
431 sub _app_simple_ctrlc_func
432 0 ▶{
Subroutine _app_simple_ctrlc_func: not called CC=2 SCAR=17.9
433  
434 # exit if we are already in ctrlC
435 0 ▶ exit(2) if ( $_app_simple_ctrlc_count++ ) ;
Branch: 1 branch
BranchTrueFalseTotal
if $_app_simple_ctrlc_count++ 0 0 0
436 0 _output( 'STDERR', "\nCaught Ctrl-C. press again to exit immediately" ) ;
437  
438 # re-init the handler
439 0 $SIG{'INT'} = $_app_simple_ctrlc_handler ;
440 }
441  
442 # ----------------------------------------------------------------------------
443  
444 # to help with testing we may want to die, which can be caught rather than
445 # exiting, so lets find out
446  
447 sub _exit_or_die
448 3 ▶{
Subroutine _exit_or_die: called CC=3 SCAR=11.2
449 3 ▶ my $state = shift || 1 ;
Condition100%2/2
shift() || 1
l!l
21
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
450  
451 3 ▶ if ($_test_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_test_mode) 3 0 3
452 3 STDERR->flush() ;
453 3 STDOUT->flush() ;
454 3 die "exit state $state" ;
455 }
456 0 exit($state) ;
457 }
458  
459 # ----------------------------------------------------------------------------
460  
461  
462 sub show_usage
463 3 ▶{
Subroutine show_usage: called CC=2 SCAR=7.2
Pod documented
464 3 my ( $msg, $state ) = @_ ;
465  
466 3 my $help = qq{
467 Syntax: $PROGRAM [options] $_app_simple_help_cmdline
468  
469 About: $_app_simple_help_text
470  
471 [options]
472 $_app_simple_help_options} ;
473 3 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) { } 3 0 3
474  
475 # if we have an error message it MUST go to STDERR
476 # to make sure that any program the output is piped to
477 # does not get the message to process
478 3 _output( 'STDERR', "$help\nError: $msg\n" ) ;
479 } else {
480 0 _output( 'STDOUT', $help ) ;
481 }
482  
483 3 _exit_or_die($state) ;
484 }
485  
486 # ----------------------------------------------------------------------------
487  
488  
489 sub msg_exit
490 0 ▶{
Subroutine msg_exit: not called CC=2 SCAR=17.9
Pod documented
491 0 my ( $msg, $state ) = @_ ;
492  
493 0 ▶ _output( 'STDERR', $msg ) if ($msg) ;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 0 0 0
494 0 _exit_or_die($state) ;
495 }
496  
497 # -----------------------------------------------------------------------------
498  
499  
500 sub daemonise
501 0 ▶{
Subroutine daemonise: not called CC=6 SCAR=37.4
Pod documented
502 0 my $rootdir = shift ;
503  
504 0 ▶ if ($rootdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rootdir) 0 0 0
505 0 ▶ chroot($rootdir)
Branch: 1 branch
BranchTrueFalseTotal
chroot $rootdir or die "Could not chroot to $rootdir, only the root user can do this." 0 0 0
506 or die
507 "Could not chroot to $rootdir, only the root user can do this." ;
508 }
509  
510 # fork once and let the parent exit
511 0 my $pid = fork() ;
512  
513 # exit if $pid ;
514 # parent to return 0, as it is logical
515 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
516 0 return 0 ;
517 }
518 0 ▶ die "Couldn't fork: $!" unless defined $pid ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
519  
520 # disassociate from controlling terminal, leave the
521 # process group behind
522  
523 0 ▶ POSIX::setsid() or die "Can't start a new session" ;
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() or die "Can't start a new session" 0 0 0
524  
525 # show that we have started a daemon process
526 0 return 1 ;
527 }
528  
529 # ----------------------------------------------------------------------------
530  
531  
532 sub execute_cmd
533 0 ▶{
Subroutine execute_cmd: not called CC=2 SCAR=17.9
Pod documented
534 0 my %args = @_ ;
535  
536 0 ▶ my $command = $args{command} or die "command required" ;
Branch: 1 branch
BranchTrueFalseTotal
my $command = $args{"command"} or die "command required" 0 0 0
537 # pass everything thought encode incase there is utf8 there
538 0 utf8::encode($command) ;
539  
540 0 my $r = IPC::Cmd::run_forked( $command, \%args ) ;
541  
542 0 return $r ;
543 }
544  
545 # ----------------------------------------------------------------------------
546  
547  
548 sub run_cmd
549 2 ▶{
Subroutine run_cmd: called CC=2 SCAR=6.9
Pod documented
550 2 my ( $cmd, $timeout ) = @_ ;
551  
552 # use our local version of path so that it can pass taint checks
553 2 local $ENV{PATH} = $ENV{PATH} ;
554  
555 # pass everything thought encode incase there is utf8 there
556 2 utf8::encode($cmd) ;
557  
558 2 my %data = ( command => $cmd ) ;
559 2 ▶ $data{timeout} = $timeout if ($timeout) ;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout 0 2 2
560 2 my ( $ret, $err, $full_buff, $stdout_buff, $stderr_buff ) = run(%data) ;
561  
562 2 my $stdout = join( "\n", @{$stdout_buff} ) ;
563 2 my $stderr = join( "\n", @{$stderr_buff} ) ;
564  
565 2 return ( !$ret, $stdout, $stderr ) ;
566 }
567  
568 # -----------------------------------------------------------------------------
569  
570  
571 sub fix_filename
572 9 ▶{
Subroutine fix_filename: called CC=5 SCAR=16.1
Pod documented
573 9 my $file = shift ;
574 9 ▶ return if ( !$file ) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 9 9
575  
576 9 my $home = File::HomeDir->my_home ;
577 9 $file =~ s/^~/$home/ ;
578 9 ▶ if ( $file =~ m|^\.\./| ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[^\.\./]u) 1 8 9
579 1 my $parent = path( Path::Tiny->cwd )->dirname ;
580 1 $file =~ s|^(\.{2})/|$parent/| ;
581 }
582 9 ▶ if ( $file =~ m|^\./| || $file eq '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[^\./]u or $file eq ".") 2 7 9
Condition100%3/3
$file =~ m[^\./]u or $file eq "."
l!l&&r!l&&!r
117
MC/DC100%2/2
$file =~ m[^\./]u or $file eq "."
$file =~ m[^\./]u $file eq "."
Truth table100%3/3
A or B
A= $file =~ m[^\./]uB= $file eq "."
ABresult
1X1
011
000
583 2 my $cwd = Path::Tiny->cwd ;
584 2 $file =~ s|^(\.)/?|$cwd| ;
585 }
586  
587 # replace multiple separators
588 9 $file =~ s|//|/|g ;
589  
590 # get the OS specific path
591 9 return path($file)->canonpath ;
592 }
593  
594 # ----------------------------------------------------------------------------
595 # Returns a hash containing a formatted name for each option. For example:
596 # ( 'help|h|?' ) -> { 'help|h|?' => '-h, -?, --help' }
597 sub _desc_names
598 12 ▶{
Subroutine _desc_names: called CC=3 SCAR=11.0
599 12 my %descs ;
600 12 foreach my $o (@_) {
601 29 $_ = $o ; # Keep a copy of key in $o.
602 29 s/=.*$// ;
603  
604 # Sort by length so single letter options are shown first.
605 29 my @parts = sort { length $a <=> length $b } split /\|/ ;
606  
607 # Single chars get - prefix, names get -- prefix.
608 29 ▶ my $s = join ", ", map { ( length > 1 ? '--' : '-' ) . $_ } @parts ;
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 29 26 55
609  
610 29 $descs{$o} = $s ;
611 }
612 12 return %descs ;
613 }
614  
615 # ----------------------------------------------------------------------------
616 # special function to help us test this module, as it flags that we can die
617 # rather than exiting when doing some operations
618 # also test mode will not output to STDERR/STDOUT
619  
620 sub set_test_mode
621 1 ▶{
Subroutine set_test_mode: called CC=1 SCAR=0.0
Pod undocumented
622 1 $_test_mode = shift ;
623 }
624  
625 # ----------------------------------------------------------------------------
626  
627  
628  
629  
630 # saymd function taken and modied from
631 # echomd -- An md like conversion tool for shell terminals
632 # https://raw.githubusercontent.com/WebReflection/echomd/master/perl/echomd
633 # some mod's of my own
634  
635 #
636 # Fully inspired by the work of John Gruber
637 # <http://daringfireball.net/projects/markdown/>
638 #
639 # -----------------------------------------------------------------------------
640 # The MIT License (MIT)
641 # Copyright (c) 2016 Andrea Giammarchi - @WebReflection
642 #
643 # Permission is hereby granted, free of charge, to any person obtaining a
644 # copy of this software and associated documentation files (the "Software"),
645 # to deal in the Software without restriction, including without limitation
646 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
647 # and/or sell copies of the Software, and to permit persons to whom
648 # the Software is furnished to do so, subject to the following conditions:
649 #
650 # The above copyright notice and this permission notice shall be included
651 # in all copies or substantial portions of the Software.
652 #
653 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
654 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
655 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
656 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
657 # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
658 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
659 # THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
660 # -----------------------------------------------------------------------------
661  
662 # for *bold* _underline_ ~strike~ (strike on Linux only)
663 # it works with both double **__~~ or just single *_~
664 sub _bold_underline_strike
665 0 ▶{
Subroutine _bold_underline_strike: not called CC=1 SCAR=6.9
666 0 my ($txt) = @_ ;
667 0 $txt =~ s/(\*{1,2})(?=\S)(.+?)(?<=\S)\1/\x1B[1m$2\x1B[22m/gs ;
668 0 $txt =~ s/(\_{1,2})(?=\S)(.+?)(?<=\S)\1/\x1B[4m$2\x1B[24m/gs ;
669 0 $txt =~ s/(\~{1,2})(?=\S)(.+?)(?<=\S)\1/\x1B[9m$2\x1B[29m/gs ;
670 0 return $txt ;
671 }
672  
673 # for #color(text) or bg#bgcolor(text)
674 # virtually compatible with #RGBA(text)
675 # or for background via bg#RGBA(text)
676 sub _color
677 0 ▶{
Subroutine _color: not called CC=1 SCAR=6.9
678 0 my ($txt) = @_ ;
679 0 $txt =~ s{(bg)?#([a-zA-Z0-9]{3,8})\((.+?)\)(?!\))}
680 0 {_get_color($1,$2,$3)}egs ;
681 0 return $txt ;
682 }
683  
684 # for very important # Headers
685 # and for less important ## One
686 sub _header
687 0 ▶{
Subroutine _header: not called CC=1 SCAR=6.9
688 0 my ($txt) = @_ ;
689 0 $txt =~ s{^(\#{1,6})[ \t]+(.+?)[ \t]*\#*([\r\n]+|$)}
690 0 {_get_header($1,$2).$3}egm ;
691 0 return $txt ;
692 }
693  
694 # for horizontal lines
695 # --- or - - - or ___ or * * *
696 sub _horizontal
697 0 ▶{
Subroutine _horizontal: not called CC=1 SCAR=6.9
698 0 my ($txt) = @_ ;
699 0 my $line = "─" x 72 ;
700 0 $txt =~ s{^[ ]{0,2}([ ]?[\*_-][ ]?){3,}[ \t]*$}
701 {\x1B[1m$line\x1B[22m}gm ;
702 0 return $txt ;
703 }
704  
705 # for lists such:
706 # * list 1
707 # etc, etc
708 # * list 2
709 # * list 3
710 sub _list
711 0 ▶{
Subroutine _list: not called CC=1 SCAR=6.9
712 0 my ($txt) = @_ ;
713 0 $txt =~ s/^([ \t]{2,})[*+-]([ \t]{1,})/$1•$2/gm ;
714 0 return $txt ;
715 }
716  
717 # for quoted text such:
718 # > this is quote
719 # > this is the rest of the quote
720 sub _quote
721 0 ▶{
Subroutine _quote: not called CC=1 SCAR=6.9
722 0 my ($txt) = @_ ;
723 0 $txt =~ s/^[ \t]*>([ \t]?)/\x1B[7m$1\x1B[27m$1/gm ;
724 0 return $txt ;
725 }
726  
727 # HELPERS
728  
729 # used to grab colors by name
730 sub _get_color
731 0 ▶{
Subroutine _get_color: not called CC=12 SCAR=50.5
732 0 my $bg = $1 ;
733 0 my $rgb = $2 ;
734 0 my $out = "" ;
735 # one day, when it won't show experimental warnings
736 # given($rgb){
737 # when("black") { $out = "\x1B[30m" }
738 # when("red") { $out = "\x1B[31m" }
739 # when("green") { $out = "\x1B[32m" }
740 # when("blue") { $out = "\x1B[34m" }
741 # when("magenta") { $out = "\x1B[35m" }
742 # when("cyan") { $out = "\x1B[36m" }
743 # when("white") { $out = "\x1B[37m" }
744 # when("yellow") { $out = "\x1B[39m" }
745 # when("grey") { $out = "\x1B[90m" }
746 # }
747 0 ▶ if ( $rgb eq "black" ) {
Branch: 9 branches
BranchTrueFalseTotal
if ($rgb eq 'black') { } 0 0 0
elsif ($rgb eq 'red') { } 0 0 0
elsif ($rgb eq 'green') { } 0 0 0
elsif ($rgb eq 'blue') { } 0 0 0
elsif ($rgb eq 'magenta') { } 0 0 0
elsif ($rgb eq 'cyan') { } 0 0 0
elsif ($rgb eq 'white') { } 0 0 0
elsif ($rgb eq 'yellow') { } 0 0 0
elsif ($rgb eq 'grey') { } 0 0 0
748 0 $out = "\x1B[30m" ;
749 } elsif ( $rgb eq "red" ) {
750 0 $out = "\x1B[31m" ;
751 } elsif ( $rgb eq "green" ) {
752 0 $out = "\x1B[32m" ;
753 } elsif ( $rgb eq "blue" ) {
754 0 $out = "\x1B[34m" ;
755 } elsif ( $rgb eq "magenta" ) {
756 0 $out = "\x1B[35m" ;
757 } elsif ( $rgb eq "cyan" ) {
758 0 $out = "\x1B[36m" ;
759 } elsif ( $rgb eq "white" ) {
760 0 $out = "\x1B[37m" ;
761 } elsif ( $rgb eq "yellow" ) {
762 0 $out = "\x1B[39m" ;
763 } elsif ( $rgb eq "grey" ) {
764 0 $out = "\x1B[90m" ;
765 }
766 0 ▶ $out .= ( $out eq "" ) ? $3 : "$3\x1B[39m" ;
Branch: 1 branch
BranchTrueFalseTotal
$out eq '' ? : 0 0 0
767 0 ▶ return ( !defined $bg ) ? $out : "\x1B[7m$out\x1B[27m" ;
Branch: 1 branch
BranchTrueFalseTotal
!defined($bg) ? : 0 0 0
768 }
769  
770 sub _get_header
771 0 ▶{
Subroutine _get_header: not called CC=2 SCAR=17.9
772 0 my ( $hash, $txt ) = @_ ;
773 0 ▶ if ( length($hash) eq 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $hash eq 1) 0 0 0
774 0 $txt = "\x1B[1m$txt\x1B[22m" ;
775 }
776 0 return "\x1B[7m $txt \x1B[27m" ;
777 }
778  
779 # used to place parsed code back
780 sub _get_source
781 0 ▶{
Subroutine _get_source: not called CC=3 SCAR=24.8
782 0 my ($hash) = @_ ;
783 0 my %code = %{ $_[1] } ;
784 0 for my $source ( keys %code ) {
785 0 ▶ if ( $code{$source} eq $hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code{$source} eq $hash) 0 0 0
786 0 return $source ;
787 }
788 }
789 }
790  
791 # main transformer
792 # takes care of code blocks too
793 # without modifying their content
794 # inline `code blocks` as well as
795 # ```
796 # multiline code blocks
797 # ```
798 sub saymd
799 0 ▶{
Subroutine saymd: not called CC=1 SCAR=6.9
Pod documented
800 0 my ($txt) = @_ ;
801 0 my %code ;
802 # preserve code blocks
803 0 $txt =~ s{(`{2,})(.+?)(?<!`)\1(?!`)}
804 0 {$1.($code{$2}=md5_base64($2)).$1}egs ;
805 # preserve inline blocks too
806 0 $txt =~ s{(`)(.+?)\1}{$1.($code{$2}=md5_base64($2)).$1}egm ;
807 # converter everything else
808 0 $txt = _horizontal($txt) ;
809 0 $txt = _header($txt) ;
810 0 $txt = _bold_underline_strike($txt) ;
811 0 $txt = _list($txt) ;
812 0 $txt = _quote($txt) ;
813 0 $txt = _color($txt) ;
814 # put back inline blocks
815 0 $txt =~ s{(`)(.+?)\1}{$1._get_source($2,\%code).$1}egm ;
816 # put back code blocks too
817 0 $txt =~ s{(`{3})(.+?)(?<!`)\1(?!`)}
818 0 {$1._get_source($2,\%code).$1}egs ;
819 0 say $txt;
820 }
821  
822 # ----------------------------------------------------------------------------
823 # make sure we do any cleanup required
824  
825 2 ▶END {
Subroutine END: called CC=2 SCAR=10.5
826  
827 # call any user supplied cleanup
828 2 ▶ if ($_app_simple_cleanup_func) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_app_simple_cleanup_func) 0 2 2
829 0 $_app_simple_cleanup_func->() ;
830 0 $_app_simple_cleanup_func = undef ;
831 }
832 }
833  
834  
835 # ----------------------------------------------------------------------------
836  
837 2 1 ;
838  
839 __END__
↑ Summary Basis/Config.pm »