line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Amon2::Plugin::CLI; |
2
|
7
|
|
|
7
|
|
108639
|
use strict; |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
261
|
|
3
|
7
|
|
|
7
|
|
37
|
use warnings; |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
198
|
|
4
|
|
|
|
|
|
|
|
5
|
7
|
|
|
7
|
|
1519
|
use Plack::Util; |
|
7
|
|
|
|
|
17542
|
|
|
7
|
|
|
|
|
180
|
|
6
|
7
|
|
|
7
|
|
806
|
use Amon2::Util qw/add_method/; |
|
7
|
|
|
|
|
63755
|
|
|
7
|
|
|
|
|
5557
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
my $CLI_OPT_KEY = '.'. __PACKAGE__; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub init { |
11
|
6
|
|
|
6
|
1
|
63
|
my ($self, $c, $code_conf) = @_; |
12
|
|
|
|
|
|
|
|
13
|
6
|
50
|
|
|
|
33
|
if ($code_conf->{cli_opt_key}) { |
14
|
0
|
|
|
|
|
0
|
$CLI_OPT_KEY = $code_conf->{cli_opt_key}; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
6
|
|
|
|
|
26
|
_load_getopt_long($code_conf->{getopt}); |
18
|
|
|
|
|
|
|
|
19
|
6
|
|
50
|
|
|
1241
|
add_method($c => ($code_conf->{run_method} || 'run'), _run($code_conf)); |
20
|
|
|
|
|
|
|
|
21
|
6
|
|
|
|
|
105
|
add_method($c => 'show_usage', \&_show_usage); |
22
|
6
|
|
|
|
|
55
|
add_method($c => 'parse_opt', \&_parse_opt); |
23
|
6
|
|
|
|
|
61
|
add_method($c => 'setopt', \&_setopt); |
24
|
6
|
|
|
|
|
61
|
add_method($c => 'getopt', \&_getopt); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _load_getopt_long { |
28
|
6
|
|
50
|
6
|
|
61
|
my $getopt = shift || [qw/:config posix_default no_ignore_case gnu_compat/]; |
29
|
|
|
|
|
|
|
|
30
|
6
|
|
|
|
|
7112
|
require Getopt::Long; |
31
|
6
|
|
|
|
|
68078
|
Getopt::Long->import(@{$getopt}); |
|
6
|
|
|
|
|
30
|
|
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _run { |
35
|
6
|
|
|
6
|
|
13
|
my $code_conf = shift; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
return sub { |
38
|
6
|
|
|
6
|
|
9274
|
my ($c, $arg) = @_; |
39
|
|
|
|
|
|
|
|
40
|
6
|
|
|
|
|
15
|
eval { |
41
|
6
|
100
|
|
|
|
32
|
if (my $before_run = $code_conf->{before_run}) { |
42
|
1
|
|
|
|
|
5
|
$before_run->($c, $arg); |
43
|
|
|
|
|
|
|
} |
44
|
6
|
100
|
|
|
|
82
|
if (ref $arg eq 'CODE') { |
45
|
3
|
|
|
|
|
11
|
$arg->($c); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
else { |
48
|
3
|
|
|
|
|
23
|
my $runner = Plack::Util::load_class($arg, $code_conf->{base}); |
49
|
3
|
|
50
|
|
|
2097
|
my $method = $code_conf->{method} || 'main'; |
50
|
3
|
|
|
|
|
26
|
$runner->$method($c); |
51
|
|
|
|
|
|
|
} |
52
|
5
|
100
|
|
|
|
259
|
if (my $after_run = $code_conf->{after_run}) { |
53
|
1
|
|
|
|
|
4
|
$after_run->($c, $arg); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
}; |
56
|
6
|
100
|
|
|
|
69
|
if (my $e = $@) { |
57
|
1
|
50
|
|
|
|
4
|
if ($code_conf->{on_error}) { |
58
|
0
|
|
|
|
|
0
|
$code_conf->{on_error}->($c, $e); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
1
|
|
|
|
|
6
|
_croak("$0\t$e"); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
6
|
|
|
|
|
48
|
}; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _show_usage { |
68
|
1
|
|
|
1
|
|
1112
|
my ($self, %args) = @_; |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
912
|
require Pod::Usage; |
71
|
1
|
|
|
|
|
53139
|
Pod::Usage::pod2usage(%args); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _parse_opt { |
75
|
1
|
|
|
1
|
|
9
|
my ($c, %options) = @_; |
76
|
|
|
|
|
|
|
|
77
|
1
|
|
|
|
|
4
|
my @cli_args = @ARGV; # save @ARGV |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Getopt::Long::GetOptionsFromArray( |
80
|
|
|
|
|
|
|
\@cli_args, |
81
|
|
|
|
|
|
|
%options, |
82
|
|
|
|
|
|
|
'h' => sub { |
83
|
0
|
|
|
0
|
|
0
|
$c->show_usage(-exitval => 1); |
84
|
|
|
|
|
|
|
}, |
85
|
|
|
|
|
|
|
'help' => sub { |
86
|
0
|
|
|
0
|
|
0
|
$c->show_usage(-exitval => 1, -verbose => 2); |
87
|
|
|
|
|
|
|
}, |
88
|
1
|
50
|
|
|
|
14
|
) or $c->show_usage(-exitval => 2); |
89
|
|
|
|
|
|
|
|
90
|
1
|
|
|
|
|
360
|
return $c; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _setopt { |
94
|
1
|
|
|
1
|
|
2
|
my ($c, $opt) = @_; |
95
|
|
|
|
|
|
|
|
96
|
1
|
50
|
|
|
|
5
|
_croak('$opt is not HASH') unless ref $opt eq 'HASH'; |
97
|
|
|
|
|
|
|
|
98
|
1
|
|
|
|
|
8
|
$c->{$CLI_OPT_KEY} = $opt; |
99
|
|
|
|
|
|
|
|
100
|
1
|
|
|
|
|
3
|
return $c; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _getopt { |
104
|
1
|
|
|
1
|
|
18
|
my ($c, $opt_key) = @_; |
105
|
|
|
|
|
|
|
|
106
|
1
|
50
|
33
|
|
|
9
|
if (!defined $opt_key || $opt_key eq '') { |
107
|
0
|
|
|
|
|
0
|
return $c->{$CLI_OPT_KEY}; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
1
|
|
|
|
|
56
|
return $c->{$CLI_OPT_KEY}{$opt_key}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub _croak { |
114
|
1
|
|
|
1
|
|
2
|
my ($msg) = @_; |
115
|
|
|
|
|
|
|
|
116
|
1
|
|
|
|
|
7
|
require Carp; |
117
|
1
|
|
|
|
|
178
|
Carp::croak($msg); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
1; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
__END__ |