File Coverage

lib/Term/RouterCLI/Config.pm
Criterion Covered Total %
statement 42 68 61.7
branch 2 4 50.0
condition 1 3 33.3
subroutine 11 17 64.7
pod 0 8 0.0
total 56 100 56.0


line stmt bran cond sub pod time code
1             #####################################################################
2             # This program is not guaranteed to work at all, and by using this #
3             # program you release the author of any and all liability. #
4             # #
5             # You may use this code as long as you are in compliance with the #
6             # license (see the LICENSE file) and this notice, disclaimer and #
7             # comment box remain intact and unchanged. #
8             # #
9             # Package: Term::RouterCLI #
10             # Class: Config #
11             # Description: Methods for building a Router (Stanford) style CLI #
12             # #
13             # Written by: Bret Jordan (jordan at open1x littledot org) #
14             # Created: 2011-02-21 #
15             #####################################################################
16             #
17             #
18             #
19             #
20             package Term::RouterCLI::Config;
21              
22 4     4   44 use 5.8.8;
  4         14  
  4         173  
23 4     4   21 use strict;
  4         5  
  4         150  
24 4     4   20 use warnings;
  4         9  
  4         101  
25 4     4   2173 use Term::RouterCLI::Debugger;
  4         13  
  4         144  
26 4     4   42 use Config::General();
  4         6  
  4         73  
27 4     4   4590 use File::Copy;
  4         33946  
  4         330  
28 4     4   42 use Log::Log4perl;
  4         7  
  4         42  
29              
30             our $VERSION = '1.00';
31             $VERSION = eval $VERSION;
32              
33             our $hRunningConfig;
34             our $hStartupConfig;
35             my $oDebugger = new Term::RouterCLI::Debugger();
36              
37              
38             sub new
39             {
40 4     4 0 12 my $pkg = shift;
41 4   33     36 my $class = ref($pkg) || $pkg;
42            
43 4         11 my $self = {};
44 4         15 $self->{'_sName'} = $pkg; # Lets set the object name so we can use it in debugging
45 4         12 bless ($self, $class);
46              
47             # Lets send any passed in arguments to the _init method
48 4         24 $self->_init(@_);
49 4         9 return $self;
50             }
51              
52             sub _init
53             {
54 4     4   11 my $self = shift;
55 4         12 my %hParameters = @_;
56              
57 4         44 $self->{'_sFilename'} = undef;
58            
59             # Lets overwrite any defaults with values that are passed in
60 4 50       20 if (%hParameters)
61             {
62 0         0 foreach (keys (%hParameters)) { $self->{$_} = $hParameters{$_}; }
  0         0  
63             }
64             }
65              
66             sub DESTROY
67             {
68 0     0   0 my $self = shift;
69 0         0 $self = {};
70             }
71              
72              
73              
74             # ----------------------------------------
75             # Public Methods
76             # ----------------------------------------
77             sub SetFilename
78             {
79             # This method is for setting the filename for the configuration file
80             # Required:
81             # string(file name)
82 3     3 0 11 my $self = shift;
83 3         10 my $parameter = shift;
84 3 50       16 if (defined $parameter) { $self->{'_sFilename'} = $parameter; }
  3         27  
85             }
86              
87             sub LoadConfig
88             {
89             # This method will load the current configuration in to a hash that can be used
90 3     3 0 10 my $self = shift;
91              
92 3         52 my $oConfig = new Config::General
93             (
94             -ConfigFile => "$self->{'_sFilename'}",
95             -LowerCaseNames => 0,
96             -MergeDuplicateOptions => 1,
97             -AutoTrue => 0,
98             -ExtendedAccess => 1,
99             -SaveSorted => 1
100             );
101            
102             # Lets get all of the configuration in one pass to save disk IO then lets save the data in to the object
103 3         9409 my %hStartupConfiguration = $oConfig->getall();
104 3         111 my %hRunningConfiguration = %hStartupConfiguration;
105 3         11 $hStartupConfig = \%hStartupConfiguration;
106 3         17 $hRunningConfig = \%hRunningConfiguration;
107             }
108              
109             sub GetStartupConfig
110             {
111             # This method will return the global object for the Startup Configuration
112 0     0 0   my $self = shift;
113 0           return $hStartupConfig;
114             }
115              
116             sub GetRunningConfig
117             {
118             # This method will return the global object for the Running Configuration
119 0     0 0   my $self = shift;
120 0           return $hRunningConfig;
121             }
122              
123             sub ReloadConfig
124             {
125             # This method will reload the current configuration
126 0     0 0   my $self = shift;
127            
128 0           $hStartupConfig = undef;
129 0           $hRunningConfig = undef;
130 0           $self->LoadConfig();
131             }
132              
133             sub SaveConfig
134             {
135             # This method will save out the hash of the configuration back to the same file. It will make a backup first
136 0     0 0   my $self = shift;
137 0           my $logger = $oDebugger->GetLogger($self);
138 0           $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
139              
140             # Backup configuration first
141 0           $self->BackupConfig();
142            
143 0           my $oConfig = new Config::General
144             (
145             -ConfigFile => "$self->{'_sFilename'}",
146             -LowerCaseNames => 0,
147             -MergeDuplicateOptions => 1,
148             -AutoTrue => 0,
149             -ExtendedAccess => 1,
150             -SaveSorted => 1
151             );
152            
153             # Save current configuration
154 0           $oConfig->save_file("$self->{'_sFilename'}", $hRunningConfig);
155 0           $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
156             }
157              
158             sub BackupConfig
159             {
160             # This method will make a backup of the current configuration file
161 0     0 0   my $self = shift;
162 0           my $logger = $oDebugger->GetLogger($self);
163 0           $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
164 0           my $sOriginalFile = $self->{'_sFilename'};
165 0           my $sBackupFile = $self->{'_sFilename'} . ".bak";
166 0           copy ($sOriginalFile, $sBackupFile);
167 0           $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
168             }
169              
170             return 1;