File Coverage

blib/lib/Ixchel/functions/install_yq.pm
Criterion Covered Total %
statement 29 46 63.0
branch 0 10 0.0
condition 0 12 0.0
subroutine 10 11 90.9
pod 0 1 0.0
total 39 80 48.7


line stmt bran cond sub pod time code
1             package Ixchel::functions::install_yq;
2              
3 2     2   131020 use 5.006;
  2         13  
4 2     2   15 use strict;
  2         4  
  2         83  
5 2     2   11 use warnings;
  2         4  
  2         140  
6 2     2   15 use Exporter 'import';
  2         5  
  2         164  
7             our @EXPORT = qw(install_yq);
8 2     2   1186 use Rex -feature => [qw/1.4/];
  2         155990  
  2         19  
9 2     2   680343 use Rex::Hardware;
  2         5  
  2         74  
10 2     2   20 use Rex::Commands::Pkg;
  2         6  
  2         21  
11 2     2   1037 use English;
  2         5  
  2         30  
12 2     2   1228 use Ixchel::functions::github_fetch_release_asset;
  2         10  
  2         174  
13 2     2   17 use Fcntl qw( :mode );
  2         5  
  2         1287  
14              
15             # prevents Rex from printing out rex is exiting after the script ends
16             $::QUIET = 2;
17              
18             =head1 NAME
19              
20             Ixchel::functions::install_yq - Installs mikefarah/yq.
21              
22             =head1 VERSION
23              
24             Version 0.0.1
25              
26             =cut
27              
28             our $VERSION = '0.0.1';
29              
30             =head1 SYNOPSIS
31              
32             use Ixchel::functions::install_yq;
33             use Data::Dumper;
34              
35             eval{ install_yq };
36             if ($@) {
37             print 'Failed to install yq... '.$@."\n";
38             }
39              
40             # install it to /usr/local/bin/yq
41             eval{ install_yq(path=>'/usr/local/bin/yq') };
42             if ($@) {
43             print 'Failed to install yq... '.$@."\n";
44             }
45              
46             The available options are as below.
47              
48             - path :: Where to install to if not using a package.
49             Default :: /usr/bin/yq
50              
51             - no_pkg :: Don't attempt to install via a package.
52             Default :: 0
53              
54             Supported OS as below...
55              
56             FreeBSD
57             Linux
58             NetBSD
59             OpenBSD
60              
61             Package support is available for the OSes below.
62              
63             FreeBSD
64              
65             =cut
66              
67             sub install_yq {
68 0     0 0   my (%opts) = @_;
69              
70 0 0 0       if ( $OSNAME eq 'freebsd' && !$opts{no_pkg} ) {
71 0           pkg( "go-yq", ensure => "present" );
72 0           return;
73             }
74              
75 0 0 0       if ( $OSNAME ne 'linux' && $OSNAME ne 'netbsd' && $OSNAME ne 'openbsd' && $OSNAME ne 'freebsd' ) {
      0        
      0        
76 0           die( $OSNAME . ' is not a supported OS' );
77             }
78              
79 0 0         if ( !defined( $opts{path} ) ) {
80 0           $opts{path} = '/usr/bin/yq';
81             }
82              
83 0           my %hw = Rex::Hardware->get(qw/ Kernel /);
84              
85 0           my $asset = 'yq_' . $OSNAME . '_' . $hw{Kernel}{architecture};
86              
87 0           eval {
88             github_fetch_release_asset(
89             owner => 'mikefarah',
90             repo => 'yq',
91             asset => $asset,
92             output => $opts{path},
93 0           pre => 0,
94             draft => 0,
95             atomic => 1,
96             append => 0,
97             return => 0,
98             );
99             };
100 0 0         if ($@) {
101 0           die( 'Fetch the latest release of yq failed... ' . $@ );
102             }
103              
104 0           system( 'chmod', '0755', $opts{path} );
105 0 0         if ( $? != 0 ) {
106 0           die( 'Failed to chmod 0755 ' . $opts{path} . ' ... ' . $@ );
107             }
108              
109             } ## end sub install_yq
110              
111             1;