BusyBird/Runner.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 118 CC 8 SCAR 18.3
CC
8
Cov
0%
prepare_plack_opts
12.0
_ensure_config_dir_exists
12.0
_ensure_config_file_exists
12.0
CRAP 6.3
1 package BusyBird::Runner;
2 use v5.8.0;
3 use strict;
4 use warnings;
5 use Getopt::Long qw(GetOptionsFromArray :config no_ignore_case bundling);
6 use BusyBird::Util qw(config_directory config_file_path);
7 use File::Spec;
8 use Try::Tiny;
9 use Plack::Runner ();
10 use Exporter 5.57 qw(import);
11  
12 our @EXPORT_OK = qw(run);
13  
14 sub run {
15 my (@argv) = @_;
16 my @plack_opts = try {
17 prepare_plack_opts(@argv);
18 }catch {
19 ();
20 };
21 return 1 if !@plack_opts;
22 my $runner = Plack::Runner->new;
23 $runner->parse_options(@plack_opts);
24 $runner->run();
25 return undef;
26 }
27  
28 sub prepare_plack_opts {
29 my (@argv) = @_;
30 my $need_help = 0;
31 my $bind_host = "127.0.0.1";
32 my $bind_port = 5000;
33 my $config_script;
34 GetOptionsFromArray(
35 \@argv,
36 "h|help" => \$need_help,
37 "o|host=s" => \$bind_host,
38 "p|port=i" => \$bind_port
39 ) or die "command-line error";
40 die "need help" if $need_help;
41 _ensure_config_dir_exists();
42 $config_script = shift @argv;
43 if(defined($config_script)) {
44 _ensure_config_file_exists($config_script);
45 }else {
46 $config_script = config_file_path("config.psgi");
47 _ensure_config_file_exists($config_script, 1);
48 }
49 return ("--no-default-middleware", "-a", $config_script,
50 "-o", $bind_host, "-p", $bind_port, "-s", "Twiggy");
51 }
52  
53 sub _ensure_config_dir_exists {
54 my $dir = config_directory;
55 if(! -d $dir) {
56 if(!mkdir $dir) {
57 warn "Cannot create config directory $dir: $!\n";
58 die "_check_config_dir";
59 }
60 }
61 }
62  
63 sub _ensure_config_file_exists {
64 my ($filepath, $create_default) = @_;
65 if(! -f $filepath) {
66 if($create_default) {
67 _create_default_config_file($filepath);
68 }else {
69 my $msg = "Cannot access config file $filepath. Maybe it does not exist.";
70 warn "$msg\n";
71 die $msg;
72 }
73 }
74 }
75  
76 sub _create_default_config_file {
77 my ($config_filepath) = @_;
78 require File::ShareDir;
79 require File::Copy;
80 my $sample = File::Spec->catfile(File::ShareDir::dist_dir("BusyBird"), "sample_config.psgi");
81 File::Copy::copy($sample, $config_filepath) or do {
82 warn "Error while copying $sample to $config_filepath: $!\n";
83 die "_create_default_config_file";
84 };
85 }
86  
87  
88 1;
89 __END__