use test data for Perls probe
[scpubgit/System-Introspector.git] / lib / System / Introspector / Util.pm
CommitLineData
f79a227c 1use strictures 1;
2
3package System::Introspector::Util;
4use Exporter 'import';
db5ba31f 5use IPC::Run qw( run );
9c3e454c 6use IPC::Open2;
7use File::Spec;
8use Scalar::Util qw( blessed );
23b83532 9use Capture::Tiny qw( capture_stderr );
f79a227c 10
11our @EXPORT_OK = qw(
12 handle_from_command
db5ba31f 13 handle_from_file
db5ba31f 14 output_from_command
15 output_from_file
91c60940 16 lines_from_command
17 files_from_dir
f79a227c 18 transform_exceptions
19);
20
c23ab973 21do {
65e569ee 22 package System::Introspector::_Exception;
c23ab973 23 use Moo;
24 has message => (is => 'ro');
25};
26
65e569ee 27sub fail { die System::Introspector::_Exception->new(message => shift) }
28sub is_report_exception { ref(shift) eq 'System::Introspector::_Exception' }
c23ab973 29
db5ba31f 30sub files_from_dir {
31 my ($dir) = @_;
32 my $dh;
33 opendir $dh, $dir
c23ab973 34 or fail "Unable to read directory $dir: $!";
db5ba31f 35 my @files;
36 while (defined( my $item = readdir $dh )) {
37 next if -d "$dir/$item";
38 push @files, $item;
39 }
40 return @files;
41}
42
f79a227c 43sub transform_exceptions (&) {
44 my ($code) = @_;
f79a227c 45 my $result = eval { $code->() };
c23ab973 46 if (my $error = $@) {
8b69d66e 47 return { __error__ => $error->message }
c23ab973 48 if is_report_exception $error;
49 die $@;
50 }
f79a227c 51 return $result;
52}
53
db5ba31f 54sub output_from_command {
55 my ($command, $in) = @_;
56 $in = ''
57 unless defined $in;
58 my ($out, $err) = ('', '');
ff4d9e13 59 my $ok = eval { run($command, \$in, \$out, \$err) or die $err};
60 $err = $@ unless $ok;
db5ba31f 61 return $out, $err, $ok
62 if wantarray;
63 $command = join ' ', @$command
64 if ref $command;
c23ab973 65 fail "Error running command ($command): $err"
db5ba31f 66 unless $ok;
67 return $out;
68}
69
91c60940 70sub lines_from_command {
71 my ($command) = @_;
72 my $output = output_from_command $command;
73 chomp $output;
74 return split m{\n}, $output;
75}
76
f79a227c 77sub handle_from_command {
78 my ($command) = @_;
9c3e454c 79 my $pipe;
80 local $@;
81 my $ok = eval {
82 my $out;
23b83532 83 my $child_pid;
84 my @lines;
85 my ($err) = capture_stderr {
86 $child_pid = open2($out, File::Spec->devnull, $command);
87 @lines = <$out>;
88 close $out;
89 waitpid $child_pid, 0;
90 };
9c3e454c 91 my $content = join '', @lines;
92 my $status = $? >> 8;
23b83532 93 $err = "Unknown error"
94 unless defined $err;
95 fail "Command error ($command): $err\n"
96 if $status;
9c3e454c 97 open $pipe, '<', \$content;
98 1;
99 };
100 unless ($ok) {
101 my $err = $@;
102 die $err
103 if blessed($err) and $err->isa('System::Introspector::_Exception');
104 fail "Error from command '$command': $err";
105 }
f79a227c 106 return $pipe;
107}
108
db5ba31f 109sub handle_from_file {
110 my ($file) = @_;
111 open my $fh, '<', $file
c23ab973 112 or fail "Unable to read $file: $!";
db5ba31f 113 return $fh;
114}
115
116sub output_from_file {
117 my ($file) = @_;
118 my $fh = handle_from_file $file;
119 return <$fh>
120 if wantarray;
121 return do { local $/; <$fh> };
122}
123
f79a227c 1241;