bin/proclet

filter: Statementstmt 88.8% 64 / 72 Branchbran 53.1% 17 / 32 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 88.8% 8 / 9 Podpod n/a totaltotal 75.0% 96 / 128 CC 10 SCAR 19.0
CC
10
Cov
76%
load_envfile
20.0
load_procfile
5.0
parse_concurrency
3.1
CRAP 6.7
↑ Summary lib/Proclet.pm »
1 #!/usr/bin/perl
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Getopt::Compact::WithCmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Proclet;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 our $VERSION = $Proclet::VERSION;
11  
12 9 my %opt;
13 9 $opt{concurrency} = '';
14 9 $opt{procfile} = 'Procfile';
15 9 $opt{envfile} = '';
16 9 $opt{color} = 0;
17 9 $opt{port} = 5000;
18  
19 my $go = Getopt::Compact::WithCmd->new(
20 name => 'proclet',
21 version => $Proclet::VERSION,
22 args => '[service]',
23 command_struct => {
24 'start' => {
25 options => [
26 [
27 [qw/c concurrency/],
28 'the number of each process type to run. The value passed in should be in the format process=num,process=num',
29 '=s',
30 \$opt{concurrency},
31 { required => 0 },
32 ],
33 [
34 [qw/e env/],
35 'an alternate environment file. You can specify more than one file by using: --env file1,file2.',
36 '=s',
37 \$opt{envfile},
38 { required => 0 }
39 ],
40 [
41 [qw/f procfile/],
42 'an alternate Procfile to load, implies -d at the Procfile root',
43 '=s',
44 \$opt{procfile},
45 { required => 0 }
46 ],
47 [
48 [qw/color/],
49 'colored log',
50 '!',
51 \$opt{color},
52 { required => 0 }
53 ],
54 [
55 [qw/p port/],
56 'Port number which used as the base for this application. Should be a multiple of 1000',
57 '=i',
58 \$opt{port},
59 9 { required => 0 }
60 ],
61 ],
62 }
63 },
64 );
65  
66 9 ▶my $cmd = $go->command || $go->show_usage;
Condition33%1/3
$go->command || $go->show_usage
l!l&&r!l&&!r
900
MC/DC0%0/2
$go->command || $go->show_usage
$go->command $go->show_usage
Truth table33%1/3
A || B
A= $go->commandB= $go->show_usage
ABresult
1X1
011
000
67 9 ▶$go->show_usage if $go->opts->{help};
Branch: 1 branch
BranchTrueFalseTotal
if $go->opts->{"help"} 0 9 9
68 9 my ($args) = @{$go->args};
69  
70 9 ▶if ( -f '.foreman' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f ".foreman") 9 0 9
71 9 my $yaml_opt;
72 9 eval {
73 9 $yaml_opt = YAML::XS::LoadFile('.foreman');
74 };
75 9 ▶ die 'cannot load .foreman as yaml: '. $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
76 9 %opt = (
77 %opt,
78 %$yaml_opt
79 );
80 }
81  
82 9 ▶if ( $cmd eq 'start' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq 'start') { } 9 0 9
83  
84 9 ▶ if ( ! length($opt{envfile}) && -f '.env') {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $opt{'envfile'} and -f '.env') { } 0 9 9
Condition33%1/3
not length $opt{"envfile"} and -f ".env"
!ll&&!rl&&r
090
MC/DC0%0/2
not length $opt{"envfile"} and -f ".env"
not length $opt{"envfile"} -f ".env"
Truth table33%1/3
not(A) and B
A= not length $opt{"envfile"}B= -f ".env"
ABresult
0X0
100
111
85 0 load_envfile('.env');
86 }
87 else {
88 9 my @envfile = split /,/, $opt{envfile};
89 9 load_envfile($_) for @envfile;
90 }
91  
92 9 my $services = load_procfile($opt{procfile});
93 9 my $concurrency = parse_concurrency($opt{concurrency});
94  
95 9 ▶ die 'no services defined' unless @$services;
Branch: 1 branch
BranchTrueFalseTotal
unless @$services 0 9 9
96  
97 9 my $proclet = Proclet->new( color => $opt{color}, _base_port => $opt{port} );
98 9 for my $service ( @$services ) {
99 18 my ($process, $command) = @$service;
100 18 ▶ my $worker = exists $concurrency->{$process} ? $concurrency->{$process} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $concurrency->{$process} ? : 9 9 18
101 18 ▶ $worker = 0 if defined $args && $process ne $args;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args and $process ne $args 4 14 18
Condition100%3/3
defined $args and $process ne $args
!ll&&!rl&&r
1044
MC/DC100%2/2
defined $args and $process ne $args
defined $args $process ne $args
Truth table100%3/3
A and B
A= defined $argsB= $process ne $args
ABresult
0X0
100
111
102 18 $proclet->service(
103 tag => $process,
104 code => $command,
105 worker => $worker,
106 );
107 }
108 9 $proclet->run();
109 }
110 else {
111 0 $go->show_usage()
112 }
113  
114  
115 9 ▶sub load_procfile {
Subroutine load_procfile: called CC=5 SCAR=16.2
116 9 my $file = shift;
117 9 my @services;
118 9 ▶ open(my $fh, '<:utf8', $file) or die "cannot load procfile $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $file or die "cannot load procfile ${file}: $!" 9 0 9
119 9 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 27 9 36
120 27 ▶ next if $line =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#/ 9 18 27
121 18 ▶ if (my ($name, $command) = ($line =~ /^([^:]+)\s*:\s*(.+)$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $line =~ /^([^:]+)\s*:\s*(.+)$/) 18 0 18
122 18 push @services, [$name, $command];
123 }
124 }
125 9 return \@services;
126 }
127  
128 0 ▶sub load_envfile {
Subroutine load_envfile: not called CC=4 SCAR=30.0
129 0 my $file = shift;
130 0 ▶ open(my $fh, '<:utf8', $file) or die "cannot load envfile $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $file or die "cannot load envfile ${file}: $!" 0 0 0
131 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
132 0 ▶ if (my ($name, $val) = ($line =~ /^([^=]+)\s*=\s*(.+)/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $line =~ /^([^=]+)\s*=\s*(.+)/) 0 0 0
133 0 $ENV{$name} = $val;
134 }
135 }
136 }
137  
138 9 ▶sub parse_concurrency {
Subroutine parse_concurrency: called CC=3 SCAR=11.3
139 9 my $opt = shift;
140 9 my %concurrency;
141 9 for my $process ( split /,/, $opt ) {
142 9 ▶ if ( $process =~ /^\s*([^=]+)\s*=\s*(\d+)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($process =~ /^\s*([^=]+)\s*=\s*(\d+)\s*$/) { } 9 0 9
143 9 $concurrency{$1} = $2;
144 }
145 else {
146 0 die "incorrect concurrency option, near '$process'";
147 }
148 }
149 9 return \%concurrency;
150 }
151  
152 __END__
↑ Summary lib/Proclet.pm »