line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Rolling; |
2
|
5
|
|
|
5
|
|
271116
|
use strict; |
|
5
|
|
|
|
|
15
|
|
|
5
|
|
|
|
|
187
|
|
3
|
5
|
|
|
5
|
|
40
|
use warnings; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
167
|
|
4
|
5
|
|
|
5
|
|
27
|
use Carp qw/croak/; |
|
5
|
|
|
|
|
79
|
|
|
5
|
|
|
|
|
300
|
|
5
|
5
|
|
|
5
|
|
6261
|
use Getopt::Long qw/GetOptionsFromArray/; |
|
5
|
|
|
|
|
83837
|
|
|
5
|
|
|
|
|
38
|
|
6
|
5
|
|
|
5
|
|
6679
|
use Pod::Usage; |
|
5
|
|
|
|
|
313818
|
|
|
5
|
|
|
|
|
787
|
|
7
|
5
|
|
|
5
|
|
4544
|
use Path::Class qw/dir file/; |
|
5
|
|
|
|
|
216643
|
|
|
5
|
|
|
|
|
425
|
|
8
|
5
|
|
|
5
|
|
48
|
use IO::File; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
850
|
|
9
|
5
|
|
|
5
|
|
4742
|
use IO::Interactive qw/is_interactive/; |
|
5
|
|
|
|
|
20221
|
|
|
5
|
|
|
|
|
38
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.12'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub run { |
14
|
1
|
|
|
1
|
1
|
944
|
my ($class, @argv) = @_; |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
|
|
5
|
my %config = $class->_process(@argv); |
17
|
|
|
|
|
|
|
|
18
|
1
|
50
|
|
|
|
4
|
if (!$config{file}) { |
19
|
0
|
|
|
|
|
0
|
croak "-f --file is required"; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
else { |
22
|
1
|
|
|
|
|
7
|
$class->_roll(%config); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
|
|
5
|
return 1; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _process { |
29
|
1
|
|
|
1
|
|
4
|
my ($self, @argv) = @_; |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
4
|
my %config = $self->_config_read; |
32
|
|
|
|
|
|
|
|
33
|
1
|
50
|
|
|
|
66
|
pod2usage(2) unless @argv; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
GetOptionsFromArray( |
36
|
|
|
|
|
|
|
\@argv, |
37
|
|
|
|
|
|
|
'f|file=s' => \$config{file}, |
38
|
|
|
|
|
|
|
'a|age=i' => \$config{age}, |
39
|
|
|
|
|
|
|
'i|interval=i' => \$config{interval}, |
40
|
|
|
|
|
|
|
't|through' => \$config{through}, |
41
|
|
|
|
|
|
|
'nr|no-rotate' => \$config{no_rotate}, |
42
|
|
|
|
|
|
|
'h|help' => sub { |
43
|
0
|
|
|
0
|
|
0
|
pod2usage(2); |
44
|
|
|
|
|
|
|
}, |
45
|
|
|
|
|
|
|
'v|version' => sub { |
46
|
0
|
|
|
0
|
|
0
|
print "roll v$App::Rolling::VERSION\n"; |
47
|
0
|
|
|
|
|
0
|
exit 1; |
48
|
|
|
|
|
|
|
}, |
49
|
1
|
50
|
|
|
|
15
|
) or pod2usage(2); |
50
|
|
|
|
|
|
|
|
51
|
1
|
50
|
|
|
|
735
|
$config{file} = shift @argv unless $config{file}; |
52
|
|
|
|
|
|
|
|
53
|
1
|
50
|
|
|
|
5
|
$config{age} = 5 unless $config{age}; |
54
|
1
|
50
|
|
|
|
4
|
$config{interval} = 60 unless $config{interval}; |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
8
|
return %config; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _config_read { |
60
|
1
|
|
|
1
|
|
3
|
my $self = shift; |
61
|
|
|
|
|
|
|
|
62
|
1
|
|
|
|
|
5
|
my $filename = $self->_config_file; |
63
|
|
|
|
|
|
|
|
64
|
1
|
50
|
|
|
|
212
|
return unless -e $filename; |
65
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
|
|
0
|
my $fh = IO::File->new($filename, '<') |
67
|
|
|
|
|
|
|
or croak "[ERROR] couldn't open config file $filename: $!"; |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
0
|
my %config; |
70
|
0
|
|
|
|
|
0
|
while (<$fh>) { |
71
|
0
|
|
|
|
|
0
|
chomp; |
72
|
0
|
0
|
|
|
|
0
|
next if /\A\s*\Z/sm; |
73
|
0
|
0
|
|
|
|
0
|
if (/\A(\w+):\s*(.+)\Z/sm) { $config{$1} = $2; } |
|
0
|
|
|
|
|
0
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
0
|
undef $fh; |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
0
|
return %config; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _config_file { |
82
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
83
|
|
|
|
|
|
|
|
84
|
1
|
|
50
|
|
|
8
|
my $configdir = $ENV{'ROLLING_DIR'} || ''; |
85
|
|
|
|
|
|
|
|
86
|
1
|
50
|
33
|
|
|
11
|
if ( !$configdir && $ENV{'HOME'} ) { |
87
|
1
|
|
|
|
|
7
|
$configdir = dir( $ENV{'HOME'}, '.rolling' ); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
1
|
|
|
|
|
198
|
return file($configdir, 'config'); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _roll { |
94
|
1
|
|
|
1
|
|
5
|
my ($self, %config) = @_; |
95
|
|
|
|
|
|
|
|
96
|
1
|
50
|
|
|
|
5
|
if ( !is_interactive() ) { |
97
|
1
|
|
|
|
|
50
|
while ( my $line = ) { |
98
|
5
|
|
|
|
|
20
|
my $age_id = int(time / $config{interval}); |
99
|
5
|
50
|
|
|
|
18
|
my $now_suffix = $config{no_rotate} ? '' : '.' . $age_id; |
100
|
5
|
50
|
33
|
|
|
37
|
if (!$config{no_rotate} && $config{age}) { |
101
|
5
|
|
|
|
|
8
|
my $old_suffix = $age_id - $config{age}; |
102
|
5
|
50
|
|
|
|
76
|
if (-e "$config{file}\.$old_suffix") { |
103
|
0
|
|
|
|
|
0
|
unlink "$config{file}\.$old_suffix"; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
5
|
|
|
|
|
12
|
my $file = "$config{file}$now_suffix"; |
107
|
5
|
50
|
|
|
|
31
|
my $fh = IO::File->new($file, '>>') |
108
|
|
|
|
|
|
|
or croak "[ERROR] could't open file $file: $!"; |
109
|
5
|
|
|
|
|
495
|
$fh->print($line); |
110
|
5
|
|
|
|
|
58
|
undef $fh; |
111
|
5
|
50
|
|
|
|
333
|
print $line if $config{through}; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
1; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
__END__ |