File Coverage

blib/lib/Ixchel/Actions/xeno.pm
Criterion Covered Total %
statement 23 74 31.0
branch 0 30 0.0
condition 0 8 0.0
subroutine 8 12 66.6
pod 2 4 50.0
total 33 128 25.7


line stmt bran cond sub pod time code
1             package Ixchel::Actions::xeno;
2              
3 1     1   83651 use 5.006;
  1         9  
4 1     1   13 use strict;
  1         2  
  1         44  
5 1     1   5 use warnings;
  1         3  
  1         53  
6 1     1   482 use File::Slurp;
  1         53953  
  1         117  
7 1     1   579 use JSON::Path;
  1         93763  
  1         12  
8 1     1   670 use YAML::XS qw(Load);
  1         3861  
  1         104  
9 1     1   631 use Ixchel::functions::file_get;
  1         5  
  1         94  
10 1     1   9 use base 'Ixchel::Actions::base';
  1         2  
  1         730  
11              
12             =head1 NAME
13              
14             Ixchel::Actions::xeno - Invokes xeno_build with the specified hash.
15              
16             =head1 VERSION
17              
18             Version 0.3.0
19              
20             =cut
21              
22             our $VERSION = '0.3.0';
23              
24             =head1 CLI SYNOPSIS
25              
26             ixchel -a xeno B<--xb>
27              
28             ixchel -a xeno B<-r>
29              
30             =head1 CODE SYNOPSIS
31              
32             use Data::Dumper;
33              
34             my $results=$ixchel->action(action=>'xeno', opts=>{r=>'librenms/extends/smart' });
35              
36             print Dumper($results);
37              
38             =head1 FLAGS
39              
40             =head2 --xb
41              
42             Read this YAML file to use for with xeno_build.
43              
44             =head2 -r
45              
46             Uses the specified value to fetch
47             'https://raw.githubusercontent.com/LilithSec/xeno_build/main/$repo_item.yaml'.
48              
49             =head2 -u
50              
51             Install use the file from URL.
52              
53             =head1 RESULT HASH REF
54              
55             .errors :: A array of errors encountered.
56             .status_text :: A string description of what was done and the results.
57             .ok :: Set to zero if any of the above errored.
58             .xeno_results :: Return from xeno_build.
59              
60             =cut
61              
62       0 0   sub new_extra { }
63              
64             sub action_extra {
65 0     0 0   my $self = $_[0];
66              
67             # if neither are defined error and return
68 0 0 0       if ( !defined( $self->{opts}{xb} ) && !defined( $self->{opts}{r} ) && !defined( $self->{opts}{u} ) ) {
      0        
69 0           my $error = 'Neither --xb, -r, or -u specified';
70 0           warn($error);
71 0           push( @{ $self->{results}{errors} }, $error );
  0            
72 0           return undef;
73             }
74              
75             # if neither are defined error and return
76 0           my $args_test = 0;
77 0 0         if ( defined( $self->{opts}{xb} ) ) {
78 0           $args_test++;
79             }
80 0 0         if ( defined( $self->{opts}{r} ) ) {
81 0           $args_test++;
82             }
83 0 0         if ( defined( $self->{opts}{u} ) ) {
84 0           $args_test++;
85             }
86 0 0         if ( $args_test >= 2 ) {
87 0           $self->status_add( error => 1, status => '--xb, -r, and/or -u specified together... can only use one' );
88 0           return undef;
89             }
90              
91 0           my $xeno_build;
92             my $xeno_build_raw;
93 0 0         if ( defined( $self->{opts}{xb} ) ) {
    0          
    0          
94 0           my $xeno_build_file;
95 0 0         if ( -f $self->{opts}{xb} ) {
    0          
    0          
96 0           $xeno_build_file = $self->{opts}{xb};
97             } elsif ( -f $self->{share_dir} . '/' . $self->{opts}{xb} ) {
98 0           $xeno_build_file = $self->{share_dir} . '/' . $self->{opts}{xb};
99             } elsif ( -f $self->{share_dir} . '/' . $self->{opts}{xb} . '.yaml' ) {
100 0           $xeno_build_file = $self->{share_dir} . '/' . $self->{opts}{xb} . '.yaml';
101             }
102 0   0       eval { $xeno_build_raw = read_file($xeno_build_file) || die( 'Failed to read "' . $xeno_build_file . '"' ); };
  0            
103 0 0         if ($@) {
104 0           $self->status_add( error => 1, status => 'Reading the specified file failed ... ' . $@ );
105 0           return undef;
106             }
107             } elsif ( defined( $self->{opts}{r} ) ) {
108 0           $self->{opts}{r} =~ s/\.yaml$//;
109 0           my $url = 'https://raw.githubusercontent.com/LilithSec/xeno_build/main/' . $self->{opts}{r} . '.yaml';
110 0           eval { $xeno_build_raw = file_get( url => $url ); };
  0            
111 0 0         if ($@) {
112 0           $self->status_add(
113             error => 1,
114             status => 'Fetching the specified item from the xeno_build repo failed ... ' . $@
115             );
116 0           return undef;
117             }
118             } elsif ( defined( $self->{opts}{u} ) ) {
119 0           eval { $xeno_build_raw = file_get( url => $self->{opts}{u} ); };
  0            
120 0 0         if ($@) {
121 0           $self->status_add( error => 1, status => 'Fetching the specified URL failed ... ' . $@ );
122 0           return undef;
123             }
124             }
125              
126             # parse the xeno_build yaml
127 0           eval { $xeno_build = Load($xeno_build_raw); };
  0            
128 0 0         if ($@) {
129 0           $self->status_add( error => 1, status => 'Decoding xeno_build YAML failed ... ' . $@ );
130 0           return undef;
131             }
132              
133 0           eval {
134             $self->{results}{xeno_results}
135 0           = $self->{ixchel}->action( action => 'xeno_build', opts => { xeno_build => $xeno_build } );
136             };
137              
138 0           return undef;
139             } ## end sub action_extra
140              
141             sub short {
142 0     0 1   return 'Invoke xeno_build on the specified hash.';
143             }
144              
145             sub opts_data {
146 0     0 1   return '
147             xb=s
148             r=s
149             u=s
150             ';
151             }
152              
153             1;