lib/Beekeeper/Config.pm

filter: Statementstmt 36.1% 26 / 72 Branchbran 10.0% 4 / 40 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 55.5% 5 / 9 Podpod 60.0% 3 / 5 totaltotal 23.0% 38 / 165 CC 29 SCAR 43.2
CC
29
Cov
22%
read_config_file
129.6
get_bus_group_config
110.0
get_pool_config
20.0
CRAP 75.5
« lib/Beekeeper/Service/ToyBroker/Worker.pm ↑ Summary blib/lib/auto/share/dist/Beekeeper/flood/flood.pl »
1 package Beekeeper::Config;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.10';
7  
8 11 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 my %Cache;
12 my $Config_dir;
13  
14  
15 0 ▶sub set_config_dir {
Subroutine set_config_dir: not called CC=3 SCAR=24.8
Pod undocumented
16 0 my ($class, $dir) = @_;
17  
18 0 ▶ croak "Couldn't read config files from $dir: directory does not exist\n" unless ($dir && -d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless $dir and -d $dir 0 0 0
Condition0%0/3
$dir and -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
$dir and -d $dir
$dir -d $dir
Truth table0%0/3
A and B
A= $dirB= -d $dir
ABresult
0X0
100
111
19  
20 0 $Config_dir = $dir;
21 }
22  
23 7 ▶sub get_bus_config {
Subroutine get_bus_config: called CC=4 SCAR=14.0
Pod documented
24 7 my ($class, %args) = @_;
25  
26 7 my $bus_id = $args{'bus_id'};
27  
28 7 ▶ croak "bus_id was not specified" unless ($bus_id);
Branch: 1 branch
BranchTrueFalseTotal
unless $bus_id 0 7 7
29  
30 7 my $config = $class->read_config_file( 'bus.config.json' );
31  
32 7 ▶ croak "Couldn't read config file bus.config.json: file not found\n" unless defined ($config);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config 0 7 7
33  
34 7 my %bus_cfg = map { $_->{'bus_id'} => $_ } @$config;
35  
36 7 ▶ return ($bus_id eq '*') ? \%bus_cfg : $bus_cfg{$bus_id};
Branch: 1 branch
BranchTrueFalseTotal
$bus_id eq '*' ? : 6 1 7
37 }
38  
39 0 ▶sub get_pool_config {
Subroutine get_pool_config: not called CC=4 SCAR=30.0
Pod documented
40 0 my ($class, %args) = @_;
41  
42 0 my $pool_id = $args{'pool_id'};
43  
44 0 ▶ croak "pool_id was not specified" unless ($pool_id);
Branch: 1 branch
BranchTrueFalseTotal
unless $pool_id 0 0 0
45  
46 0 my $config = $class->read_config_file( 'pool.config.json' );
47  
48 0 ▶ croak "Couldn't read config file pool.config.json: file not found\n" unless defined ($config);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config 0 0 0
49  
50 0 my %pool_cfg = map { $_->{'pool_id'} => $_ } @$config;
51  
52 0 ▶ return ($pool_id eq '*') ? \%pool_cfg : $pool_cfg{$pool_id};
Branch: 1 branch
BranchTrueFalseTotal
$pool_id eq '*' ? : 0 0 0
53 }
54  
55 0 ▶sub get_bus_group_config {
Subroutine get_bus_group_config: not called CC=10 SCAR=47.0
Pod undocumented
56 0 my ($class, %args) = @_;
57  
58 0 my $bus_role = $args{'bus_role'};
59 0 my $bus_id = $args{'bus_id'};
60 0 my @group_config;
61  
62 0 ▶ croak "No bus_role or bus_id was specified" unless ($bus_id || $bus_role);
Branch: 1 branch
BranchTrueFalseTotal
unless $bus_id or $bus_role 0 0 0
Condition0%0/3
$bus_id or $bus_role
l!l&&r!l&&!r
000
MC/DC0%0/2
$bus_id or $bus_role
$bus_id $bus_role
Truth table0%0/3
A or B
A= $bus_idB= $bus_role
ABresult
1X1
011
000
63  
64 0 my $config = $class->read_config_file( 'bus.config.json' );
65  
66 0 ▶ if ($bus_role) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bus_role) { } 0 0 0
elsif ($bus_id) { } 0 0 0
67  
68 0 ▶ @group_config = grep { defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role } @$config;
Condition0%0/3
defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role
defined $_->{'bus_role'} $_->{'bus_role'} eq $bus_role
Truth table0%0/3
A && B
A= defined $_->{'bus_role'}B= $_->{'bus_role'} eq $bus_role
ABresult
0X0
100
111
69 }
70 elsif ($bus_id) {
71  
72 0 my ($bus_config) = grep { $_->{'bus_id'} eq $bus_id } @$config;
73 0 ▶ return [] unless $bus_config;
Branch: 1 branch
BranchTrueFalseTotal
unless $bus_config 0 0 0
74  
75 0 $bus_role = $bus_config->{'bus_role'};
76 0 ▶ return [ $bus_config ] unless $bus_role;
Branch: 1 branch
BranchTrueFalseTotal
unless $bus_role 0 0 0
77  
78 @group_config = grep {
79 0 ▶ (defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role) || $_->{'bus_id'} eq $bus_id
Condition0%0/6
defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role || $_->{'bus_id'} eq $bus_id
l!l&&r!l&&!r
000
defined $_->{"bus_role"} and $_->{"bus_role"} eq $bus_role
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role || $_->{'bus_id'} eq $bus_id
defined $_->{"bus_role"} $_->{"bus_role"} eq $bus_role $_->{'bus_id'} eq $bus_id
Truth table0%0/5
A and B || C
A= defined $_->{"bus_role"}B= $_->{"bus_role"} eq $bus_roleC= $_->{'bus_id'} eq $bus_id
ABCresult
11X1
0X11
1011
0X00
1000
80 } @$config;
81 }
82  
83 0 return \@group_config;
84 }
85  
86 0 ▶sub read_config_file {
Subroutine read_config_file: not called CC=12 SCAR=48.6
Pod documented
87 10 my ($class, $file) = @_;
88  
89 10 ▶ croak "Couldn't read config file: filename was not specified\n" unless ($file);
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
90  
91 10 my $cdir;
92 0 $cdir = $Config_dir;
93 0 ▶ $cdir = $ENV{'BEEKEEPER_CONFIG_DIR'} unless ($cdir && -d $cdir);
Branch: 1 branch
BranchTrueFalseTotal
unless $cdir and -d $cdir 0 0 0
Condition0%0/3
$cdir and -d $cdir
!ll&&!rl&&r
000
MC/DC0%0/2
$cdir and -d $cdir
$cdir -d $cdir
Truth table0%0/3
A and B
A= $cdirB= -d $cdir
ABresult
0X0
100
111
94 0 ▶ $cdir = '~/.config/beekeeper' unless ($cdir && -d $cdir);
Branch: 1 branch
BranchTrueFalseTotal
unless $cdir and -d $cdir 0 0 0
Condition0%0/3
$cdir and -d $cdir
!ll&&!rl&&r
000
MC/DC0%0/2
$cdir and -d $cdir
$cdir -d $cdir
Truth table0%0/3
A and B
A= $cdirB= -d $cdir
ABresult
0X0
100
111
95 0 ▶ $cdir = '/etc/beekeeper' unless ($cdir && -d $cdir);
Branch: 1 branch
BranchTrueFalseTotal
unless $cdir and -d $cdir 0 0 0
Condition0%0/3
$cdir and -d $cdir
!ll&&!rl&&r
000
MC/DC0%0/2
$cdir and -d $cdir
$cdir -d $cdir
Truth table0%0/3
A and B
A= $cdirB= -d $cdir
ABresult
0X0
100
111
96  
97 0 $file = "$cdir/$file";
98  
99 0 ▶ return $Cache{$file} if exists $Cache{$file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Cache{$file} 0 0 0
100  
101 0 ▶ return undef unless (-e $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
102  
103 0 local($/);
104 0 ▶ open(my $fh, '<', $file) or croak "Couldn't read config file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("Couldn't read config file ${file}: $!") 0 0 0
105 0 my $data = <$fh>;
106 0 close($fh);
107  
108 # Allow comments and end-comma
109 0 my $json = JSON::XS->new->utf8->relaxed;
110  
111 0 my $config = eval { $json->decode($data) };
112  
113 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
114 0 croak "Couldn't parse config file $file: Invalid JSON syntax";
115 }
116  
117 0 $Cache{$file} = $config;
118  
119 0 return $config;
120 }
121  
122 1 1;
123  
124 __END__
« lib/Beekeeper/Service/ToyBroker/Worker.pm ↑ Summary blib/lib/auto/share/dist/Beekeeper/flood/flood.pl »