Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Show diff of file changes during a Rex run
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
You can’t perform that action at this time.