File Coverage

blib/lib/Shell/Var/Reader.pm
Criterion Covered Total %
statement 11 56 19.6
branch 0 18 0.0
condition 0 9 0.0
subroutine 4 5 80.0
pod 1 1 100.0
total 16 89 17.9


line stmt bran cond sub pod time code
1             package Shell::Var::Reader;
2              
3 1     1   133269 use 5.006;
  1         5  
4 1     1   7 use strict;
  1         3  
  1         53  
5 1     1   6 use warnings;
  1         3  
  1         61  
6 1     1   804 use File::Slurp qw(read_file);
  1         49104  
  1         1016  
7              
8             =head1 NAME
9              
10             Shell::Var::Reader - Runs a sh or bash script and returns the variables that have been set as well as their values.
11              
12             =head1 VERSION
13              
14             Version 0.10.0
15              
16             =cut
17              
18             our $VERSION = '0.10.0';
19              
20             =head1 SYNOPSIS
21              
22             Lets say '/usr/local/etc/someconfig.conf' which is basically a shell
23             config and read via include in a sh or bash script, this can be used for
24             getting a hash ref conttaining them.
25              
26             Similarly on systems like FreeBSD, this is also useful for reading '/etc/rc.conf'.
27              
28             use Shell::Var::Reader;
29             use Data::Dumper;
30              
31             my $found_vars=Shell::Var::Reader->read_in('/usr/local/etc/someconfig.conf');
32              
33             print Dumper($found_vars);
34              
35             =head1 SUBROUTINES
36              
37             =head2 read_in
38              
39             This runs a specified file as a include and then figures out what the
40             new variables are. fetching them.
41              
42             =cut
43              
44             sub read_in {
45 0     0 1   my $file = $_[1];
46              
47 0 0         if ( !defined($file) ) {
48 0           die('No file specified');
49             }
50              
51 0 0         if ( !-f $file ) {
52 0           die( '"' . $file . '" does not exist or is not a file' );
53             }
54              
55             # figure out if we are using bash or not
56 0 0         my $raw_file = read_file($file) or die 'Failed to read "' . $file . '"';
57 0           my @raw_split = split( /\n/, $raw_file );
58 0           my $shell = 'sh';
59 0 0 0       if ( defined( $raw_split[0] ) && ( $raw_split[0] =~ /^\#\!.*bash/ ) ) {
60 0           $shell = 'bash';
61             }
62              
63             #
64             # figure out what variables already exist...
65             #
66 0           my $cmd = $shell . " -c 'if [ -z \"\$BASH_VERSION\" ]; then set; else set -o posix; set; fi'";
67 0           my $results = `$cmd`;
68 0           my $base_vars = { 'ShellVarReaderFile' => 1 };
69 0           my @results_split = split( /\n/, $results );
70 0           foreach my $line (@results_split) {
71 0 0         if ( $line =~ /^[\_a-zA-Z]+[\_a-zA-Z0-9]*\=/ ) {
72 0           my @line_split = split( /=/, $line, 2 );
73 0           $base_vars->{ $line_split[0] } = 1;
74             }
75             }
76              
77             #
78             # Figure out what has been set
79             #
80 0           $ENV{ShellVarReaderFile} = $file;
81 0           $cmd
82             = $shell
83             . " -c ' . \"\$ShellVarReaderFile\" > /dev/null 2> /dev/null ; if [ -z \"\$BASH_VERSION\" ]; then set; else set -o posix; set; fi'";
84 0           $results = `$cmd`;
85 0           my $found_vars = {};
86 0           @results_split = split( /\n/, $results );
87 0           my $multiline = 0;
88 0           my $var_key;
89              
90 0           foreach my $line (@results_split) {
91 0 0 0       if ( $line =~ /^[\_a-zA-Z]+[\_a-zA-Z0-9]*\=/ && !$multiline ) {
92 0           my @line_split = split( /=/, $line, 2 );
93 0           $var_key = $line_split[0];
94 0           $found_vars->{$var_key} = $line_split[1];
95              
96             # if the value starts with a ' and does not end with a '
97             # it is going to be multiline
98 0 0 0       if ( $found_vars->{$var_key} =~ /^\'/
99             && $found_vars->{$var_key} !~ /\'$/ )
100             {
101 0           $found_vars->{$var_key} =~ s/^\'//;
102 0           $multiline = 1;
103             } else {
104 0           $found_vars->{$var_key} =~ s/^\'//;
105 0           $found_vars->{$var_key} =~ s/\'$//;
106             }
107             } else {
108 0           $found_vars->{$var_key} = $line;
109             # if it ends with ', then we have reached the end of the variable
110 0 0         if ( $found_vars->{$var_key} =~ /\'$/ ) {
111 0           $found_vars->{$var_key} =~ s/\'$//;
112 0           $multiline = 0;
113             }
114             }
115             } ## end foreach my $line (@results_split)
116              
117             #
118             # remove base vars
119             #
120 0           my @found_keys = keys( %{$found_vars} );
  0            
121 0           foreach my $var_key (@found_keys) {
122 0 0         if ( defined( $base_vars->{$var_key} ) ) {
123 0           delete $found_vars->{$var_key};
124             }
125             }
126              
127 0           return $found_vars;
128             } ## end sub read_in
129              
130             =head1 AUTHOR
131              
132             Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
133              
134             =head1 BUGS
135              
136             Please report any bugs or feature requests to C<bug-shell-var-reader at rt.cpan.org>, or through
137             the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Shell-Var-Reader>. I will be notified, and then you'll
138             automatically be notified of progress on your bug as I make changes.
139              
140             =head1 SUPPORT
141              
142             You can find documentation for this module with the perldoc command.
143              
144             perldoc Shell::Var::Reader
145              
146              
147             You can also look for information at:
148              
149             =over 4
150              
151             =item * RT: CPAN's request tracker (report bugs here)
152              
153             L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Shell-Var-Reader>
154              
155             =item * Search CPAN
156              
157             L<https://metacpan.org/release/Shell-Var-Reader>
158              
159             =back
160              
161              
162             =head1 ACKNOWLEDGEMENTS
163              
164              
165             =head1 LICENSE AND COPYRIGHT
166              
167             This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
168              
169             This is free software, licensed under:
170              
171             The Artistic License 2.0 (GPL Compatible)
172              
173              
174             =cut
175              
176             1; # End of Shell::Var::Reader