| 1 | package App::RepoSync::SystemUtil; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use Cwd; | ||
| 5 | use Exporter::Lite; | ||
| 6 | our @EXPORT_OK = qw(system_or_die chdir_qx); | ||
| 7 | use Term::ANSIColor; | ||
| 8 | |||
| 9 | =head1 | ||
| 10 | |||
| 11 | App::RepoSync::SystemUtil - system util functions | ||
| 12 | |||
| 13 | =head2 system_or_die | ||
| 14 | |||
| 15 | @param arrayref|string $command | ||
| 16 | @param string $description | ||
| 17 | @param string $chdir | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | sub system_or_die { | ||
| 22 | my ($command,$description,$chdir) = @_; | ||
| 23 | my $cwd = getcwd(); | ||
| 24 | $description ||= $command; | ||
| 25 | chdir $chdir if $chdir; | ||
| 26 | |||
| 27 | my $ret = 0; | ||
| 28 | $ret = system( $command ); | ||
| 29 | |||
| 30 | my $q = $?; | ||
| 31 | my $exit_value = $q >> 8; | ||
| 32 | # killing signal is lower 7-bits of top byte, which was shifted to lower byte | ||
| 33 | my $signal_num = $q & 127; | ||
| 34 | # core-dump flag is top bit | ||
| 35 | my $dumped_core = $q & 128; | ||
| 36 | |||
| 37 | |||
| 38 | if ($q != -1 && (($q & 127) == 2) && (!($q & 128))) { | ||
| 39 | # Drop the "$? & 128" if you want to include failures that generated coredump | ||
| 40 | die color('yellow') ,"$command: interrupted" , color 'reset'; | ||
| 41 | } elsif( $q != 0 ) { | ||
| 42 | print color 'red'; | ||
| 43 | print "$description failed: $?\n"; | ||
| 44 | print color 'reset'; | ||
| 45 | } | ||
| 46 | chdir $cwd if $chdir; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | =head2 chdir_qx | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub chdir_qx { | ||
| 55 | my ($cmd,$chdir) = @_; | ||
| 56 | my $cwd = getcwd(); | ||
| 57 | chdir $chdir; | ||
| 58 | my $ret = qx($cmd); | ||
| 59 | chdir $cwd; | ||
| 60 | return $ret; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | 1; |