| 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.09'; | |||||||||||||||||||||||||||
| 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 | sub set_config_dir { | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | my ($class, $dir) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_config_dir: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | croak "Couldn't read config files from $dir: directory does not exist\n" unless ($dir && -d $dir); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$dir and -d $dir
MC/DC0%0/2
$dir and -d $dir
$dir
-d $dir
Truth table0%0/3
A and B
A= $dirB= -d $dir
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | $Config_dir = $dir; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub get_bus_config { | ||||||||||||||||||||||||||||
| 24 | 7 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_bus_config: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 7 | my $bus_id = $args{'bus_id'}; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 7 | ▶ | croak "bus_id was not specified" unless ($bus_id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub get_pool_config { | ||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_pool_config: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | my $pool_id = $args{'pool_id'}; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | croak "pool_id was not specified" unless ($pool_id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub get_bus_group_config { | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_bus_group_config: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$bus_id or $bus_role
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
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $config = $class->read_config_file( 'bus.config.json' ); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ($bus_role) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | @group_config = grep { defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role } @$config; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | $bus_role = $bus_config->{'bus_role'}; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | return [ $bus_config ] unless $bus_role; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | @group_config = grep { | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | (defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role) || $_->{'bus_id'} eq $bus_id | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $_->{"bus_role"} and $_->{"bus_role"} eq $bus_role
MC/DC0%0/2
defined $_->{"bus_role"} and $_->{"bus_role"} eq $bus_role
defined $_->{"bus_role"}
$_->{"bus_role"} eq $bus_role
Truth table0%0/3
A and B
A= defined $_->{"bus_role"}B= $_->{"bus_role"} eq $bus_role
| |||||||||||||||||||||||||||||
| 80 | } @$config; | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | return \@group_config; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub read_config_file { | ||||||||||||||||||||||||||||
| 87 | 10 | ▶ | my ($class, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read_config_file: not called CC=12 SCAR=48.6
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 10 | ▶ | croak "Couldn't read config file: filename was not specified\n" unless ($file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 10 | my $cdir; | |||||||||||||||||||||||||||
| 92 | 0 | $cdir = $Config_dir; | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | $cdir = $ENV{'BEEKEEPER_CONFIG_DIR'} unless ($cdir && -d $cdir); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cdir and -d $cdir
MC/DC0%0/2
$cdir and -d $cdir
$cdir
-d $cdir
Truth table0%0/3
A and B
A= $cdirB= -d $cdir
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $cdir = '~/.config/beekeeper' unless ($cdir && -d $cdir); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cdir and -d $cdir
MC/DC0%0/2
$cdir and -d $cdir
$cdir
-d $cdir
Truth table0%0/3
A and B
A= $cdirB= -d $cdir
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $cdir = '/etc/beekeeper' unless ($cdir && -d $cdir); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cdir and -d $cdir
MC/DC0%0/2
$cdir and -d $cdir
$cdir
-d $cdir
Truth table0%0/3
A and B
A= $cdirB= -d $cdir
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | $file = "$cdir/$file"; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return $Cache{$file} if exists $Cache{$file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return undef unless (-e $file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | local($/); | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | open(my $fh, '<', $file) or croak "Couldn't read config file $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||