| 1 | package DBIx::FileStore::ConfigFile; | ||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 3 | ▶ | use List::Util qw(first); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use File::Spec::Functions qw(catfile); # to concat dirs & filenames portably | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use Config::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use DBIx::FileStore::UtilityFunctions qw(get_user_homedir); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | ▶ | use fields qw( vars_hash verbose ); # you can set $obj->{verbose} for debugging | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | ###################################### | ||||||||||
| 13 | # my $conf = new DBIx::FileStore::ConfigFile() | ||||||||||
| 14 | sub new { | ||||||||||
| 15 | 0 | ▶ | my DBIx::FileStore::ConfigFile $self = shift; | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 16 | 0 | ▶ | unless (ref $self) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 0 | $self = fields::new($self); | |||||||||
| 18 | } | ||||||||||
| 19 | 0 | return $self; | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | ############################################ | ||||||||||
| 24 | # my $hashref = $conf->read_config_file() | ||||||||||
| 25 | # or | ||||||||||
| 26 | # my $hashref = $conf->read_config_file( "/etc/fdb-alternate.conf" ) | ||||||||||
| 27 | sub read_config_file { | ||||||||||
| 28 | 0 | ▶ | my ($self, $opt_filename) = @_; | ||||||||
|
Subroutine
read_config_file: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 29 | |||||||||||
| 30 | # choose which config file we're going to use | ||||||||||
| 31 | 0 | my $filename = $opt_filename; | |||||||||
| 32 | 0 | my $user_dotfile = catfile( get_user_homedir(), ".fdbrc" ); # ~/.fdbrc | |||||||||
| 33 | 0 | my $etc_conffile = "/etc/fdb.conf"; | |||||||||
| 34 | 0 | ▶ | unless( $filename ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | # explicit sub{} like this for perl 5.6.2 | ||||||||||
| 36 | 0 | ▶ | $filename = first( sub { -e }, $user_dotfile, $etc_conffile ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | unless($filename) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | die ("$0: Can't find config file to open " . | |||||||||
| 41 | "(tried $user_dotfile and $etc_conffile)\n"); | ||||||||||
| 42 | } | ||||||||||
| 43 | 0 | ▶ | print "$0: reading $filename\n" if $self->{verbose}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | |||||||||||
| 45 | 0 | $self->{vars_hash} = Config::File::read_config_file( $filename ); | |||||||||
| 46 | |||||||||||
| 47 | # sanity check that there's a dbuser passed. | ||||||||||
| 48 | 0 | ▶ | unless( $self->{vars_hash}->{dbuser} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | warn "$0: Can't find a 'dbuser' setting in $filename\n"; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 0 | return $self->{vars_hash}; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 3 | 1; | |||||||||
| 56 | |||||||||||
| 57 | =pod | ||||||||||
| 58 | |||||||||||
| 59 | =head1 NAME | ||||||||||
| 60 | |||||||||||
| 61 | DBIx::FileStore::ConfigFile -- Find and read filestore conf files. | ||||||||||
| 62 | |||||||||||
| 63 | =head1 SYNOPSIS | ||||||||||
| 64 | |||||||||||
| 65 | my $conf = new DBIx::FileStore::ConfigFile(); | ||||||||||
| 66 | my $hashref = $conf->read_config_file(); | ||||||||||
| 67 | |||||||||||
| 68 | # these are the fields we use, along wth dbpasswd | ||||||||||
| 69 | print "db: $hashref->{dbname}, user: $hashref->{dbuser}\n"; | ||||||||||
| 70 | |||||||||||
| 71 | =head1 DESCRIPTION | ||||||||||
| 72 | |||||||||||
| 73 | Provides interface to read DBIx::FileStore configuration files. | ||||||||||
| 74 | |||||||||||
| 75 | The read_config_file() method reads from the optionally | ||||||||||
| 76 | passed configuration file, the file .fdbrc in the user's | ||||||||||
| 77 | home directory , or /etc/fdb.conf, whichever is found first. | ||||||||||
| 78 | |||||||||||
| 79 | =head1 METHODS | ||||||||||
| 80 | |||||||||||
| 81 | =over 4 | ||||||||||
| 82 | |||||||||||
| 83 | =item new DBIx::FileStore::ConfigFile(); | ||||||||||
| 84 | |||||||||||
| 85 | my $conf = new DBIx::FileStore::ConfigFile(); | ||||||||||
| 86 | |||||||||||
| 87 | Returns a new DBIx::FileStore::ConfigFile object. | ||||||||||
| 88 | |||||||||||
| 89 | =item $conf->read_config_file() | ||||||||||
| 90 | |||||||||||
| 91 | my $conf_hash = $conf->read_config_file(); | ||||||||||
| 92 | |||||||||||
| 93 | my $conf_hash = $conf->read_config_file( $filename ) | ||||||||||
| 94 | |||||||||||
| 95 | Returns a hashref with the name/value pairs parsed from | ||||||||||
| 96 | the configuration file. The settings expected by | ||||||||||
| 97 | DBIx-Filestore are: dbname, dbuser, and dbpasswd. | ||||||||||
| 98 | |||||||||||
| 99 | If a $filename is passed by the caller, that file is used as | ||||||||||
| 100 | the configuration file. Otherwise the module uses | ||||||||||
| 101 | the file .fdbrc in the current user's home directory, | ||||||||||
| 102 | or /etc/fdb.conf, whichever is found first. | ||||||||||
| 103 | |||||||||||
| 104 | If no configuration file can be found, the method dies | ||||||||||
| 105 | with an error message. | ||||||||||
| 106 | |||||||||||
| 107 | =back | ||||||||||
| 108 | |||||||||||
| 109 | =head1 COPYRIGHT | ||||||||||
| 110 | |||||||||||
| 111 | Copyright (c) 2010-2015 Josh Rabinowitz, All Rights Reserved. | ||||||||||
| 112 | |||||||||||
| 113 | =head1 AUTHORS | ||||||||||
| 114 | |||||||||||
| 115 | Josh Rabinowitz | ||||||||||
| 116 | |||||||||||
| 117 | =cut | ||||||||||
| 118 | |||||||||||