| 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
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
| |||||||||||||||||||||||||||||
| 67 | 9 | ▶ | $go->show_usage if $go->opts->{help}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 9 | my ($args) = @{$go->args}; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 9 | ▶ | if ( -f '.foreman' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 76 | 9 | %opt = ( | |||||||||||||||||||||||||||
| 77 | %opt, | ||||||||||||||||||||||||||||
| 78 | %$yaml_opt | ||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 9 | ▶ | if ( $cmd eq 'start' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 9 | ▶ | if ( ! length($opt{envfile}) && -f '.env') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not length $opt{"envfile"} and -f ".env"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 101 | 18 | ▶ | $worker = 0 if defined $args && $process ne $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args and $process ne $args
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 119 | 9 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 27 | ▶ | next if $line =~ /^\s*#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 18 | ▶ | if (my ($name, $command) = ($line =~ /^([^:]+)\s*:\s*(.+)$/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if (my ($name, $val) = ($line =~ /^([^=]+)\s*=\s*(.+)/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||