ConfigParse.pm 979 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package Gsg::ConfigParse;
  2. use strict;
  3. use warnings;
  4. use Log::Log4perl qw(:easy);
  5. use lib "/usr/local/lib";
  6. use Shellex::Shellex qw(shellex findBin);
  7. use Exporter qw(import);
  8. our @EXPORT_OK = qw();
  9. # https://perlmaven.com/trim
  10. sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
  11. sub parseGsgConfig($$) {
  12. my $configPath = shift;
  13. my $logger = shift;
  14. if ( ! -e $config ) {
  15. $logger->error("$config doesn't look like a regular file, exiting...");
  16. exit 1;
  17. }
  18. my $catCmd = findBin("cat",$logger);
  19. my @configLines = split("\n",shellex("$catCmd $config",$logger));
  20. my %configHash;
  21. foreach my $line ( @configLines ) {
  22. chomp $line;
  23. if ( $line =~ m/^(.*)\ =\ "(.*)"$/ ) {
  24. $configHash{$1} = $2;
  25. }
  26. if ( $line =~ m/^(.*)\ =\ \[(.*)\]/ ) {
  27. my @trimmedPorts;
  28. my @ports = split(",",$2);
  29. foreach my $port (@ports) {
  30. $port =~ /(\d{1,5})/;
  31. push(@trimmedPorts,trim($1));
  32. }
  33. $configHash{$1} = \@trimmedPorts;
  34. }
  35. }
  36. return %configHash;
  37. }
  38. 1;