ConfigParse.pm 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. foreach my $line ( @config_lines ) {
  23. chomp $line;
  24. if ( $line =~ m/^(.*)\ =\ "(.*)"$/ ) {
  25. $config_hash{$1} = $2;
  26. }
  27. if ( $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. }
  36. }
  37. return %config_hash;
  38. }
  39. 1;