aboutsummaryrefslogtreecommitdiff
path: root/lib/App/Acmeman/Source/File.pm
blob: 48a4010182b4e1250f1bb7d4cdc675e601dffb7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package App::Acmeman::Source::File;

use strict;
use warnings;
use Carp;
use File::Spec;
use parent 'App::Acmeman::Source';
use Getopt::Long qw(GetOptionsFromArray :config gnu_getopt no_ignore_case);

sub new {
    my $class = shift;
    my $pattern = shift || croak "file name or globbing pattern must be given";
    my $ignore = '^\.|~$|\.bak$|^#.*#$';
    my $host;
    GetOptionsFromArray(\@_,
			'ignore|i=s' => \$ignore,
			'host|h=s' => \$host);
    unless ($pattern =~ m{[][*?]}) {
	$pattern =~ s{/$}{};
	$pattern = File::Spec->catfile($pattern, '*');
    }
    bless { pattern => $pattern,
	    ignore => $ignore,
	    host => $host }, $class;
}

sub scan {
    my ($self) = @_;
    $self->debug(1, "initializing file list from $self->{pattern}");
    my $err = 0;
    if ($self->{host}) {
	$self->define_domain($self->{host});
    }
    foreach my $file (glob $self->{pattern}) {
	next if $file =~ m{$self->{ignore}};
	$err |= $self->load($file);
    }
    return $err == 0;
}

sub load {
    my ($self, $file) = @_;
    $self->debug(1, "reading $file");
    open(my $fh, '<', $file)
	or do {
	    $self->error("can't open $file: $!");
	    return 0;
        };
    chomp(my @lines = <$fh>);
    close $fh;
    if ($self->{host}) {
	$self->define_alias($self->{host}, @lines);
    } else {
	my $cn = shift @lines;
	$self->define_domain($cn);
	$self->define_alias($cn, @lines);
    }
    return 1;
}

1;

    

Return to:

Send suggestions and report system problems to the System administrator.