| 1 | package Exobrain::Config; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use parent 'Config::Tiny'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Hash::Merge::Simple qw(merge); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use File::XDG; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | # ABSTRACT: Reads exobrain config | ||||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | 1 | our $VERSION = '1.08'; # VERSION | |||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=5 SCAR=17.6
Pod
documented
| |||||||||||
| 15 | 1 | my ($class) = @_; | |||||||||
| 16 | |||||||||||
| 17 | 1 | my @config_files; | |||||||||
| 18 | |||||||||||
| 19 | 1 | ▶ | if ($ENV{EXOBRAIN_CONFIG}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | # Read directory from environment variable | ||||||||||
| 21 | 1 | ▶ | @config_files = glob("$ENV{EXOBRAIN_CONFIG}/*"); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | } | ||||||||||
| 23 | else { | ||||||||||
| 24 | # Use defaults, including ~/.exobrainrc if exists | ||||||||||
| 25 | |||||||||||
| 26 | 0 | my $config_dir = File::XDG->new(name => 'exobrain')->config_home; | |||||||||
| 27 | 0 | @config_files = glob("$config_dir/*"); | |||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | push(@config_files,"$ENV{HOME}/.exobrainrc") if -f "$ENV{HOME}/.exobrainrc"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 1 | my $config = {}; | |||||||||
| 33 | |||||||||||
| 34 | 1 | foreach my $file (@config_files) { | |||||||||
| 35 | |||||||||||
| 36 | 3 | my $indiv_config = $class->read($file); | |||||||||
| 37 | |||||||||||
| 38 | 3 | ▶ | $indiv_config or die "Cannot read config - " . Config::Tiny->errstr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | # Merge Each each individual config into our main one. | ||||||||||
| 41 | |||||||||||
| 42 | 3 | $config = merge( $config, $indiv_config ); | |||||||||
| 43 | |||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | # This may change in the future, but for now we're still | ||||||||||
| 47 | # acting a Config::Tiny object. | ||||||||||
| 48 | 1 | return bless($config,$class); | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | sub write_config { | ||||||||
|
Subroutine
write_config: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 53 | 0 | my ($class, $file, $contents) = @_; | |||||||||
| 54 | |||||||||||
| 55 | 0 | my $config_file; | |||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | if ($ENV{EXOBRAIN_CONFIG}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | $config_file = "ENV{EXOBRAIN_CONFIG}/$file"; | |||||||||
| 59 | } | ||||||||||
| 60 | else { | ||||||||||
| 61 | 0 | my $config_dir = File::XDG->new(name => 'exobrain')->config_home; | |||||||||
| 62 | |||||||||||
| 63 | # Make our config dir if it doesn't exist. | ||||||||||
| 64 | 0 | ▶ | if (not -e $config_dir) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | mkdir($config_dir) | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | $config_file = "$config_dir/$file"; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 0 | open(my $fh, '>', $config_file); | |||||||||
| 72 | |||||||||||
| 73 | 0 | print {$fh} $contents; | |||||||||
| 74 | |||||||||||
| 75 | 0 | close($fh); | |||||||||
| 76 | |||||||||||
| 77 | 0 | return $config_file; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 1 | 1; | |||||||||
| 81 | |||||||||||
| 82 | __END__ | ||||||||||