blib/lib/Beekeeper/Config.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 26 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 153 SCAR 60.4
CC
20
Cov
0%
read_config_file
72.0
get_bus_group_config
42.0
get_bus_config
20.0
CRAP 420.0
1 package Beekeeper::Config;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.09';
7  
8 use JSON::XS;
9 use Carp;
10  
11 my %Cache;
12 my $Config_dir;
13  
14  
15 sub set_config_dir {
16 my ($class, $dir) = @_;
17  
18 croak "Couldn't read config files from $dir: directory does not exist\n" unless ($dir && -d $dir);
19  
20 $Config_dir = $dir;
21 }
22  
23 sub get_bus_config {
24 my ($class, %args) = @_;
25  
26 my $bus_id = $args{'bus_id'};
27  
28 croak "bus_id was not specified" unless ($bus_id);
29  
30 my $config = $class->read_config_file( 'bus.config.json' );
31  
32 croak "Couldn't read config file bus.config.json: file not found\n" unless defined ($config);
33  
34 my %bus_cfg = map { $_->{'bus_id'} => $_ } @$config;
35  
36 return ($bus_id eq '*') ? \%bus_cfg : $bus_cfg{$bus_id};
37 }
38  
39 sub get_pool_config {
40 my ($class, %args) = @_;
41  
42 my $pool_id = $args{'pool_id'};
43  
44 croak "pool_id was not specified" unless ($pool_id);
45  
46 my $config = $class->read_config_file( 'pool.config.json' );
47  
48 croak "Couldn't read config file pool.config.json: file not found\n" unless defined ($config);
49  
50 my %pool_cfg = map { $_->{'pool_id'} => $_ } @$config;
51  
52 return ($pool_id eq '*') ? \%pool_cfg : $pool_cfg{$pool_id};
53 }
54  
55 sub get_bus_group_config {
56 my ($class, %args) = @_;
57  
58 my $bus_role = $args{'bus_role'};
59 my $bus_id = $args{'bus_id'};
60 my @group_config;
61  
62 croak "No bus_role or bus_id was specified" unless ($bus_id || $bus_role);
63  
64 my $config = $class->read_config_file( 'bus.config.json' );
65  
66 if ($bus_role) {
67  
68 @group_config = grep { defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role } @$config;
69 }
70 elsif ($bus_id) {
71  
72 my ($bus_config) = grep { $_->{'bus_id'} eq $bus_id } @$config;
73 return [] unless $bus_config;
74  
75 $bus_role = $bus_config->{'bus_role'};
76 return [ $bus_config ] unless $bus_role;
77  
78 @group_config = grep {
79 (defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role) || $_->{'bus_id'} eq $bus_id
80 } @$config;
81 }
82  
83 return \@group_config;
84 }
85  
86 sub read_config_file {
87 my ($class, $file) = @_;
88  
89 croak "Couldn't read config file: filename was not specified\n" unless ($file);
90  
91 my $cdir;
92 $cdir = $Config_dir;
93 $cdir = $ENV{'BEEKEEPER_CONFIG_DIR'} unless ($cdir && -d $cdir);
94 $cdir = '~/.config/beekeeper' unless ($cdir && -d $cdir);
95 $cdir = '/etc/beekeeper' unless ($cdir && -d $cdir);
96  
97 $file = "$cdir/$file";
98  
99 return $Cache{$file} if exists $Cache{$file};
100  
101 return undef unless (-e $file);
102  
103 local($/);
104 open(my $fh, '<', $file) or croak "Couldn't read config file $file: $!";
105 my $data = <$fh>;
106 close($fh);
107  
108 # Allow comments and end-comma
109 my $json = JSON::XS->new->utf8->relaxed;
110  
111 my $config = eval { $json->decode($data) };
112  
113 if ($@) {
114 croak "Couldn't parse config file $file: Invalid JSON syntax";
115 }
116  
117 $Cache{$file} = $config;
118  
119 return $config;
120 }
121  
122 1;
123  
124 __END__