File Coverage

blib/lib/DBIx/FileStore/ConfigFile.pm
Criterion Covered Total %
statement 18 33 54.5
branch 0 8 0.0
condition n/a
subroutine 6 8 75.0
pod 1 1 100.0
total 25 50 50.0


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