package System::Introspector::Config;
use Moo;
use Config::General;
+use File::Basename;
has config => (is => 'lazy');
sub sudo_user { $_[0]->config->{sudo_user} }
-sub groups { keys %{ $_[0]->config->{group} || {} } }
+sub groups { sort keys %{ $_[0]->config->{group} || {} } }
sub has_group { exists $_[0]->config->{group}{ $_[1] } }
+my $_load_host_file = sub {
+ my ($self, $path) = @_;
+ my $full_path = join '/', dirname($self->config_file), $path;
+ open my $fh, '<:utf8', $full_path
+ or die "Unable to read host_file '$full_path': $!\n";
+ my @hosts = <$fh>;
+ chomp @hosts;
+ return grep { m{\S} and not m{^\s*#} } @hosts;
+};
+
sub hosts {
my ($self) = @_;
my $host_spec = $self->config->{host};
- return ref($host_spec) ? @$host_spec : $host_spec;
+ my $host_file = $self->config->{host_file};
+ return(
+ ref($host_spec)
+ ? @$host_spec
+ : defined($host_spec) ? $host_spec : (),
+ defined($host_file)
+ ? $self->$_load_host_file($host_file)
+ : (),
+ );
}
sub user { $_[0]->config->{user} }
--- /dev/null
+use strictures 1;
+use Test::More;
+use FindBin;
+
+use System::Introspector::Config;
+
+my $config = System::Introspector::Config->new(
+ config_file => "$FindBin::Bin/data/test.conf",
+);
+
+is $config->sudo_user, 'root', 'sudo user';
+is_deeply [$config->groups], [qw( stable unstable )], 'groups';
+ok $config->has_group('stable'), 'has group';
+ok !$config->has_group('none'), 'does not have group';
+is_deeply [$config->hosts], [qw( foo bar baz qux quux quuux )], 'hosts';
+is $config->user, 'introspect', 'user';
+
+is_deeply $config->config_for_group('stable'), {
+ introspect => {
+ Foo => {},
+ Bar => { sudo => 1 },
+ },
+}, 'multiple elements with one sudo';
+
+is_deeply $config->config_for_group('unstable'), {
+ introspect => {
+ Qux => { sudo => 1 },
+ },
+}, 'single element with group-wide sudo';
+
+done_testing;
--- /dev/null
+baz
+qux
+quux
+
+# comment
+quuux
--- /dev/null
+host foo
+host bar
+
+host_file from-hosts.txt
+
+user introspect
+sudo_user root
+
+<group stable>
+ <introspect Foo/>
+ <introspect Bar>
+ sudo 1
+ </introspect>
+</group>
+
+<group unstable>
+ sudo 1
+ <introspect Qux/>
+</group>
+