File Coverage

lib/Config/Proxy/Impl/haproxy.pm
Criterion Covered Total %
statement 65 67 97.0
branch 7 8 87.5
condition 1 2 50.0
subroutine 15 16 93.7
pod 0 4 0.0
total 88 97 90.7


line stmt bran cond sub pod time code
1             package Config::Proxy::Impl::haproxy;
2 4     4   145 use 5.010;
  4         20  
3 4     4   27 use strict;
  4         8  
  4         122  
4 4     4   28 use warnings;
  4         16  
  4         363  
5 4     4   26 use parent 'Config::Proxy::Base';
  4         7  
  4         68  
6 4     4   376 use Text::Locus;
  4         31  
  4         226  
7 4     4   53 use Config::Proxy::Node::Root;
  4         10  
  4         158  
8 4     4   25 use Config::Proxy::Node::Section;
  4         7  
  4         108  
9 4     4   54 use Config::Proxy::Node::Statement;
  4         10  
  4         119  
10 4     4   19 use Config::Proxy::Node::Comment;
  4         8  
  4         102  
11 4     4   20 use Config::Proxy::Node::Empty;
  4         5  
  4         136  
12 4     4   19 use Text::ParseWords;
  4         6  
  4         329  
13 4     4   25 use Carp;
  4         8  
  4         3312  
14              
15             our $VERSION = '1.0';
16              
17             my %sections = (
18             global => 1,
19             defaults => 1,
20             frontend => 1,
21             backend => 1,
22             resolvers => 1
23             );
24              
25             sub new {
26 5     5 0 11 my $class = shift;
27 5   50     75 return $class->SUPER::new(shift // '/etc/haproxy/haproxy.cfg', 'haproxy -c -f');
28             }
29              
30             sub parse {
31 5     5 0 11 my $self = shift;
32              
33 5 50       39 open(my $fh, '<', $self->filename)
34             or croak "can't open ".$self->filename.": $!";
35 5         17 my $line = 0;
36 5         25 $self->reset();
37 5         52 my $cur = $self->tree;
38 5         212 while (<$fh>) {
39 50         196 my $locus = new Text::Locus($self->filename, ++$line);
40 50         1729 chomp;
41 50         94 my $orig = $_;
42 50         251 s/^\s+//;
43 50         210 s/\s+$//;
44              
45 50 100       169 if ($_ eq "") {
46 2         16 $cur->append_node(
47             new Config::Proxy::Node::Empty(orig => $orig,
48             locus => $locus));
49 2         21 next;
50             }
51              
52 48 100       124 if (/^#.*/) {
53 3         36 $cur->append_node(
54             new Config::Proxy::Node::Comment(orig => $orig,
55             locus => $locus));
56 3         32 next;
57             }
58              
59 45         146 my @words = parse_line('\s+', 1, $_);
60 45         4488 my $kw = shift @words;
61 45 100       136 if ($sections{$kw}) {
62 13         79 my $section =
63             new Config::Proxy::Node::Section(kw => $kw,
64             argv => \@words,
65             orig => $orig,
66             locus => $locus);
67 13         50 $self->tree->append_node($section);
68 13         67 $cur = $section;
69             } else {
70 32         159 $cur->append_node(
71             new Config::Proxy::Node::Statement(kw => $kw,
72             argv => \@words,
73             orig => $orig,
74             locus => $locus));
75             }
76             }
77 5         71 close $fh;
78 5         152 return $self;
79             }
80              
81             sub declare_section {
82 1     1 0 5 my ($class, $name) = @_;
83 1         6 $sections{$name} = 1;
84             }
85              
86             sub undeclare_section {
87 0     0 0   my ($class, $name) = @_;
88 0           $sections{$name} = 0;
89             }
90              
91             1;
92             __END__