File Coverage

blib/lib/App/Notifier/Service.pm
Criterion Covered Total %
statement 26 37 70.2
branch 0 6 0.0
condition 0 5 0.0
subroutine 9 11 81.8
pod n/a
total 35 59 59.3


line stmt bran cond sub pod time code
1             package App::Notifier::Service;
2             $App::Notifier::Service::VERSION = '0.0901';
3 2     2   1071 use 5.014;
  2         7  
4              
5 2     2   771 use Mojolicious::Lite -signatures;
  2         901575  
  2         16  
6 2     2   49467 use Plack::Builder;
  2         15062  
  2         124  
7              
8 2     2   13 use File::Spec ();
  2         4  
  2         36  
9 2     2   9 use JSON::MaybeXS qw( decode_json );
  2         4  
  2         108  
10 2     2   743 use YAML::XS qw( LoadFile );
  2         4809  
  2         95  
11 2     2   1019 use List::MoreUtils qw();
  2         22232  
  2         60  
12              
13 2     2   17 use POSIX ":sys_wait_h";
  2         5  
  2         18  
14 2     2   355 use Errno;
  2         3  
  2         1977  
15              
16             sub _REAPER
17             {
18 0     0     local $!; # don't let waitpid() overwrite current error
19 0   0       while ( ( my $pid = waitpid( -1, WNOHANG ) ) > 0 && WIFEXITED($?) )
20             {
21             }
22 0           $SIG{CHLD} = \&_REAPER; # loathe SysV
23             }
24              
25             # $SIG{CHLD} = \&_REAPER;
26              
27             my $config_fn = ( $ENV{'NOTIFIER_CONFIG'}
28             || File::Spec->catfile( $ENV{HOME}, '.app_notifier.yml' ) );
29              
30             my $config;
31              
32             sub _process_cmd_line_arg
33             {
34 0     0     my ( $arg, $text_params ) = @_;
35              
36 0 0         if ( ref($arg) eq '' )
    0          
37             {
38 0           return $arg;
39             }
40             elsif ( ref($arg) eq 'HASH' )
41             {
42 0 0         if ( $arg->{type} eq 'text_param' )
43             {
44 0   0       return ( $text_params->{ $arg->{param_name} } // '' );
45             }
46             else
47             {
48 0           die +{ msg => "Unknown special argument type $arg->{type}", };
49             }
50             }
51             else
52             {
53 0           die +{ msg =>
54             "Unhandled perl type for argument in command line template (should be string or hash.",
55             };
56             }
57              
58 0           return;
59             }
60              
61             get '/notify' => sub {
62             my $c = shift;
63              
64             $config ||= LoadFile($config_fn);
65              
66             my $cmd_id = ( $c->req->param('cmd_id') || 'default' );
67             my $text_params = {};
68             if ( defined( my $text_params_as_json = $c->req->param('text_params') ) )
69             {
70             $text_params = decode_json($text_params_as_json);
71             if ( ref($text_params) ne 'HASH' )
72             {
73             return "Invalid text_params - should be a JSON hash.\n";
74             }
75             elsif ( List::MoreUtils::any { ref($_) ne '' } values(%$text_params) )
76             {
77             return "Invalid text_params - all values must be strings.\n";
78             }
79             }
80             my $cmd = $config->{commands}->{$cmd_id};
81              
82             if ( defined($cmd) )
83             {
84             my @before_cmd_line = ( ( ref($cmd) eq 'ARRAY' ) ? @$cmd : $cmd );
85              
86             my @cmd_line = eval {
87             map { _process_cmd_line_arg( $_, $text_params ); } @before_cmd_line;
88             };
89              
90             if ( my $Err = $@ )
91             {
92             if ( ref($Err) eq 'HASH' and ( exists( $Err->{msg} ) ) )
93             {
94             return ( $Err->{msg} . "\n" );
95             }
96             else
97             {
98             die $Err;
99             }
100             }
101              
102             my $pid;
103             if ( !defined( $pid = fork() ) )
104             {
105             die "Cannot fork: $!";
106             }
107             elsif ( !$pid )
108             {
109             # I'm the child.
110             if ( fork() eq 0 )
111             {
112             # I'm the grandchild.
113             system { $cmd_line[0] } @cmd_line;
114             }
115             POSIX::_exit(0);
116             }
117             else
118             {
119             waitpid( $pid, 0 );
120             }
121             $c->render( text => "Success.\n" );
122             }
123             else
124             {
125             $c->log("Unknown Command ID '$cmd_id'.");
126             return "Unknown Command ID.\n";
127             }
128             };
129              
130             get '/' => sub {
131             my $c = shift;
132             $c->render( text => "OK\n" );
133             };
134              
135             builder
136             {
137             # enable 'Deflater';
138             app->start;
139             };
140              
141             # "true";
142              
143             # start;
144              
145             __END__