| 1 | package Devel::CheckOS::Helpers::LinuxOSrelease; | ||||||||||
| 2 | |||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 7 | ▶ | use parent 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 7 | ▶ | use Cwd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 7 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 7 | our $VERSION = '1.0'; | |||||||||
| 11 | 7 | 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</etc/os-release> 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<ID> or C<undef>, if the conditions are not those | ||||||||||
| 52 | specified above. | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | 7 | my $file_path = File::Spec->catfile('', 'etc', 'os-release'); | |||||||||
| 57 | |||||||||||
| 58 | 11 | ▶ | sub _set_file { $file_path = File::Spec->catfile(getcwd, @_); } | ||||||||
|
Subroutine
_set_file: called CC=1 SCAR=0.0
| |||||||||||
| 59 | |||||||||||
| 60 | 172 | ▶ | sub distributor_id { | ||||||||
|
Subroutine
distributor_id: called CC=6 SCAR=20.9
Pod
documented
| |||||||||||
| 61 | 172 | ▶ | if ( -r $file_path ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 172 | ▶ | open my $in, '<', $file_path or die "Cannot read $file_path: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 172 | ▶ | while (<$in>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 764 | chomp; | |||||||||
| 65 | 764 | ▶ | if ( $_ =~ /^ID=["']?(.+?)(["']|$)/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 172 | return $1; | |||||||||
| 67 | } | ||||||||||
| 68 | } | ||||||||||
| 69 | 0 | ▶ | close($in) or die "Cannot close $file_path: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 7 | 1; | |||||||||