d88f590cad6c581ba35c94f5d4a57e64e6998ca4
[scpubgit/System-Introspector.git] / lib / System / Introspector / Gatherer.pm
1 package System::Introspector::Gatherer;
2 use Moo;
3 use Object::Remote;
4 use Object::Remote::Future;
5 use System::Introspector::Gatherer::Bridge;
6 use Module::Runtime qw( use_module );
7
8 has introspectors => (is => 'ro', required => 1);
9
10 sub gather_all {
11     my ($self) = @_;
12     my %report;
13     for my $spec (@{ $self->introspectors }) {
14         my ($base, $args) = @$spec;
15         $report{$base} = use_module("System::Introspector::Probe::$base")
16             ->new($args)
17             ->gather;
18     }
19     return \%report;
20 }
21
22 sub _new_direct {
23     my ($class, $remote, $args) = @_;
24     return $class->new::on($remote, $args || {});
25 }
26
27 sub _new_bridged {
28     my ($class, $bridge, $remote, $args) = @_;
29     return System::Introspector::Gatherer::Bridge->new::on($bridge,
30         remote_spec  => $remote,
31         remote_class => $class,
32         remote_args  => $args || {},
33     );
34 }
35
36 sub new_from_spec {
37     my ($class, %arg) = @_;
38     my ($user, $host, $sudo_user) = @arg{qw( user host sudo_user )};
39     my $sudo = defined($sudo_user) ? sprintf('%s@', $sudo_user) : undef;
40     my $args = { introspectors => $arg{introspectors} };
41     if (defined $host) {
42         my $remote = join '@', grep defined, $user, $host;
43         if (defined $sudo_user) {
44             return $class->_new_bridged($remote, $sudo, $args);
45         }
46         else {
47             return $class->_new_direct($remote, $args);
48         }
49     }
50     else {
51         if (defined $sudo_user) {
52             return $class->_new_direct($sudo, $args);
53         }
54         else {
55             return $class->new($args);
56         }
57     }
58 }
59
60 1;