Acmeman/Source/File.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 92 CC 8 SCAR 22.8
CC
8
Cov
0%
scan
20.0
new
12.0
load
12.0
CRAP 9.8
1 package App::Acmeman::Source::File;
2  
3 use strict;
4 use warnings;
5 use Carp;
6 use File::Spec;
7 use parent 'App::Acmeman::Source';
8 use Getopt::Long qw(GetOptionsFromArray :config gnu_getopt no_ignore_case);
9 use App::Acmeman::Log qw(:all);
10  
11 sub new {
12 my $class = shift;
13 my $pattern = shift || croak "file name or globbing pattern must be given";
14 my $ignore = '^\.|~$|\.bak$|^#.*#$';
15 my $host;
16 GetOptionsFromArray(\@_,
17 'ignore|i=s' => \$ignore,
18 'host|h=s' => \$host);
19 unless ($pattern =~ m{[][*?]}) {
20 $pattern =~ s{/$}{};
21 if (-d $pattern) {
22 $pattern = File::Spec->catfile($pattern, '*');
23 }
24 }
25 bless { pattern => $pattern,
26 ignore => $ignore,
27 host => $host }, $class;
28 }
29  
30 sub scan {
31 my ($self) = @_;
32 debug(1, "initializing file list from $self->{pattern}");
33 my $err = 0;
34 if ($self->{host}) {
35 $self->define_domain($self->{host});
36 }
37 foreach my $file (glob $self->{pattern}) {
38 next if $file =~ m{$self->{ignore}};
39 unless ($self->load($file)) {
40 ++$err;
41 }
42 }
43 return $err == 0;
44 }
45  
46 sub load {
47 my ($self, $file) = @_;
48 debug(1, "reading $file");
49 open(my $fh, '<', $file)
50 or do {
51 error("can't open $file: $!");
52 return 0;
53 };
54 chomp(my @lines = <$fh>);
55 close $fh;
56 if (@lines) {
57 if ($self->{host}) {
58 $self->define_alias($self->{host}, @lines);
59 } else {
60 my $cn = shift @lines;
61 $self->define_domain($cn);
62 $self->define_alias($cn, @lines);
63 }
64 }
65 return 1;
66 }
67  
68 1;
69  
70