36a8288cde87497b5e597b1734cb970e78638d0f
[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;
61
62 __END__
63
64 =head1 NAME
65
66 System::Introspector::Gatherer - Remote Gather Handler
67
68 =head1 SEE ALSO
69
70 =over
71
72 =item L<System::Introspector>
73
74 =back
75
76 =head1 COPYRIGHT
77
78 Copyright (c) 2012 the L<System::Introspector>
79 L<AUTHOR|System::Introspector/AUTHOR>,
80 L<CONTRIBUTORS|System::Introspector/CONTRIBUTORS> and
81 L<SPONSORS|System::Introspector/SPONSORS>.
82
83 =head1 LICENSE
84
85 This library is free software and may be distributed under the same terms
86 as perl itself.
87
88 =cut