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(); # https://perlmaven.com/trim sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s }; sub parseGsgConfig($$) { my $configPath = shift; my $logger = shift; if ( ! -e $config ) { $logger->error("$config doesn't look like a regular file, exiting..."); exit 1; } my $catCmd = findBin("cat",$logger); my @configLines = split("\n",shellex("$catCmd $config",$logger)); my %configHash; foreach my $line ( @configLines ) { chomp $line; if ( $line =~ m/^(.*)\ =\ "(.*)"$/ ) { $configHash{$1} = $2; } if ( $line =~ m/^(.*)\ =\ \[(.*)\]/ ) { my @trimmedPorts; my @ports = split(",",$2); foreach my $port (@ports) { $port =~ /(\d{1,5})/; push(@trimmedPorts,trim($1)); } $configHash{$1} = \@trimmedPorts; } } return %configHash; } 1;