File Coverage

blib/lib/Passwd/Keyring/Auto/Config.pm
Criterion Covered Total %
statement 58 94 61.7
branch 10 44 22.7
condition 3 9 33.3
subroutine 13 14 92.8
pod 0 5 0.0
total 84 166 50.6


line stmt bran cond sub pod time code
1             package Passwd::Keyring::Auto::Config;
2 7     7   37 use Moo;
  7         10  
  7         37  
3 7     7   6660 use File::HomeDir;
  7         51271  
  7         562  
4 7     7   4040 use Config::Tiny;
  7         7196  
  7         244  
5 7     7   14456 use Path::Tiny;
  7         123689  
  7         694  
6 7     7   75 use Carp;
  7         11  
  7         531  
7 7     7   4497 use namespace::clean;
  7         96106  
  7         51  
8              
9             =head1 NAME
10              
11             Passwd::Keyring::Auto::Config - config file support
12              
13             =head1 DESCRIPTION
14              
15             Configuration file allows user to configure his or her keyring backend
16             selection criteria.
17              
18             Internal object, not intended to be used directly.
19              
20             =cut
21              
22             # Explicit location if specified
23             has 'location' => (is=>'ro');
24             has 'debug' => (is=>'ro');
25              
26             # Actual location (may be non-existant if that's default)
27             has 'config_location' => (is=>'lazy');
28              
29             # Config object
30             has '_config_obj' => (is=>'lazy');
31              
32              
33             sub force($$) {
34 7     7 0 15 my ($self, $app) = @_;
35 7         25 my $force = $self->_read_param("force", $app);
36 7         28 return $force;
37             }
38              
39             sub forbid($$) {
40 7     7 0 12 my ($self, $app) = @_;
41 7         20 my $forbid = $self->_read_param("forbid", $app);
42 7         58 return $forbid;
43             }
44              
45             sub prefer($$) {
46 7     7 0 18 my ($self, $app) = @_;
47 7         18 my $prefer = $self->_read_param("prefer", $app);
48 7         51 return $prefer;
49             }
50              
51             sub backend_args($$$) {
52 7     7 0 59 my ($self, $app_name, $backend_name) = @_;
53 7         133 my $cfg_obj = $self->_config_obj;
54 7         44 my %reply;
55 7         14 my $dflt = $cfg_obj->{_};
56 7         23 foreach my $key (keys %$dflt) {
57 0 0       0 if($key =~ /^$backend_name\.(.*)/x) {
58 0         0 $reply{$1} = $dflt->{$key};
59             }
60             }
61 7 50 33     57 if( $app_name && exists $cfg_obj->{$app_name}) {
62 0         0 my $app = $cfg_obj->{$app_name};
63 0         0 foreach my $key (keys %$app) {
64 0 0       0 if($key =~ /^$backend_name\.(.*)/x) {
65 0         0 $reply{$1} = $app->{$key};
66             }
67             }
68             }
69 7 50       42 return wantarray ? %reply : \%reply;
70             }
71              
72             # Return listref of all overriden names
73             sub apps_with_overrides {
74 0     0 0 0 my $self = shift;
75 0         0 my $cfg_obj = $self->_config_obj;
76 0         0 my @apps = grep { /^[^_]/ } keys %$cfg_obj;
  0         0  
77 0         0 return [sort @apps];
78             }
79              
80             sub _read_param {
81 21     21   28 my ($self, $param, $app) = @_;
82              
83 21         41 my $debug = $self->debug;
84 21         379 my $cfg_obj = $self->_config_obj;
85              
86 21 50 33     181 if( $app && exists $cfg_obj->{$app} ) {
87 0         0 my $per_app_section = $cfg_obj->{$app};
88 0 0       0 if($per_app_section) {
89 0         0 my $per_app = $per_app_section->{$param};
90 0 0       0 if($per_app) {
91 0 0       0 print STDERR "[Passwd::Keyring] Per-app config value found for $param (for $app): $per_app\n" if $debug;
92 0         0 return $per_app;
93             }
94             }
95             }
96 21         32 my $default = $cfg_obj->{_}->{$param};
97 21 50       39 if($default) {
98 0 0       0 print STDERR "[Passwd::Keyring] Default config value found for $param: $default\n" if $debug;
99 0         0 return $default;
100             }
101 21 50       34 print STDERR "[Passwd::Keyring] No config value found for $param\n" if $debug;
102 21         40 return; # undef
103             }
104              
105             sub _build__config_obj {
106 7     7   1607 my ($self) = @_;
107              
108 7         67 my $path = $self->config_location;
109 7         9 my $config;
110 7 50 33     54 if($path && $path->exists) {
111             # print STDERR "[Passwd::Keyring] Reading config from $path\n" if $self->debug;
112 0 0       0 $config = Config::Tiny->read("$path", "utf8")
113             or croak("Can not read Passwd::Keyring config file from $path: $Config::Tiny::errstr");
114             # use Data::Dumper; print STDERR Dumper($config);
115             } else {
116 7         125 $config = Config::Tiny->new;
117             }
118 7         46 return $config;
119             }
120              
121             sub _build_config_location {
122 7     7   1763 my ($self) = @_;
123              
124 7         24 my $debug = $self->debug;
125              
126 7         20 my $loc = $self->location;
127 7 50       23 if($loc) {
128 0         0 my $path = path($loc);
129 0 0       0 unless($path->is_file) {
130 0         0 croak("File specified by config=> parameter ($path) does not exist");
131             }
132 0 0       0 if($debug) {
133 0         0 print STDERR "[Passwd::Keyring] Using config file specified by config=> parameter: $path\n";
134             }
135 0         0 return $path;
136             }
137              
138 7         18 my $env = $ENV{PASSWD_KEYRING_CONFIG};
139 7 50       20 if($env) {
140 0         0 my $path = path($env);
141 0 0       0 unless($path->is_file) {
142 0         0 croak("File specified by PASSWD_KEYRING_CONFIG environment variable ($path) does not exist");
143             }
144 0 0       0 if($debug) {
145 0         0 print STDERR "[Passwd::Keyring] Using config file specified by PASSWD_KEYRING_CONFIG environment variable: $path\n";
146             }
147 0         0 return $path;
148             }
149              
150 7         54 my $path = path(File::HomeDir->my_data)->child(".passwd-keyring.cfg");
151 7 50       1098 if($path->is_file) {
152 0 0       0 if($debug) {
153 0         0 print STDERR "[Passwd::Keyring] Using default config file: $path\n";
154             }
155 0         0 return $path;
156             }
157              
158 7 50       348 if($debug) {
159 0         0 print STDERR "[Passwd::Keyring] Config file not specified by any means, and default config ($path) does not exist. Proceeding without config\n";
160             }
161              
162 7         30 return $path; # To preserve info where it is to be created, for example
163             }
164              
165             1;