Skip to content

Commit

Permalink
Add detection for SUSE
Browse files Browse the repository at this point in the history
  • Loading branch information
DrHyde committed May 19, 2024
1 parent 7e678a4 commit 41fd4d2
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 7 deletions.
7 changes: 6 additions & 1 deletion MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,13 @@ t/etc-os-release/ubuntu
t/etc-os-release/alma
t/etc-os-release/centos
t/etc-os-release/centos-stream
t/etc-os-release/opensuse
t/etc-os-release/opensuse-leap
t/etc-os-release/oracle
t/etc-os-release/rhel
t/etc-os-release/rocky
t/etc-os-release/slackware
lib/Devel/AssertOS/Linux/OpenSUSE.pm
lib/Devel/AssertOS/Linux/SUSE.pm
t/etc-os-release/sles
t/suse.t
lib/Devel/AssertOS/Linux/SLES.pm
35 changes: 35 additions & 0 deletions lib/Devel/AssertOS/Linux/OpenSUSE.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Devel::AssertOS::Linux::OpenSUSE;

use Devel::CheckOS;
use strict;
use warnings;

use Devel::CheckOS::Helpers::LinuxOSrelease 'distributor_id';

no warnings 'redefine';

our $VERSION = '1.0';

sub os_is {
my $id = distributor_id;

Devel::CheckOS::os_is('Linux') &&
defined($id) &&
($id =~ /^opensuse-/)
}

sub expn { "The operating system is some version of OpenSUSE" }

Devel::CheckOS::die_unsupported() unless ( os_is() );

=head1 COPYRIGHT and LICENCE
Copyright 2024 David Cantrell
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.
=cut

1;


35 changes: 35 additions & 0 deletions lib/Devel/AssertOS/Linux/SLES.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Devel::AssertOS::Linux::SLES;

use Devel::CheckOS;
use strict;
use warnings;

use Devel::CheckOS::Helpers::LinuxOSrelease 'distributor_id';

no warnings 'redefine';

our $VERSION = '1.0';

sub os_is {
my $id = distributor_id;

Devel::CheckOS::os_is('Linux') &&
defined($id) &&
$id eq 'sles';
}

sub expn { "The operating system is SUSE Linux Enterprise Server" }

Devel::CheckOS::die_unsupported() unless ( os_is() );

=head1 COPYRIGHT and LICENCE
Copyright 2024 David Cantrell
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.
=cut

1;


31 changes: 31 additions & 0 deletions lib/Devel/AssertOS/Linux/SUSE.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Devel::AssertOS::Linux::SUSE;

use Devel::CheckOS;
use strict;
use warnings;

use Devel::CheckOS::Helpers::LinuxOSrelease 'distributor_id';

no warnings 'redefine';

our $VERSION = '1.0';

sub matches { map { "Linux::$_" } qw(SLES OpenSUSE) }

sub os_is { Devel::CheckOS::os_is(matches()) }

sub expn { "The operating system is some version of SUSE, (see Linux::SLES for the commercial variant, and Linux::OpenSUSE for the community version)" }

Devel::CheckOS::die_unsupported() unless ( os_is() );

=head1 COPYRIGHT and LICENCE
Copyright 2024 David Cantrell
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.
=cut

1;


6 changes: 5 additions & 1 deletion lib/Devel/CheckOS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ sub os_is {

die("Devel::CheckOS: $target isn't a legal OS name\n")
unless($target =~ /^\w+(::\w+)*$/);
eval "use Devel::AssertOS::$target";

$@ = undef;
if(! "Devel::AssertOS::$target"->can('os_is')) {
eval "use Devel::AssertOS::$target";
}
if(!$@) {
no strict 'refs';
$rval = 1 if(&{"Devel::AssertOS::${target}::os_is"}());
Expand Down
7 changes: 2 additions & 5 deletions lib/Devel/CheckOS/Helpers/LinuxOSrelease.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,18 @@ my $file_path = File::Spec->catfile('', 'etc', 'os-release');
sub _set_file { $file_path = File::Spec->catfile(getcwd, @_); }

sub distributor_id {
my $regex = qr/^ID=["']?(.+?)(["']|$)/;
my $dist_id = undef;

if ( -r $file_path ) {
open my $in, '<', $file_path or die "Cannot read $file_path: $!";
while (<$in>) {
chomp;
if ( $_ =~ $regex ) {
if ( $_ =~ /^ID=["']?(.+?)(["']|$)/ ) {
return $1;
}
}
close($in) or die "Cannot close $file_path: $!";
}

return $dist_id;
return undef;
}

=head1 COPYRIGHT and LICENCE
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions t/etc-os-release/sles
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
NAME="SLES"
VERSION="12"
VERSION_ID="12"
PRETTY_NAME="SUSE Linux Enterprise Server 12"
ID="sles"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sles:12"
23 changes: 23 additions & 0 deletions t/suse.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use warnings;
use strict;
use Test::More;
use Devel::CheckOS qw(os_is os_isnt);
use Devel::CheckOS::Helpers::LinuxOSrelease 'distributor_id';

local $^O = 'linux';

Devel::CheckOS::Helpers::LinuxOSrelease::_set_file('t/etc-os-release/sles');
ok(os_is('Linux::SLES'), "detected SLES");
ok(os_is('Linux::SUSE'), "... and also as SUSE");
ok(os_isnt('Linux::OpenSUSE'), "... but not as OpenSUSE");

Devel::CheckOS::Helpers::LinuxOSrelease::_set_file('t/etc-os-release/opensuse-tumbleweed');
ok(os_is('Linux::OpenSUSE'), "detected tumbleweed as OpenSUSE");
ok(os_is('Linux::SUSE'), "... and also as SUSE");
ok(os_isnt('Linux::SLES'), "... but not as SLES");

Devel::CheckOS::Helpers::LinuxOSrelease::_set_file('t/etc-os-release/opensuse-leap');
ok(os_is('Linux::OpenSUSE'), "detected leap as OpenSUSE");
ok(os_is('Linux::SUSE'), "... and also as SUSE");

done_testing;

0 comments on commit 41fd4d2

Please sign in to comment.