Subversion Repositories camp_sysinfo_client_3

Rev

Rev 219 | Rev 227 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 219 Rev 223
Line 184... Line 184...
184
   return $config unless -e $filename;
184
   return $config unless -e $filename;
185
   my $yaml = YAML::Tiny->read( $filename );
185
   my $yaml = YAML::Tiny->read( $filename );
186
   return $yaml->[0];
186
   return $yaml->[0];
187
}
187
}
188
 
188
 
-
 
189
# merges $hash2 into $hash, with $hash1 taking precedence
-
 
190
# if a key is a hashref on both sides, will recursively merg
-
 
191
# otherwise, if $hash1->{$key} already has a value, no action
-
 
192
# will be taken
-
 
193
# using this instead of Hash::Merge as we don't need the extra power
-
 
194
sub mergeHash {
-
 
195
   my ($hash1, $hash2) = shift;
-
 
196
   foreach my $key ( keys %$hash2 ) {
-
 
197
      if ( ! ( defined( $hash1->{$key} ) && length($hash1->{$key} ) ) ) { # defined and not empty
-
 
198
         # just put it into hash 1
-
 
199
         $hash1->{$key} = $hash2->{$key};
-
 
200
      } elsif ( ref( $hash2->{$key} ) eq 'HASH' && ref( $hash1->{$key} ) eq 'HASH' ) {
-
 
201
         # they are both hashes, so recursively merge
-
 
202
         $hash1 = &mergeHash( $hash1->{$key}, $hash2->{$key} );
-
 
203
      }
-
 
204
      # ignore everything else
-
 
205
   }
-
 
206
   return $hash1;
-
 
207
}
-
 
208
      
-
 
209
 
189
sub makeConfig {
210
sub makeConfig {
190
   my @configFileNames = @_;
211
   my @configFileNames = @_;
191
   my $config;
212
   my $config = {}; # make sure it is a ref to a hash so mergeHash can recognize it
192
 
213
 
193
   use Hash::Merge;
-
 
194
   my $merge = Hash::Merge->new('RIGHT_PRECEDENT');
-
 
195
   foreach my $configFile ( @configFileNames ) {
214
   foreach my $configFile ( @configFileNames ) {
196
      next unless $configFile && -e $configFile;
215
      next unless $configFile && -e $configFile;
197
      print "Processing config file $configFile\n";
216
      print "Processing config file $configFile\n";
198
      my $thisConfig = &readConfig( $configFile );
217
      my $thisConfig = &readConfig( $configFile );
199
      # add the new config to %config, overwriting any existing keys which are duplicated
218
      # add the new config to %config, overwriting any existing keys which are duplicated
200
      $config = $merge->merge( $config, $thisConfig );
219
      $config = &mergeHash( $config, $thisConfig );
201
      #@config{keys %$thisConfig} = values %$thisConfig;
220
      #@config{keys %$thisConfig} = values %$thisConfig;
202
   }
221
   }
203
   # now, ensure the correct values are loaded in some areas
222
   # now, ensure the correct values are loaded in some areas
204
   unless ( $config->{'hostname'} ) {
223
   unless ( $config->{'hostname'} ) {
205
      $hostname = `hostname -f`;
224
      $hostname = `hostname -f`;