package Gsg::ConfigParse; use strict; use warnings; use Log::Log4perl qw(:easy); use lib "/usr/local/lib"; use Shellex::Shellex qw(shellex findBin); use Exporter qw(import); our @EXPORT_OK = qw(parse_gsg_config); # https://perlmaven.com/trim sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s }; sub parse_gsg_config($$) { my $config_path = shift; my $logger = shift; if ( ! -e $config_path ) { $logger->error("$config_path doesn't look like a regular file, exiting..."); exit 1; } # Should probably just open with perl, needless shellout TODO my $cat_cmd = findBin("cat",$logger); my @config_lines = split("\n",shellex("$cat_cmd $config_path",$logger)); my %config_hash; my $line_counter = 1; foreach my $line ( @config_lines ) { chomp $line; if ( $line =~ m/^(.*)\ =\ "(.*)"$/ ) { $config_hash{$1} = $2; } elsif ( $line =~ m/^(.*)\ =\ \[(.*)\]/ ) { my @trimmed_vals; my @vals = split(",",$2); foreach my $val (@vals) { $val =~ /"(.*)"/; push(@trimmed_vals,trim($1)); } $config_hash{$1} = \@trimmed_vals; } elsif ( $line =~ m/^\#/ ) { next; } else { $logger->error("Couldn't parse config line $line_counter : $line : exiting"); exit 1; } $line_counter++; } return %config_hash; } 1;