File Coverage

blib/lib/Dev/Util/OS.pm
Criterion Covered Total %
statement 43 48 89.5
branch 11 18 61.1
condition 10 12 83.3
subroutine 12 12 100.0
pod 9 9 100.0
total 85 99 85.8


line stmt bran cond sub pod time code
1             package Dev::Util::OS;
2              
3 3     3   3008 use Dev::Util::Syntax;
  3         11  
  3         28  
4 3     3   44 use Exporter qw(import);
  3         52  
  3         169  
5              
6 3     3   2149 use IPC::Cmd qw[can_run run];
  3         190277  
  3         4484  
7              
8             our $VERSION = version->declare("v2.19.35");
9              
10             our @EXPORT_OK = qw(
11             get_os
12             get_hostname
13             is_linux
14             is_mac
15             is_freebsd
16             is_openbsd
17             is_sunos
18             ipc_run_c
19             ipc_run_e
20             );
21              
22             our %EXPORT_TAGS = ( all => \@EXPORT_OK );
23              
24             sub get_os {
25 10     10 1 96115 my $OS = qx(uname -s);
26 10         130 chomp $OS;
27              
28 10         316 return $OS;
29             }
30              
31             sub get_hostname {
32 1     1 1 218376 my $host = qx(uname -n);
33 1         20 chomp $host;
34              
35 1         29 return $host;
36             }
37              
38             sub is_linux {
39 2 50   2 1 1286 if ( get_os() eq "Linux" ) {
40 2         54 return 1;
41             }
42             else {
43 0         0 return 0;
44             }
45             }
46              
47             sub is_mac {
48 3 50   3 1 1082 if ( get_os() eq "Darwin" ) {
49 0         0 return 1;
50             }
51             else {
52 3         77 return 0;
53             }
54             }
55              
56             sub is_freebsd {
57 1 50   1 1 1112 if ( get_os() eq "FreeBSD" ) {
58 0         0 return 1;
59             }
60             else {
61 1         40 return 0;
62             }
63             }
64              
65             sub is_openbsd {
66 1 50   1 1 869 if ( get_os() eq "OpenBSD" ) {
67 0         0 return 1;
68             }
69             else {
70 1         46 return 0;
71             }
72             }
73              
74             sub is_sunos {
75 2 50   2 1 1409 if ( get_os() eq "SunOS" ) {
76 0         0 return 1;
77             }
78             else {
79 2         78 return 0;
80             }
81             }
82              
83             my %osx_distro_ver = (
84             10.0 => 'Cheetah',
85             10.1 => 'Puma',
86             10.2 => 'Jaguar',
87             10.3 => 'Panther',
88             10.4 => 'Tiger',
89             10.5 => 'Leopard',
90             10.6 => 'Snow Leopard',
91             10.7 => 'Lion',
92             10.8 => 'Mountain Lion',
93             10.9 => 'Mavericks',
94             10.10 => 'Yosemite',
95             10.11 => 'El Capitan',
96             10.12 => 'Sierra',
97             10.13 => 'High Sierra',
98             10.14 => 'Mojave',
99             10.15 => 'Catalina',
100             );
101             my %macos_distro_ver = (
102             11 => 'Big Sur',
103             12 => 'Monterey',
104             13 => 'Ventura',
105             14 => 'Sonoma',
106             15 => 'Sequioa',
107             16 => 'Tahoe',
108             );
109              
110             my %ubuntu_distro_ver = (
111             '4.10' => 'Warty Warthog',
112             '5.04' => 'Hoary Hedgehog',
113             '5.10' => 'Breezy Badger',
114             '6.06' => 'Dapper Drake',
115             '6.10' => 'Edgy Eft',
116             '7.04' => 'Feisty Fawn',
117             '7.10' => 'Gutsy Gibbon',
118             '8.04' => 'Hardy Heron',
119             '8.10' => 'Intrepid Ibex',
120             '9.04' => 'Jaunty Jackalope',
121             '9.10' => 'Karmic Koala',
122             '10.04' => 'Lucid Lynx',
123             '10.10' => 'Maverick Meerkat',
124             '11.04' => 'Natty Narwhal',
125             '11.10' => 'Oneiric Ocelot',
126             '12.04' => 'Precise Pangolin',
127             '12.10' => 'Quantal Quetzal',
128             '13.04' => 'Raring Ringtail',
129             '13.10' => 'Saucy Salamander',
130             '14.04' => 'Trusty Tahr',
131             '14.10' => 'Utopic Unicorn',
132             '15.04' => 'Vivid Vervet',
133             '15.10' => 'Wily Werewolf',
134             '16.04' => 'Xenial Xerus',
135             '16.10' => 'Yakkety Yak',
136             '17.04' => 'Zesty Zapus',
137             '17.10' => 'Artful Aardvark',
138             '18.04' => 'Bionic Beaver',
139             '18.10' => 'Cosmic Cuttlefish',
140             '19.04' => 'Disco Dingo',
141             '19.10' => 'Eoan Ermine',
142             '20.04' => 'Focal Fossa',
143             '20.10' => 'Groovy Gorilla',
144             '21.04' => 'Hirsute Hippo',
145             '21.10' => 'Impish Indri',
146             '22.04' => 'Jammy Jellyfish',
147             '22.10' => 'Kinetic Kudu',
148             '23.04' => 'Lunar Lobster',
149             '23.10' => 'Mantic Minotaur',
150             '24.04' => 'Noble Numbat',
151             '24.10' => 'Oracular Oriole',
152             '25.04' => 'Plucky Puffin',
153             '25.10' => 'Questing Quokka',
154             '26.04' => 'Resolute Raccoon',
155             );
156              
157             my %opensuse_distro_ver = (
158             11.2 => 'Emerald',
159             11.3 => 'Teal',
160             11.4 => 'Celadon',
161             12.1 => 'Asparagus',
162             12.2 => 'Mantis',
163             12.3 => 'Dartmoth',
164             13.1 => 'Bottle',
165             13.2 => 'Harlequin',
166             42.1 => 'Malacite',
167             );
168              
169             my %debian_distro_ver = (
170             '1.1' => 'buzz',
171             '1.2' => 'rex',
172             '1.3' => 'bo',
173             '2.0' => 'hamm',
174             '2.1' => 'slink',
175             '2.2' => 'potato',
176             '3.0' => 'woody',
177             '3.1' => 'sarge',
178             '4.0' => 'etch',
179             '5.0' => 'lenny',
180             '6.0' => 'squeeze',
181             '7' => 'wheezy',
182             '8' => 'jessie',
183             '9' => 'stretch',
184             '10' => 'buster',
185             '11' => 'bullseye',
186             '12' => 'bookworm',
187             '13' => 'trixie',
188             '14' => 'forky',
189             '15' => 'duke',
190             );
191              
192             my %redhat_distro_ver = (
193             '2.1' => 'Pensacola',
194             '3' => 'Taroon',
195             '4' => 'Nahant',
196             '5' => 'Tikanga',
197             '6' => 'Santiago',
198             '7' => 'Maipo',
199             '8' => 'Ootpa',
200             '9' => 'Plow',
201             '10' => 'Coughlan',
202             );
203              
204             my %raspbian_distro_ver = (
205             '7' => 'wheezy',
206             '8' => 'jessie',
207             '9' => 'stretch',
208             '10' => 'buster',
209             '11' => 'bullseye',
210             '12' => 'bookworm',
211             '13' => 'trixie',
212             );
213              
214             # execute the cmd return 1 on success 0 on failure
215             sub ipc_run_e {
216 5     5 1 1168 my ($arg_ref) = @_;
217 5   50     52 $arg_ref->{ debug } ||= 0;
218 5 50       24 warn "cmd: $arg_ref->{ cmd }\n" if $arg_ref->{ debug };
219              
220 5 100 100     81 if (
      100        
221             scalar run(
222             command => $arg_ref->{ cmd },
223             buffer => $arg_ref->{ buf },
224             verbose => $arg_ref->{ verbose } || 0,
225             timeout => $arg_ref->{ timeout } || 10,
226             )
227             )
228             {
229 4         32884 return 1;
230             }
231 1         14750 return 0;
232             }
233              
234             # capture the output of the cmd and return it as an array or undef on failure
235             sub ipc_run_c {
236 3     3 1 3481 my ($arg_ref) = @_;
237 3   50     39 $arg_ref->{ debug } ||= 0;
238 3 50       15 warn "cmd: $arg_ref->{ cmd }\n" if $arg_ref->{ debug };
239              
240             my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf )
241             = run(
242             command => $arg_ref->{ cmd },
243             verbose => $arg_ref->{ verbose } || 0,
244 3   100     58 timeout => $arg_ref->{ timeout } || 10,
      100        
245             );
246              
247             # each element of $stdout_buf can contain multiple lines
248             # flatten to one line per element in result returned
249 3 100       52773 if ($success) {
250 2         18 my @result;
251 2         9 foreach my $lines ( @{ $stdout_buf } ) {
  2         86  
252 2         19 foreach my $line ( split( /\n/, $lines ) ) {
253 11         27 push @result, $line;
254             }
255             }
256 2         33 return @result;
257             }
258 1         19 return;
259             }
260              
261             1; # End of Dev::Util::OS
262              
263             =pod
264              
265             =encoding utf-8
266              
267             =head1 NAME
268              
269             Dev::Util::OS - OS discovery and functions
270              
271             =head1 VERSION
272              
273             Version v2.19.35
274              
275             =head1 SYNOPSIS
276              
277             OS discovery and functions
278              
279             use Disk::SmartTools::OS;
280              
281             my $OS = get_os();
282             my $hostname = get_hostname();
283             my $system_is_linux = is_linux();
284             ...
285             my $status = ipc_run_e( { cmd => 'echo hello world', buf => \$buf } );
286             my @seq = ipc_run_c( { cmd => 'seq 1 10', } );
287              
288             =head1 EXPORT
289              
290             get_os
291             get_hostname
292             is_linux
293             is_mac
294             is_freebsd
295             is_openbsd
296             is_sunos
297             ipc_run_e
298             ipc_run_c
299              
300             =head1 SUBROUTINES
301              
302             =head2 B<get_os>
303              
304             Return the OS of the current system.
305              
306             my $OS = get_os();
307              
308             =head2 B<get_hostname>
309              
310             Return the hostname of the current system.
311              
312             my $hostname = get_hostname();
313              
314             =head2 B<is_linux>
315              
316             Return true if the current system is Linux.
317              
318             my $system_is_linux = is_linux();
319              
320             =head2 B<is_mac>
321              
322             Return true if the current system is MacOS (Darwin).
323              
324             my $system_is_macOS = is_mac();
325              
326             =head2 B<is_freebsd>
327              
328             Return true if the current system is FreeBSD.
329              
330             my $system_is_FreeBSD = is_freebsd();
331              
332             =head2 B<is_openbsd>
333              
334             Return true if the current system is OpenBSD.
335              
336             my $system_is_OpenBSD = is_openbsd();
337              
338             =head2 B<is_sunos>
339              
340             Return true if the current system is SunOS.
341              
342             my $system_is_sunOS = is_sunos();
343              
344             =head2 B<ipc_run_e(ARGS_HASH)>
345              
346             Execute an external program and return the status of it's execution.
347              
348             B<ARGS_HASH:>
349             { cmd => CMD, buf => BUFFER_REF, verbose => VERBOSE_BOOL, timeout => SECONDS, debug => DEBUG_BOOL }
350              
351             C<CMD> The external command to execute
352              
353             C<BUFFER_REF> A reference to a buffer
354              
355             C<VERBOSE_BOOL:optional> 1 (default) for verbose output, 0 not so much
356              
357             C<SECONDS:optional> number of seconds to wait for CMD to execute, default: 10 sec
358              
359             C<DEBUG_BOOL: optional> Debug flag, default: 0
360              
361             my $status = ipc_run_e( { cmd => 'echo hello world', verbose => 1, timeout => 8 } );
362              
363             =head2 B<ipc_run_c(ARGS_HASH)>
364              
365             Capture the output of an external program. Return the output or return undef on failure.
366              
367             B<ARGS_HASH:>
368             { cmd => CMD, buf => BUFFER_REF, verbose => VERBOSE_BOOL, timeout => SECONDS, debug => DEBUG_BOOL }
369              
370             my @seq = ipc_run_c( { cmd => 'seq 1 10', } );
371              
372             =head1 AUTHOR
373              
374             Matt Martini, C<< <matt at imaginarywave.com> >>
375              
376             =head1 BUGS
377              
378             Please report any bugs or feature requests to C<bug-dev-util at rt.cpan.org>, or through
379             the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dev-Util>. I will
380             be notified, and then you'll automatically be notified of progress on your bug as I make changes.
381              
382             =head1 SUPPORT
383              
384             You can find documentation for this module with the perldoc command.
385              
386             perldoc Dev::Util::OS
387              
388             You can also look for information at:
389              
390             =over 4
391              
392             =item * RT: CPAN's request tracker (report bugs here)
393              
394             L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Dev-Util>
395              
396             =item * Search CPAN
397              
398             L<https://metacpan.org/release/Dev-Util>
399              
400             =back
401              
402             =head1 ACKNOWLEDGMENTS
403              
404             =head1 LICENSE AND COPYRIGHT
405              
406             This software is Copyright © 2019-2025 by Matt Martini.
407              
408             This is free software, licensed under:
409              
410             The GNU General Public License, Version 3, June 2007
411              
412             =cut
413              
414             __END__