| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Beekeeper::Config; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 11 |  |  | 11 |  | 1227 | use strict; | 
|  | 11 |  |  |  |  | 25 |  | 
|  | 11 |  |  |  |  | 359 |  | 
| 4 | 11 |  |  | 11 |  | 60 | use warnings; | 
|  | 11 |  |  |  |  | 23 |  | 
|  | 11 |  |  |  |  | 496 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '0.08'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 11 |  |  | 11 |  | 9312 | use JSON::XS; | 
|  | 11 |  |  |  |  | 41614 |  | 
|  | 11 |  |  |  |  | 685 |  | 
| 9 | 11 |  |  | 11 |  | 90 | use Carp; | 
|  | 11 |  |  |  |  | 24 |  | 
|  | 11 |  |  |  |  | 9141 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | my %Cache; | 
| 12 |  |  |  |  |  |  | my $Config_dir; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub set_config_dir { | 
| 16 | 0 |  |  | 0 | 0 | 0 | my ($class, $dir) = @_; | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 0 | 0 | 0 |  |  | 0 | croak "Couldn't read config files from $dir: directory does not exist\n" unless ($dir && -d $dir); | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 0 |  |  |  |  | 0 | $Config_dir = $dir; | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub get_bus_config { | 
| 24 | 7 |  |  | 7 | 1 | 5009795 | my ($class, %args) = @_; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 7 |  |  |  |  | 46 | my $bus_id = $args{'bus_id'}; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 7 | 50 |  |  |  | 83 | croak "bus_id was not specified" unless ($bus_id); | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 7 |  |  |  |  | 129 | my $config = $class->read_config_file( 'bus.config.json' ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 7 | 50 |  |  |  | 1075 | croak "Couldn't read config file bus.config.json: file not found\n" unless defined ($config); | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 7 |  |  |  |  | 106 | my %bus_cfg  = map { $_->{'bus_id'}  => $_ } @$config; | 
|  | 7 |  |  |  |  | 135 |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 7 | 100 |  |  |  | 149 | return ($bus_id eq '*') ? \%bus_cfg : $bus_cfg{$bus_id}; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub get_pool_config { | 
| 40 | 0 |  |  | 0 | 1 |  | my ($class, %args) = @_; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 0 |  |  |  |  |  | my $pool_id = $args{'pool_id'}; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 0 | 0 |  |  |  |  | croak "pool_id was not specified" unless ($pool_id); | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 0 |  |  |  |  |  | my $config = $class->read_config_file( 'pool.config.json' ); | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 | 0 |  |  |  |  | croak "Couldn't read config file pool.config.json: file not found\n" unless defined ($config); | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  |  |  |  | my %pool_cfg = map { $_->{'pool_id'} => $_ } @$config; | 
|  | 0 |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 0 | 0 |  |  |  |  | return ($pool_id eq '*') ? \%pool_cfg : $pool_cfg{$pool_id}; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub get_bus_group_config { | 
| 56 | 0 |  |  | 0 | 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 | 0 | 0 |  |  |  | croak "No bus_role or bus_id was specified" unless ($bus_id || $bus_role); | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 |  |  |  |  |  | my $config = $class->read_config_file( 'bus.config.json' ); | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 | 0 |  |  |  |  | if ($bus_role) { | 
|  |  | 0 |  |  |  |  |  | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 0 | 0 |  |  |  |  | @group_config = grep { defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role } @$config; | 
|  | 0 |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | elsif ($bus_id) { | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 |  |  |  |  |  | my ($bus_config) = grep { $_->{'bus_id'} eq $bus_id } @$config; | 
|  | 0 |  |  |  |  |  |  | 
| 73 | 0 | 0 |  |  |  |  | return [] unless $bus_config; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 0 |  |  |  |  |  | $bus_role = $bus_config->{'bus_role'}; | 
| 76 | 0 | 0 |  |  |  |  | return [ $bus_config ] unless $bus_role; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | @group_config = grep { | 
| 79 | 0 | 0 | 0 |  |  |  | (defined $_->{'bus_role'} && $_->{'bus_role'} eq $bus_role) || $_->{'bus_id'} eq $bus_id | 
|  | 0 |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | } @$config; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 0 |  |  |  |  |  | return \@group_config; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub read_config_file { | 
| 87 | 0 |  |  | 0 | 1 |  | my ($class, $file) = @_; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 0 | 0 |  |  |  |  | croak "Couldn't read config file: filename was not specified\n" unless ($file); | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 0 |  |  |  |  |  | my $cdir; | 
| 92 | 0 |  |  |  |  |  | $cdir = $Config_dir; | 
| 93 | 0 | 0 | 0 |  |  |  | $cdir = $ENV{'BEEKEEPER_CONFIG_DIR'} unless ($cdir && -d $cdir); | 
| 94 | 0 | 0 | 0 |  |  |  | $cdir = '~/.config/beekeeper'        unless ($cdir && -d $cdir); | 
| 95 | 0 | 0 | 0 |  |  |  | $cdir = '/etc/beekeeper'             unless ($cdir && -d $cdir); | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 0 |  |  |  |  |  | $file = "$cdir/$file"; | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 0 | 0 |  |  |  |  | return $Cache{$file} if exists $Cache{$file}; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 0 | 0 |  |  |  |  | return undef unless (-e $file); | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 0 |  |  |  |  |  | local($/); | 
| 104 | 0 | 0 |  |  |  |  | open(my $fh, '<', $file) or croak "Couldn't read config file $file: $!"; | 
| 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) }; | 
|  | 0 |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 0 | 0 |  |  |  |  | if ($@) { | 
| 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; | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | __END__ |