ConfigParse.pm 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(parse_gsg_config);
  9. # https://perlmaven.com/trim
  10. sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
  11. sub parse_gsg_config($$) {
  12. my $config_path = shift;
  13. my $logger = shift;
  14. if ( ! -e $config_path ) {
  15. $logger->error("$config_path doesn't look like a regular file, exiting...");
  16. exit 1;
  17. }
  18. # Should probably just open with perl, needless shellout TODO
  19. my $cat_cmd = findBin("cat",$logger);
  20. my @config_lines = split("\n",shellex("$cat_cmd $config_path",$logger));
  21. my %config_hash;
  22. my $line_counter = 1;
  23. foreach my $line ( @config_lines ) {
  24. chomp $line;
  25. if ( $line =~ m/^(.*)\ =\ "(.*)"$/ ) {
  26. $config_hash{$1} = $2;
  27. } elsif ( $line =~ m/^(.*)\ =\ \[(.*)\]/ ) {
  28. my @trimmed_vals;
  29. my @vals = split(",",$2);
  30. foreach my $val (@vals) {
  31. $val =~ /"(.*)"/;
  32. push(@trimmed_vals,trim($1));
  33. }
  34. $config_hash{$1} = \@trimmed_vals;
  35. } elsif ( $line =~ m/^\#/ ) {
  36. next;
  37. } else {
  38. $logger->error("Couldn't parse config line $line_counter : $line : exiting");
  39. exit 1;
  40. }
  41. $line_counter++;
  42. }
  43. return %config_hash;
  44. }
  45. 1;