File Coverage

blib/lib/Sys/OsPackage/Driver/Alpine.pm
Criterion Covered Total %
statement 12 46 26.0
branch 0 22 0.0
condition n/a
subroutine 4 9 44.4
pod 0 5 0.0
total 16 82 19.5


line stmt bran cond sub pod time code
1             # Sys::OsPackage::Driver::Alpine
2             # ABSTRACT: Alpine APK packaging handler for Sys::OsPackage
3             # Copyright (c) 2022 by Ian Kluft
4             # Open Source license Perl's Artistic License 2.0:
5             # SPDX-License-Identifier: Artistic-2.0
6              
7             # This module is maintained for minimal dependencies so it can build systems/containers from scratch.
8              
9             ## no critic (Modules::RequireExplicitPackage)
10             # This resolves conflicting Perl::Critic rules which want package and strictures each before the other
11 2     2   1325 use strict;
  2         3  
  2         105  
12 2     2   12 use warnings;
  2         4  
  2         134  
13 2     2   11 use utf8;
  2         21  
  2         19  
14             ## use critic (Modules::RequireExplicitPackage)
15              
16             package Sys::OsPackage::Driver::Alpine;
17             $Sys::OsPackage::Driver::Alpine::VERSION = '0.4.0';
18 2     2   173 use parent "Sys::OsPackage::Driver";
  2         3  
  2         28  
19              
20             # check if packager command found (alpine)
21             sub pkgcmd
22             {
23 0     0 0   my ( $class, $ospkg ) = @_;
24              
25 0 0         return ( defined $ospkg->sysenv("apk") ? 1 : 0 );
26             }
27              
28             # find name of package for Perl module (alpine)
29             sub modpkg
30             {
31 0     0 0   my ( $class, $ospkg, $args_ref ) = @_;
32 0 0         return if not $class->pkgcmd($ospkg);
33              
34             # search by alpine format for Perl module packages
35 0           my $pkgname = join( "-", "perl", map { lc $_ } @{ $args_ref->{mod_parts} } );
  0            
  0            
36 0           $args_ref->{pkg} = $pkgname;
37 0 0         if ( not $class->find( $ospkg, $args_ref ) ) {
38 0           return;
39             }
40 0 0         $ospkg->debug() and print STDERR "debug(" . __PACKAGE__ . "->modpkg): $pkgname\n";
41              
42             # package was found
43 0           return $pkgname;
44             }
45              
46             # find named package in repository (alpine)
47             sub find
48             {
49 0     0 0   my ( $class, $ospkg, $args_ref ) = @_;
50 0 0         return if not $class->pkgcmd($ospkg);
51              
52 0           my $querycmd = $ospkg->sysenv("apk");
53 0           my @pkglist = sort map { substr( $_, 0, index( $_, " " ) ) }
54 0           ( $ospkg->capture_cmd( { list => 1 }, $ospkg->sudo_cmd(), $querycmd, qw(list --quiet), $args_ref->{pkg} ) );
55 0 0         return if not scalar @pkglist; # empty list means nothing found
56 0           return $pkglist[-1]; # last of sorted list should be most recent version
57             }
58              
59             # install package (alpine)
60             sub install
61             {
62 0     0 0   my ( $class, $ospkg, $args_ref ) = @_;
63 0 0         return if not $class->pkgcmd($ospkg);
64              
65             # determine packages to install
66 0           my @packages;
67 0 0         if ( exists $args_ref->{pkg} ) {
68 0 0         if ( ref $args_ref->{pkg} eq "ARRAY" ) {
69 0           push @packages, @{ $args_ref->{pkg} };
  0            
70             } else {
71 0           push @packages, $args_ref->{pkg};
72             }
73             }
74              
75             # install the packages
76 0           my $pkgcmd = $ospkg->sysenv("apk");
77 0           return $ospkg->run_cmd( $ospkg->sudo_cmd(), $pkgcmd, qw(add --quiet), @packages );
78             }
79              
80             # check if an OS package is installed locally
81             sub is_installed
82             {
83 0     0 0   my ( $class, $ospkg, $args_ref ) = @_;
84 0 0         return if not $class->pkgcmd($ospkg);
85              
86             # check if package is installed
87 0           my $querycmd = $ospkg->sysenv("apk");
88             my @pkglist = $ospkg->capture_cmd(
89             { list => 1 },
90             $ospkg->sudo_cmd(), $querycmd, qw(list --installed --quiet),
91             $args_ref->{pkg}
92 0           );
93 0 0         return ( scalar @pkglist > 0 ) ? 1 : 0;
94             }
95              
96             1;
97              
98             =pod
99              
100             =encoding UTF-8
101              
102             =head1 NAME
103              
104             Sys::OsPackage::Driver::Alpine - Alpine APK packaging handler for Sys::OsPackage
105              
106             =head1 VERSION
107              
108             version 0.4.0
109              
110             =head1 SYNOPSIS
111              
112             my $ospkg = Sys::OsPackage->instance();
113              
114             # check if packaging commands exist for this system
115             if (not $ospkg->call_pkg_driver(op => "implemented")) {
116             return 0;
117             }
118              
119             # find OS package name for Perl module
120             my $pkgname = $ospkg->call_pkg_driver(op => "find", module => $module);
121              
122             # install a Perl module as an OS package
123             my $result1 = $ospkg->call_pkg_driver(op => "modpkg", module => $module);
124              
125             # install an OS package
126             my $result2 = $ospkg->call_pkg_driver(op => "install", pkg => $pkgname);
127              
128             =head1 DESCRIPTION
129              
130             ⛔ This is for Sys::OsPackage internal use only.
131              
132             The Sys::OsPackage method call_pkg_driver() will call the correct driver for the running platform.
133             The driver implements these methods: I, I, I, I, I and I.
134              
135             =head1 SEE ALSO
136              
137             Alpine Linux docs: "Working with the Alpine Package Keeper (apk)" L
138              
139             GitHub repository for Sys::OsPackage: L
140              
141             =head1 BUGS AND LIMITATIONS
142              
143             Please report bugs via GitHub at L
144              
145             Patches and enhancements may be submitted via a pull request at L
146              
147             =head1 LICENSE INFORMATION
148              
149             Copyright (c) 2022 by Ian Kluft
150              
151             This module is distributed in the hope that it will be useful, but it is provided “as is” and without any express or implied warranties. For details, see the full text of the license in the file LICENSE or at L.
152              
153             =head1 AUTHOR
154              
155             Ian Kluft
156              
157             =head1 COPYRIGHT AND LICENSE
158              
159             This software is Copyright (c) 2022 by Ian Kluft.
160              
161             This is free software, licensed under:
162              
163             The Artistic License 2.0 (GPL Compatible)
164              
165             =cut
166              
167             __END__