Skip to content

Instantly share code, notes, and snippets.

@ferki
Created January 27, 2020 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferki/19e6d6701cca801f77088289389079c4 to your computer and use it in GitHub Desktop.
Save ferki/19e6d6701cca801f77088289389079c4 to your computer and use it in GitHub Desktop.
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