File Coverage

blib/lib/Devel/CheckOS/Helpers/LinuxOSrelease.pm
Criterion Covered Total %
statement 22 24 91.6
branch 4 8 50.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 34 40 85.0


line stmt bran cond sub pod time code
1             package Devel::CheckOS::Helpers::LinuxOSrelease;
2              
3 7     7   1487 use strict;
  7         16  
  7         238  
4 7     7   49 use warnings;
  7         11  
  7         340  
5 7     7   2106 use parent 'Exporter';
  7         1717  
  7         65  
6              
7 7     7   473 use Cwd;
  7         14  
  7         522  
8 7     7   38 use File::Spec;
  7         15  
  7         2444  
9              
10             our $VERSION = '1.0';
11             our @EXPORT_OK = qw(distributor_id);
12              
13             =pod
14              
15             =head1 NAME
16              
17             Devel::CheckOS::Helpers::LinuxOSrelease - functions to deal with /etc/os-release file
18              
19             =head1 SYNOPSIS
20              
21             use Devel::CheckOS::Helpers::LinuxOSrelease 'distributor_id';
22             my $id = distributor_id;
23              
24             =head1 DESCRIPTION
25              
26             This module exports functions to handle text files related to Debian-like
27             distributions.
28              
29             =head1 EXPORTED
30              
31             The following subs are exported.
32              
33             =head2 distributor_id
34              
35             Retrieves and returns the distributor ID from the F file.
36              
37             It is expected that the file exists, it is readable and have the following
38             (minimum) content format:
39              
40             NAME="Ubuntu"
41             VERSION_ID="22.04"
42             VERSION="22.04.4 LTS (Jammy Jellyfish)"
43             VERSION_CODENAME=jammy
44             ID=ubuntu
45             ID_LIKE=debian
46             HOME_URL="https://www.ubuntu.com/"
47              
48             This excerpt is from Ubuntu 22.04, but other distributions might have fewer,
49             more or different fields and values.
50              
51             It returns the value of C or C, if the conditions are not those
52             specified above.
53              
54             =cut
55              
56             my $file_path = File::Spec->catfile('', 'etc', 'os-release');
57              
58 11     11   586053 sub _set_file { $file_path = File::Spec->catfile(getcwd, @_); }
59              
60             sub distributor_id {
61 172 50   172 1 7112 if ( -r $file_path ) {
62 172 50       8127 open my $in, '<', $file_path or die "Cannot read $file_path: $!";
63 172         7426 while (<$in>) {
64 764         1462 chomp;
65 764 100       3424 if ( $_ =~ /^ID=["']?(.+?)(["']|$)/ ) {
66 172         3999 return $1;
67             }
68             }
69 0 0         close($in) or die "Cannot close $file_path: $!";
70             }
71              
72 0           return undef;
73             }
74              
75             =head1 COPYRIGHT and LICENCE
76              
77             Copyright 2024 David Cantrell
78              
79             This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively.
80              
81             =cut
82              
83             1;