Skip to content

Commit

Permalink
Use SIG{__WARN__} when loading Rexfile
Browse files Browse the repository at this point in the history
  • Loading branch information
uralm1 authored and ferki committed Jan 15, 2023
1 parent 85934b6 commit dfe68be
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Revision history for Rex
- Correct example for file_read command

[ENHANCEMENTS]
- Use $SIG{__WARN__} when loading Rexfile instead stderr redirection

[MAJOR]

Expand Down
30 changes: 15 additions & 15 deletions lib/Rex/CLI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ sub __run__ {
%opts = Rex::Args->getopts;

if ( $opts{'Q'} ) {
my ( $stdout, $stderr );
my $stdout;
open( my $newout, '>', \$stdout );
select $newout;
close(STDERR);
Expand Down Expand Up @@ -744,14 +744,11 @@ sub load_rexfile {
}
};

my ( $stdout, $stderr, $default_stderr );
open $default_stderr, ">&", STDERR;

# we close STDERR here because we don't want to see the
# normal perl error message on the screen. Instead we print
# the error message in the catch-if below.
local *STDERR;
open( STDERR, ">>", \$stderr );
# we don't want to see the
# normal perl warning message on the screen. Instead we print
# the warning message in the catch-if below
my @warnings;
local $SIG{__WARN__} = sub { push @warnings, $_[0] };

# we can't use $rexfile here, because if the variable contains dots
# the perl interpreter try to load the file directly without using @INC
Expand All @@ -761,13 +758,16 @@ sub load_rexfile {
# update %INC so that we can later use it to find the rexfile
$INC{"__Rexfile__.pm"} = $rexfile;

# reopen STDERR
open STDERR, ">&", $default_stderr;

if ($stderr) {
my @lines = split( $/, $stderr );
if (@warnings) {
Rex::Logger::info( "You have some code warnings:", 'warn' );
Rex::Logger::info( "\t$_", 'warn' ) for @lines;
for (@warnings) {

# remove /loader/.../ prefix before filename
## no critic (RegularExpressions);
s|/loader/[^/]+/||;
## use critic
Rex::Logger::info( "\t$_", 'warn' );
}
}

1;
Expand Down

0 comments on commit dfe68be

Please sign in to comment.