Commit | Line | Data |
60e1cc39 |
1 | package System::Introspector::Config; |
2 | use Moo; |
3 | use Config::General; |
4 | |
5 | has config => (is => 'lazy'); |
6 | |
7 | has config_file => (is => 'ro', required => 1); |
8 | |
9 | sub _build_config { |
10 | my ($self) = @_; |
11 | my $reader = Config::General->new($self->config_file); |
12 | my %config = $reader->getall; |
13 | return \%config; |
14 | } |
15 | |
a5e1e1c6 |
16 | sub sudo_user { $_[0]->config->{sudo_user} } |
17 | |
60e1cc39 |
18 | sub groups { keys %{ $_[0]->config->{group} || {} } } |
19 | |
20 | sub has_group { exists $_[0]->config->{group}{ $_[1] } } |
21 | |
b079a95d |
22 | sub hosts { |
23 | my ($self) = @_; |
24 | my $host_spec = $self->config->{host}; |
25 | return ref($host_spec) ? @$host_spec : $host_spec; |
26 | } |
27 | |
28 | sub user { $_[0]->config->{user} } |
29 | |
a5e1e1c6 |
30 | my $_get_inherited = sub { |
31 | my $data = shift; |
32 | $data ||= {}; |
33 | return |
34 | map { ($_ => $data->{$_}) } |
35 | grep { exists $data->{$_} } |
36 | qw( sudo ); |
37 | }; |
38 | |
39 | sub config_for_group { |
40 | my ($self, $name) = @_; |
41 | my %common; |
42 | my $config = $self->config; |
43 | %common = (%common, $config->$_get_inherited); |
44 | my $group = $config->{group}{ $name }; |
45 | %common = (%common, $group->$_get_inherited); |
46 | return { |
47 | introspect => { |
48 | map { |
49 | ($_ => { |
50 | %common, |
51 | %{ $group->{introspect}{ $_ } || {} }, |
52 | }); |
53 | } keys %{ $group->{introspect} || {} }, |
54 | }, |
55 | }; |
56 | } |
60e1cc39 |
57 | |
58 | 1; |