File Coverage

blib/lib/Ixchel/Actions/dump_config.pm
Criterion Covered Total %
statement 26 60 43.3
branch 0 20 0.0
condition 0 9 0.0
subroutine 9 13 69.2
pod 2 4 50.0
total 37 106 34.9


line stmt bran cond sub pod time code
1             package Ixchel::Actions::dump_config;
2              
3 1     1   89385 use 5.006;
  1         3  
4 1     1   4 use strict;
  1         2  
  1         21  
5 1     1   3 use warnings;
  1         1  
  1         42  
6 1     1   335 use Ixchel::functions::sys_info;
  1         9  
  1         74  
7 1     1   10 use JSON qw(to_json);
  1         11128  
  1         5  
8 1     1   150 use YAML::XS qw(Dump);
  1         3084  
  1         64  
9 1     1   10 use Data::Dumper;
  1         1  
  1         35  
10 1     1   7 use JSON::Path;
  1         71301  
  1         15  
11 1     1   201 use base 'Ixchel::Actions::base';
  1         1  
  1         87  
12              
13             =head1 NAME
14              
15             Ixchel::Actions::dump_config - Prints out the config.
16              
17             =head1 VERSION
18              
19             Version 0.3.0
20              
21             =cut
22              
23             our $VERSION = '0.3.0';
24              
25             =head1 CLI SYNOPSIS
26              
27             ixchel -a [B<-o> ] [B<-s>
]
28              
29             =head1 CODE SYNOPSIS
30              
31             my $results=$ixchel->action(action=>'apt_proxy', opts=>{w=>1, np=>1});
32              
33             if ($results->{ok}) {
34             print $results->{raw};
35             }else{
36             die('Action errored... '.joined("\n", @{$results->{errors}}));
37             }
38              
39             =head1 Switches
40              
41             =head2 -o
42              
43             Format to print it in.
44              
45             Available: json, yaml, toml, dumper
46              
47             Default: yaml
48              
49             =head2 -s
50              
51             A JSON style path used for fetching a sub section of the
52             config via L.
53              
54             Default: undef
55              
56             =head1 RESULT HASH REF
57              
58             .errors :: A array of errors encountered.
59             .status_text :: A string description of what was done and the results.
60             .ok :: Set to zero if any of the above errored.
61             .raw :: The config in the specified format.
62             .config :: The config hash.
63              
64             =cut
65              
66       0 0   sub new_extra { }
67              
68             sub action_extra {
69 0     0 0   my $self = $_[0];
70              
71 0 0         if ( !defined( $self->{opts}->{o} ) ) {
72 0           $self->{opts}->{o} = 'yaml';
73             }
74              
75 0 0 0       if ( $self->{opts}{o} ne 'toml'
      0        
      0        
76             && $self->{opts}{o} ne 'json'
77             && $self->{opts}{o} ne 'dumper'
78             && $self->{opts}{o} ne 'yaml' )
79             {
80             self->status_add(
81 0           status => '-o is set to "' . $self->{opts}{o} . '" which is not a understood setting',
82             error => 1
83             );
84 0           return undef;
85             } ## end if ( $self->{opts}{o} ne 'toml' && $self->...)
86              
87 0           my $config;
88 0 0         if ( defined( $self->{opts}{s} ) ) {
89 0           eval {
90 0           my $jpath = JSON::Path->new( $self->{opts}{s} );
91 0           $config = $jpath->get( $self->{config} );
92             };
93 0 0         if ($@) {
94 0           $self->status_add( status => 'JSON::Path errored ... ' . $@, error => 1 );
95 0           return undef;
96             }
97             } else {
98 0           $config = $self->{config};
99             }
100 0           $self->{results}{config} = $config;
101              
102 0           my $string;
103 0 0         if ( $self->{opts}->{o} eq 'toml' ) {
    0          
    0          
104 0           my $to_eval = 'use TOML::Tiny qw(to_toml); $string = to_toml($config) . "\n";';
105 0           eval($to_eval);
106 0 0         if ( !$self->{opts}{np} ) {
107 0           print $string;
108             }
109             } elsif ( $self->{opts}->{o} eq 'json' ) {
110 0           my $json = JSON->new;
111 0           $json->canonical(1);
112 0           $json->pretty(1);
113 0           $string = $json->encode($config);
114 0 0         if ( !$self->{opts}{np} ) {
115 0           print $string;
116             }
117             } elsif ( $self->{opts}->{o} eq 'yaml' ) {
118 0           $string = Dump($config);
119 0 0         if ( !$self->{opts}{np} ) {
120 0           print $string;
121             }
122             }
123              
124 0           return undef;
125             } ## end sub action_extra
126              
127             sub short {
128 0     0 1   return 'Dumps the config to to JSON, YAML, or TOML(default)';
129             }
130              
131             sub opts_data {
132 0     0 1   return 'o=s
133             s=s
134             np
135             ';
136             }
137              
138             1;