| 1 | # | ||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | Rex::Commands::Tail - Tail a file | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | Version <= 1.0: All these functions will not be reported. | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | All these functions are not idempotent. | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | With this module you can tail a file. | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | tail "/var/log/syslog"; | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | package Rex::Commands::Tail; | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 32 | ▶ | use v5.14.4; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 32 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 32 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 32 | require Rex::Exporter; | |||||||||||||||||||
| 34 | 32 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 32 | ▶ | use Rex::Commands::Fs; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 32 | ▶ | use Rex::Commands::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | #use Rex::Helper::Run; | ||||||||||||||||||||
| 39 | 32 | ▶ | use Rex::Commands::Run; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 32 | ▶ | use vars qw(@EXPORT); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | 32 | ▶ | use base qw(Rex::Exporter); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 32 | @EXPORT = qw(tail); | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =head2 tail($file) | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | This function will tail the given file. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | task "syslog", "server01", sub { | ||||||||||||||||||||
| 51 | tail "/var/log/syslog"; | ||||||||||||||||||||
| 52 | }; | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | If you want to control the output format, you can define a callback function: | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | task "syslog", "server01", sub { | ||||||||||||||||||||
| 57 | tail "/var/log/syslog", sub { | ||||||||||||||||||||
| 58 | my ($data) = @_; | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | my $server = Rex->get_current_connection()->{'server'}; | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | print "$server>> $data\n"; | ||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | ▶ | sub tail { | ||||||||||||||||||
|
Subroutine
tail: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 71 | 0 | my $file = shift; | |||||||||||||||||||
| 72 | 0 | my $callback = shift; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | ▶ | $callback ||= sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 75 | 0 | print $_[0]; | |||||||||||||||||||
| 76 | 0 | ▶ | }; | ||||||||||||||||||
|
Condition0%0/3
$callback ||= sub {
print $_[0];
}
MC/DC0%0/2
$callback ||= sub {
print $_[0];
}
$callback
sub {
print $_[0];
}
Truth table0%0/3
A ||= B
A= $callbackB= sub {
print $_[0];
}
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | Rex::Logger::debug("Tailing: $file"); | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | ▶ | run "tail -f $file", continuous_read => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 81 | 0 | $callback->(@_); | |||||||||||||||||||
| 82 | }, | ||||||||||||||||||||
| 83 | 0 | ; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 32 | 1; | |||||||||||||||||||