File Coverage

lib/Slaughter/Transport/local.pm
Criterion Covered Total %
statement 14 31 45.1
branch 0 2 0.0
condition 1 3 33.3
subroutine 4 8 50.0
pod 5 5 100.0
total 24 49 48.9


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3            
4             Slaughter::Transport::local - Local filesystem transport class.
5            
6             =head1 SYNOPSIS
7            
8             This transport copes with fetching files and policies from the local filesystem.
9             It is designed to allow you to test policies on a single host.
10            
11             =cut
12              
13             =head1 DESCRIPTION
14            
15             This transport is slightly different from the others supplied with slaughter
16             as it involves fetching files from the I<local> filesystem - so there is no
17             remote server involved at all.
18            
19             =cut
20              
21              
22 1     1   938 use strict;
  1         1  
  1         24  
23 1     1   3 use warnings;
  1         2  
  1         217  
24              
25              
26              
27             package Slaughter::Transport::local;
28              
29              
30             #
31             # The version of our release.
32             #
33             our $VERSION = "3.0.5";
34              
35              
36              
37             =head2 new
38            
39             Create a new instance of this object.
40            
41             =cut
42              
43             sub new
44             {
45 1     1 1 428     my ( $proto, %supplied ) = (@_);
46 1   33     5     my $class = ref($proto) || $proto;
47              
48 1         1     my $self = {};
49              
50             #
51             # Allow user supplied values to override our defaults
52             #
53 1         3     foreach my $key ( keys %supplied )
54                 {
55 0         0         $self->{ lc $key } = $supplied{ $key };
56                 }
57              
58             #
59             # Explicitly ensure we have no error.
60             #
61 1         2     $self->{ 'error' } = undef;
62              
63 1         2     bless( $self, $class );
64 1         2     return $self;
65              
66             }
67              
68              
69              
70             =head2 name
71            
72             Return the name of this transport.
73            
74             =cut
75              
76             sub name
77             {
78 1     1 1 1103     return ("local");
79             }
80              
81              
82              
83             =head2 isAvailable
84            
85             Return whether this transport is available.
86            
87             This module is pure-perl, so we unconditionally return 1.
88            
89             =cut
90              
91             sub isAvailable
92             {
93 0     0 1       my ($self) = (@_);
94              
95 0               return 1;
96             }
97              
98              
99              
100             =head2 error
101            
102             Return the last error from the transport.
103            
104             This is only set in L</isAvailable>.
105            
106             =cut
107              
108             sub error
109             {
110 0     0 1       my ($self) = (@_);
111 0               return ( $self->{ 'error' } );
112             }
113              
114              
115              
116             =head2 fetchContents
117            
118             Fetch the contents of the specified file, relative to the specified prefix.
119            
120             =cut
121              
122             sub fetchContents
123             {
124 0     0 1       my ( $self, %args ) = (@_);
125              
126             #
127             # The prefix to fetch from: /files/, /modules/, or /policies/.
128             #
129 0               my $prefix = $args{ 'prefix' };
130              
131             #
132             # The file to retrieve.
133             #
134 0               my $file = $args{ 'file' };
135              
136             #
137             # The complete path.
138             #
139 0               my $complete = $self->{ 'prefix' } . $prefix . $file;
140              
141             #
142             # Read the file.
143             #
144 0               return ( $self->_readFile($complete) );
145             }
146              
147              
148             =begin doc
149            
150             This is an internal/private method that merely returns the contents of the
151             named file - or undef on error.
152            
153             =end doc
154            
155             =cut
156              
157             sub _readFile
158             {
159 0     0         my ( $self, $file ) = (@_);
160              
161 0               my $txt = undef;
162              
163 0 0             open( my $handle, "<", $file ) or return ($txt);
164              
165 0               while ( my $line = <$handle> )
166                 {
167 0                   $txt .= $line;
168                 }
169 0               close($handle);
170              
171 0               return $txt;
172             }
173              
174              
175              
176             1;
177              
178              
179             =head1 AUTHOR
180            
181             Steve Kemp <steve@steve.org.uk>
182            
183             =cut
184              
185             =head1 LICENSE
186            
187             Copyright (c) 2010-2015 by Steve Kemp. All rights reserved.
188            
189             This module is free software;
190             you can redistribute it and/or modify it under
191             the same terms as Perl itself.
192             The LICENSE file contains the full text of the license.
193            
194             =cut
195