Created
January 27, 2020 17:59
-
-
Save ferki/19e6d6701cca801f77088289389079c4 to your computer and use it in GitHub Desktop.
Show diff of file changes during a Rex run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Rex::Hook::File::Diff; | |
use strict; | |
use warnings; | |
use Rex -base; | |
use Rex::Hook; | |
use Text::Diff; | |
register_function_hooks { before_change => { file => \&show_diff, }, }; | |
sub show_diff { | |
my ( $file, @opts ) = @_; | |
my $temp_file = get_rex_temp_file_path($file); | |
say "Diff for: $file"; | |
$file = '/dev/null' unless is_file($file); # creating file | |
$temp_file = '/dev/null' unless is_file($temp_file); # removing file | |
print diff $file, $temp_file; | |
} | |
sub get_rex_temp_file_path { | |
my $file = shift; | |
# copied verbatim from Rex::Commands::File | |
# TODO: expose it directly from Rex core | |
my @splitted_file = split( /[\/\\]/, $file ); | |
my $file_name = ".rex.tmp." . pop(@splitted_file); | |
my $tmp_file_name = ( | |
$#splitted_file != -1 | |
? ( join( "/", @splitted_file ) . "/" . $file_name ) | |
: $file_name | |
); | |
return $tmp_file_name; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment