| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Ixchel::Actions::template; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
88353
|
use 5.006; |
|
|
1
|
|
|
|
|
3
|
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
27
|
|
|
5
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
35
|
|
|
6
|
1
|
|
|
1
|
|
442
|
use File::Slurp; |
|
|
1
|
|
|
|
|
30984
|
|
|
|
1
|
|
|
|
|
83
|
|
|
7
|
1
|
|
|
1
|
|
378
|
use Ixchel::functions::sys_info; |
|
|
1
|
|
|
|
|
12
|
|
|
|
1
|
|
|
|
|
113
|
|
|
8
|
1
|
|
|
1
|
|
14
|
use String::ShellQuote; |
|
|
1
|
|
|
|
|
2092
|
|
|
|
1
|
|
|
|
|
77
|
|
|
9
|
1
|
|
|
1
|
|
24
|
use File::Find::Rule; |
|
|
1
|
|
|
|
|
10409
|
|
|
|
1
|
|
|
|
|
13
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Ixchel::Actions::template - Fill in a template. |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 VERSION |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Version 0.2.0 |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = '0.2.0'; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 CLI SYNOPSIS |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
ixchel -a template B<-t> |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 CODE SYNOPSIS |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
use Data::Dumper; |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $results=$ixchel->action(action=>'template', opts=>{np=>1, t=>$template_name}); |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
print Dumper($results); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Fills in the specified template. |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
The templates in question are L templates. |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
The following are available for use in the template. |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
- opts :: A hash with the various options specified. In the case of |
|
45
|
|
|
|
|
|
|
calling this from this CLI, see info on FLAGS. |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
- config :: The Ixchel config hash. |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
- argv :: @ARGV |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
- vars :: Any additional variables passed. Only usable when calling |
|
52
|
|
|
|
|
|
|
via $ixchel->action and not the CLI. |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
The following functions are available. |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
- shell_quote :: shell_quote from String::ShellQuote. |
|
57
|
|
|
|
|
|
|
- args :: String to quote. |
|
58
|
|
|
|
|
|
|
- return :: A quoted string. |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
- file_exists :: Check if the specified path exists and is a file. |
|
61
|
|
|
|
|
|
|
- args :: A FS path, relattive or absolute. |
|
62
|
|
|
|
|
|
|
- return :: 0/1 |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
- dir_exists :: Check if the specified path exists and is a dir. |
|
65
|
|
|
|
|
|
|
- args :: A FS path, relattive or absolute. |
|
66
|
|
|
|
|
|
|
- return :: 0/1 |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
- get_sub_dirs :: Gets a array of directories using... |
|
69
|
|
|
|
|
|
|
File::Find::Rule->directory->maxdepth(1)->in( $_[0] ); |
|
70
|
|
|
|
|
|
|
- args :: A FS path, relattive or absolute. |
|
71
|
|
|
|
|
|
|
- return :: An array of directories. |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
- is_freebsd :: If the OS is FreeBSD or not. |
|
74
|
|
|
|
|
|
|
- args :: A FS path, relattive or absolute. |
|
75
|
|
|
|
|
|
|
- return :: 0/1 |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
- is_linux :: If the OS is Linux or not. |
|
78
|
|
|
|
|
|
|
- args :: A FS path, relattive or absolute. |
|
79
|
|
|
|
|
|
|
- return :: 0/1 |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
- is_systemd :: If the OS is Linux and if the init system is systemd. |
|
82
|
|
|
|
|
|
|
- args :: A FS path, relattive or absolute. |
|
83
|
|
|
|
|
|
|
- return :: 0/1 |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
- read_file :: Reads the specified file. |
|
86
|
|
|
|
|
|
|
- args :: A FS path, relattive or absolute. |
|
87
|
|
|
|
|
|
|
- return :: Contents of the specified file. |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 FLAGS |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 --np |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Do not print the the filled in template. |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 -t |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
The name of the template to use. |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
This is resolved in order... |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
./$template |
|
102
|
|
|
|
|
|
|
/usr/local/etc/ixchel/templates/$template |
|
103
|
|
|
|
|
|
|
/usr/local/etc/ixchel/templates/$template.tt |
|
104
|
|
|
|
|
|
|
$share_dir/templates/$template |
|
105
|
|
|
|
|
|
|
$share_dir/templates/$template.tt |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=cut |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub new { |
|
110
|
0
|
|
|
0
|
0
|
|
my ( $empty, %opts ) = @_; |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
my $self = { |
|
113
|
|
|
|
|
|
|
config => {}, |
|
114
|
|
|
|
|
|
|
vars => {}, |
|
115
|
|
|
|
|
|
|
arggv => [], |
|
116
|
|
|
|
|
|
|
opts => {}, |
|
117
|
|
|
|
|
|
|
ixchel => $opts{ixchel}, |
|
118
|
0
|
|
|
|
|
|
}; |
|
119
|
0
|
|
|
|
|
|
bless $self; |
|
120
|
|
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
|
if ( defined( $opts{config} ) ) { |
|
122
|
0
|
|
|
|
|
|
$self->{config} = $opts{config}; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
|
if ( defined( $opts{t} ) ) { |
|
126
|
0
|
|
|
|
|
|
$self->{t} = $opts{t}; |
|
127
|
|
|
|
|
|
|
} else { |
|
128
|
0
|
|
|
|
|
|
die('$opts{t} is undef'); |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
0
|
0
|
|
|
|
|
if ( defined( $opts{share_dir} ) ) { |
|
132
|
0
|
|
|
|
|
|
$self->{share_dir} = $opts{share_dir}; |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
0
|
0
|
|
|
|
|
if ( defined( $opts{opts} ) ) { |
|
136
|
0
|
|
|
|
|
|
$self->{opts} = \%{ $opts{opts} }; |
|
|
0
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
0
|
0
|
|
|
|
|
if ( defined( $opts{argv} ) ) { |
|
140
|
0
|
|
|
|
|
|
$self->{argv} = $opts{argv}; |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
0
|
0
|
|
|
|
|
if ( defined( $opts{vars} ) ) { |
|
144
|
0
|
|
|
|
|
|
$self->{vars} = $opts{vars}; |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
|
|
147
|
0
|
0
|
|
|
|
|
if ( defined( $opts{vars} ) ) { |
|
148
|
0
|
|
|
|
|
|
$self->{vars} = $opts{vars}; |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
return $self; |
|
152
|
|
|
|
|
|
|
} ## end sub new |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub action { |
|
155
|
0
|
|
|
0
|
0
|
|
my $self = $_[0]; |
|
156
|
|
|
|
|
|
|
|
|
157
|
0
|
0
|
|
|
|
|
if ( !defined( $self->{opts}->{t} ) ) { |
|
158
|
0
|
|
|
|
|
|
die('No template specified via -t'); |
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
my $template = $self->{opts}->{t}; |
|
162
|
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
my $template_file; |
|
164
|
0
|
0
|
|
|
|
|
if ( -f $template ) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
$template_file = $template; |
|
166
|
|
|
|
|
|
|
} elsif ( -f '/usr/local/etc/ixchel/templates/' . $template ) { |
|
167
|
0
|
|
|
|
|
|
$template_file = '/usr/local/etc/ixchel/templates/' . $template; |
|
168
|
|
|
|
|
|
|
} elsif ( -f '/usr/local/etc/ixchel/templates/' . $template . '.tt' ) { |
|
169
|
0
|
|
|
|
|
|
$template_file = '/usr/local/etc/ixchel/templates/' . $template . '.tt'; |
|
170
|
|
|
|
|
|
|
} elsif ( -f $self->{share_dir} . '/templates/' . $template ) { |
|
171
|
0
|
|
|
|
|
|
$template_file = $self->{share_dir} . '/templates/' . $template; |
|
172
|
|
|
|
|
|
|
} elsif ( -f $self->{share_dir} . '/templates/' . $template . '.tt' ) { |
|
173
|
0
|
|
|
|
|
|
$template_file = $self->{share_dir} . '/templates/' . $template . '.tt'; |
|
174
|
|
|
|
|
|
|
} else { |
|
175
|
0
|
|
|
|
|
|
$self->{ixchel}{errors_count}++; |
|
176
|
0
|
|
|
|
|
|
die( 'Unable to locate template "' . $template . '" in either the current dir or ' . $self->{share_dir} ); |
|
177
|
|
|
|
|
|
|
} |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
my $vars = { |
|
180
|
|
|
|
|
|
|
opts => $self->{opts}, |
|
181
|
|
|
|
|
|
|
config => $self->{config}, |
|
182
|
|
|
|
|
|
|
argv => $self->{argv}, |
|
183
|
|
|
|
|
|
|
vars => $self->{vars}, |
|
184
|
|
|
|
|
|
|
sys_info => sys_info, |
|
185
|
|
|
|
|
|
|
shell_quote => \&shell_quote, |
|
186
|
|
|
|
|
|
|
file_exists => sub { |
|
187
|
0
|
|
|
0
|
|
|
eval { |
|
188
|
0
|
0
|
|
|
|
|
if ( -f $_[0] ) { |
|
189
|
0
|
|
|
|
|
|
return 1; |
|
190
|
|
|
|
|
|
|
} |
|
191
|
0
|
|
|
|
|
|
return 0; |
|
192
|
|
|
|
|
|
|
}; |
|
193
|
|
|
|
|
|
|
}, |
|
194
|
|
|
|
|
|
|
dir_exists => sub { |
|
195
|
0
|
|
|
0
|
|
|
eval { |
|
196
|
0
|
0
|
|
|
|
|
if ( -d $_[0] ) { |
|
197
|
0
|
|
|
|
|
|
return 1; |
|
198
|
|
|
|
|
|
|
} |
|
199
|
0
|
|
|
|
|
|
return 0; |
|
200
|
|
|
|
|
|
|
}; |
|
201
|
|
|
|
|
|
|
}, |
|
202
|
|
|
|
|
|
|
get_sub_dirs => sub { |
|
203
|
0
|
|
|
0
|
|
|
eval { |
|
204
|
0
|
0
|
|
|
|
|
if ( !defined( $_[0] ) ) { |
|
205
|
0
|
|
|
|
|
|
return; |
|
206
|
|
|
|
|
|
|
} |
|
207
|
0
|
|
|
|
|
|
return File::Find::Rule->directory->maxdepth(1)->in( $_[0] ); |
|
208
|
|
|
|
|
|
|
}; |
|
209
|
|
|
|
|
|
|
}, |
|
210
|
|
|
|
|
|
|
is_freebsd => sub { |
|
211
|
0
|
0
|
|
0
|
|
|
if ( $^O eq 'freebsd' ) { |
|
212
|
0
|
|
|
|
|
|
return 1; |
|
213
|
|
|
|
|
|
|
} |
|
214
|
0
|
|
|
|
|
|
return 0; |
|
215
|
|
|
|
|
|
|
}, |
|
216
|
|
|
|
|
|
|
is_linux => sub { |
|
217
|
0
|
0
|
|
0
|
|
|
if ( $^O eq 'linux' ) { |
|
218
|
0
|
|
|
|
|
|
return 1; |
|
219
|
|
|
|
|
|
|
} |
|
220
|
0
|
|
|
|
|
|
return 0; |
|
221
|
|
|
|
|
|
|
}, |
|
222
|
|
|
|
|
|
|
is_systemd => sub { |
|
223
|
0
|
0
|
0
|
0
|
|
|
if ( $^O eq 'linux' && ( -f '/usr/bin/systemctl' || -f '/bin/systemctl' ) ) { |
|
|
|
|
0
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
return 1; |
|
225
|
|
|
|
|
|
|
} |
|
226
|
0
|
|
|
|
|
|
return 0; |
|
227
|
|
|
|
|
|
|
}, |
|
228
|
|
|
|
|
|
|
read_file => sub { |
|
229
|
0
|
|
|
0
|
|
|
eval { return read_file( $_[0] ); }; |
|
|
0
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
}, |
|
231
|
0
|
|
|
|
|
|
}; |
|
232
|
|
|
|
|
|
|
|
|
233
|
0
|
|
|
|
|
|
my $template_data = read_file($template_file); |
|
234
|
0
|
0
|
|
|
|
|
if ( !defined($template_data) ) { |
|
235
|
0
|
|
|
|
|
|
$self->{ixchel}{errors_count}++; |
|
236
|
0
|
|
|
|
|
|
die( '"' . $template_file . '" could not be read' ); |
|
237
|
|
|
|
|
|
|
} |
|
238
|
|
|
|
|
|
|
|
|
239
|
0
|
|
|
|
|
|
my $output = ''; |
|
240
|
|
|
|
|
|
|
$self->{t}->process( \$template_data, $vars, \$output ) |
|
241
|
0
|
0
|
|
|
|
|
|| die( 'Failed to process template... ' . $self->{t}->error ); |
|
242
|
|
|
|
|
|
|
|
|
243
|
0
|
0
|
|
|
|
|
if ( !$self->{opts}->{np} ) { |
|
244
|
0
|
|
|
|
|
|
print $output; |
|
245
|
|
|
|
|
|
|
} |
|
246
|
|
|
|
|
|
|
|
|
247
|
0
|
|
|
|
|
|
return $output; |
|
248
|
|
|
|
|
|
|
} ## end sub action |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
sub short { |
|
251
|
0
|
|
|
0
|
0
|
|
return 'Fills in a template.'; |
|
252
|
|
|
|
|
|
|
} |
|
253
|
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
sub opts_data { |
|
255
|
0
|
|
|
0
|
0
|
|
return 't=s |
|
256
|
|
|
|
|
|
|
np'; |
|
257
|
|
|
|
|
|
|
} |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
1; |