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