File Coverage

blib/lib/Amon2/Plugin/CLI.pm
Criterion Covered Total %
statement 57 62 91.9
branch 13 18 72.2
condition 4 9 44.4
subroutine 13 15 86.6
pod 1 1 100.0
total 88 105 83.8


line stmt bran cond sub pod time code
1             package Amon2::Plugin::CLI;
2 7     7   106189 use strict;
  7         15  
  7         176  
3 7     7   35 use warnings;
  7         11  
  7         168  
4              
5 7     7   830 use Plack::Util;
  7         12114  
  7         179  
6 7     7   783 use Amon2::Util qw/add_method/;
  7         22082  
  7         5568  
7              
8             my $CLI_OPT_KEY = '.'. __PACKAGE__;
9              
10             sub init {
11 6     6 1 70 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     1182 add_method($c => ($code_conf->{run_method} || 'run'), _run($code_conf));
20              
21 6         74 add_method($c => 'show_usage', \&_show_usage);
22 6         53 add_method($c => 'parse_opt', \&_parse_opt);
23 6         60 add_method($c => 'setopt', \&_setopt);
24 6         60 add_method($c => 'getopt', \&_getopt);
25             }
26              
27             sub _load_getopt_long {
28 6   50 6   64 my $getopt = shift || [qw/:config posix_default no_ignore_case gnu_compat/];
29              
30 6         6917 require Getopt::Long;
31 6         69191 Getopt::Long->import(@{$getopt});
  6         31  
32             }
33              
34             sub _run {
35 6     6   13 my $code_conf = shift;
36              
37             return sub {
38 6     6   7276 my ($c, $arg) = @_;
39              
40 6         11 eval {
41 6 100       28 if (my $before_run = $code_conf->{before_run}) {
42 1         6 $before_run->($c, $arg);
43             }
44 6 100       78 if (ref $arg eq 'CODE') {
45 3         11 $arg->($c);
46             }
47             else {
48 3         20 my $runner = Plack::Util::load_class($arg, $code_conf->{base});
49 3   50     1687 my $method = $code_conf->{method} || 'main';
50 3         23 $runner->$method($c);
51             }
52 5 100       167 if (my $after_run = $code_conf->{after_run}) {
53 1         4 $after_run->($c, $arg);
54             }
55             };
56 6 100       61 if (my $e = $@) {
57 1 50       3 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   1161 my ($self, %args) = @_;
69              
70 1         908 require Pod::Usage;
71 1         58373 Pod::Usage::pod2usage(%args);
72             }
73              
74             sub _parse_opt {
75 1     1   9 my ($c, %options) = @_;
76              
77 1         3 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       13 ) or $c->show_usage(-exitval => 2);
89              
90 1         358 return $c;
91             }
92              
93             sub _setopt {
94 1     1   3 my ($c, $opt) = @_;
95              
96 1 50       4 _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   17 my ($c, $opt_key) = @_;
105              
106 1 50 33     8 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         6 require Carp;
117 1         224 Carp::croak($msg);
118             }
119              
120             1;
121              
122             __END__