lib/Plerd/Util.pm

filter: Statementstmt 58.1% 25 / 43 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 72.7% 8 / 11 Podpod 0.0% 0 / 1 totaltotal 52.3% 33 / 63 CC 6 SCAR 23.7
CC
6
Cov
49%
read_config_file
30.0
__ANON__
6.0
__ANON__
2.0
CRAP 10.8
« lib/Plerd/Post.pm ↑ Summary lib/Plerd/Tag.pm »
1 package Plerd::Util;
2  
3 # This is just some utility functions for Plerd, with no intended public API.
4  
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use YAML qw( LoadFile );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Path::Class::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # read_config_file: Pass in a param representing a config file location, which
15 # can be undef if we don't have one. Apply fallbacks if
16 # needed, then try to parse it as YAML and return the
17 # resulting data structure.
18 0 ▶sub read_config_file {
Subroutine read_config_file: not called CC=5 SCAR=34.0
Pod undocumented
19 0 my ( $config_file ) = @_;
20  
21 0 ▶ unless ( defined $config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config_file) 0 0 0
22 # As fallback config locations, try ./plerd.conf, conf/plerd.conf,
23 # ~/.plerd, then (for historical reasons) $bin/../conf/plerd.conf.
24 # Then give up.
25 0 my $local_file = Path::Class::File->new( getcwd, 'plerd.conf' );
26 0 my $nearby_file = Path::Class::File->new( getcwd, 'conf', 'plerd.conf' );
27 0 my $dotfile = Path::Class::File->new( File::HomeDir->my_home, '.plerd' );
28 0 foreach (
29 $local_file,
30 $nearby_file,
31 $dotfile,
32 "$FindBin::Bin/../conf/plerd.conf",
33 ) {
34 0 ▶ if ( -r $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $_) 0 0 0
35 0 $config_file = $_;
36 0 last;
37 }
38 }
39 0 ▶ unless ( defined $config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config_file) 0 0 0
40 0 die "Can't start $0: I can't find a Plerd config file in "
41 . "$local_file, $nearby_file, $dotfile, or in "
42 . "$FindBin::Bin/../conf/plerd.conf, and "
43 . "no other location was specified as a command-line argument.";
44 }
45 }
46  
47 0 my $config_ref;
48 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 $config_ref = LoadFile( $config_file );
50 }
51 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
52 0 ▶ if ( -r $config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $config_file) { } 0 0 0
53 0 die "Can't start $0: Can't read config file at $config_file: $_\n";
54 }
55 else {
56 0 die "Can't start $0: No readable config file found at $config_file.\n";
57 }
58 0 };
59  
60 0 return $config_ref;
61 }
62  
63 2 1;
64  
65 =head1 NAME
66  
67 Plerd::Util
68  
69 =head1 DESCRIPTION
70  
71 This class provides some utility functions common to Plerd's
72 command-line programs. It has no public API.
73  
74 =head1 SEE ALSO
75  
76 L<Plerd>
77  
78 =head1 AUTHOR
79  
80 Jason McIntosh <jmac@jmac.org>
« lib/Plerd/Post.pm ↑ Summary lib/Plerd/Tag.pm »