File Coverage

lib/Rex/Interface/Executor/Default.pm
Criterion Covered Total %
statement 53 56 94.6
branch 11 14 78.5
condition 1 3 33.3
subroutine 9 9 100.0
pod 0 2 0.0
total 74 84 88.1


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Interface::Executor::Default;
6              
7 55     55   828 use v5.12.5;
  55         221  
8 55     55   425 use warnings;
  55         157  
  55         2898  
9              
10             our $VERSION = '1.14.2.3'; # TRIAL VERSION
11              
12 55     55   721 use Rex::Hook;
  55         165  
  55         3609  
13 55     55   409 use Rex::Logger;
  55         132  
  55         398  
14 55     55   1510 use Data::Dumper;
  55         212  
  55         2513  
15              
16 55     55   769 use Rex::Interface::Executor::Base;
  55         153  
  55         683  
17 55     55   1834 use base qw(Rex::Interface::Executor::Base);
  55         191  
  55         25919  
18              
19             require Rex::Args;
20              
21             sub new {
22 213     213 0 517 my $that = shift;
23 213   33     999 my $proto = ref($that) || $that;
24 213         516 my $self = {@_};
25              
26 213         466 bless( $self, $proto );
27              
28 213         1334 return $self;
29             }
30              
31             sub exec {
32 54     54 0 225 my ( $self, $opts, $args ) = @_;
33              
34 54         210 my $task = $self->{task};
35              
36 54         695 Rex::Logger::debug( "Executing " . $task->name );
37              
38 54         241 my $wantarray = wantarray;
39              
40 54         164 my @ret;
41 54         178 eval {
42 54         422 my $code = $task->code;
43              
44 54         281 Rex::Hook::run_hook( task => "before_execute", $task->name, @_ );
45              
46 54 100       251 if ($wantarray) {
47 1 50       6 if ( ref $opts eq "ARRAY" ) {
48 1         2 @ret = $code->( @{$opts} );
  1         4  
49             }
50             else {
51 0         0 @ret = $code->( $opts, $args );
52             }
53             }
54             else {
55 53 100       330 if ( ref $opts eq "ARRAY" ) {
56 13         36 $ret[0] = $code->( @{$opts} );
  13         133  
57             }
58             else {
59 40         770 $ret[0] = $code->( $opts, $args );
60             }
61             }
62              
63 47         20531 Rex::Hook::run_hook( task => "after_execute", $task->name, @_ );
64             };
65              
66 54         397 my $error = $@;
67 54         1320 my %opts = Rex::Args->getopts;
68              
69 54 100       309 if ($error) {
70 7 50       53 if ( exists $opts{o} ) {
71 0         0 Rex::Output->get->add( $task->name, error => 1, msg => $error );
72             }
73             else {
74 7         118 Rex::Logger::info( "Error executing task:", "error" );
75 7         47 Rex::Logger::info( "$error", "error" );
76 7         120 die($error);
77             }
78             }
79             else {
80 47 50       289 if ( exists $opts{o} ) {
81 0         0 Rex::Output->get->add( $task->name );
82             }
83             }
84              
85 47 100       244 if ($wantarray) {
86 1         4 return @ret;
87             }
88             else {
89 46         430 return $ret[0];
90             }
91             }
92              
93             1;