Skip to content

Commit

Permalink
agent-linux: move config reading to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
grawity committed Mar 27, 2015
1 parent 823c95d commit ec4ae1a
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions agent-linux/rwho-agent
Expand Up @@ -47,12 +47,24 @@ sub parse_bool {
}

sub parse_conf {
my $fh = shift;
my ($fh) = @_;
map {split /\s*=\s*/, $_, 2}
grep {length and !/^[#;]/}
grep {chomp} <$fh>;
}

sub read_conf {
my ($file) = @_;
debug("config: reading '$file'");
if (open(my $fh, "<", $file)) {
my %conf = parse_conf($fh);
close($fh);
return %conf;
} else {
die "error: unable to read '$file' ($!)\n";
}
}

sub enum_sessions {
my @utmp = ();
if (eval {require User::Utmp}) {
Expand Down Expand Up @@ -299,15 +311,7 @@ my $pod = "$0.1.pod";
$0 = "rwho-agent";

GetOptions(
"config=s" => sub {
my ($opt, $arg) = @_;
if (open(my $fh, "<", $arg)) {
%conf = parse_conf($fh);
close($fh);
} else {
die "error: unable to read '$arg' ($!)\n";
}
},
"config=s" => \$arg{config},
"cleanup!" => \$do_cleanup,
"daemon" => \$do_fork,
"help" => sub { pod2usage(-input => $pod, -exitstatus => 0); },
Expand Down Expand Up @@ -353,6 +357,10 @@ if ($do_monitor) {

# merge arguments and config file

if ($arg{config}) {
%conf = read_conf($arg{config});
}

$skip_root = $arg{skip_root}
// parse_bool($conf{"agent.exclude_root"})
// 1;
Expand Down

0 comments on commit ec4ae1a

Please sign in to comment.