1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- 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;
- foreach my $line ( @config_lines ) {
- chomp $line;
- if ( $line =~ m/^(.*)\ =\ "(.*)"$/ ) {
- $config_hash{$1} = $2;
- }
- if ( $line =~ m/^(.*)\ =\ \[(.*)\]/ ) {
- my @trimmed_vals;
- my @vals = split(",",$2);
- foreach my $val (@vals) {
- $val =~ /"(.*)"/;
- push(@trimmed_vals,trim($1));
- }
- $config_hash{$1} = \@trimmed_vals;
- }
- }
- return %config_hash;
- }
- 1;
|