bin/ncustom

filter: Statementstmt 98.7% 155 / 157 Branchbran 52.0% 26 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 87.9% 189 / 215 CC 3 SCAR 4.8
CC
3
Cov
87%
output
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.6
« lib/NCustom/Config.pm ↑ Summary
1 #!/usr/bin/perl
2  
3 # ///////////////////////////////////////////////////////////////////
4 #<< PP: POD Start <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5 # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
6  
7 =head1 NAME
8  
9 ncustom - command line interface to NCustom
10  
11 =head1 SYNOPSIS
12  
13 ncustom [{-i|--initialise}]
14  
15 [{-n|--ncustom} ncustom_filename/url ]
16  
17 [{-u|--undo} transaction_dirname ]
18  
19 [{-b|--blat}]
20  
21 [{-c|--config} name=value ]
22  
23  
24 Options:
25 --initialise purges the transaction archive
26  
27 --undo undoes the transactions specified
28  
29 --ncustom fetch and execute the ncustom scripts specified
30  
31 --blat blat personal configuration with global configuration
32  
33 --config edit settings in personal configuration file
34  
35  
36  
37 =head1 ABSTRACT
38  
39 A command line interface to the NCustom module.
40 Currently invocation of only the simplest NCustom methods is supported, namely initialising the transaction archive, undoing transactions, and executing new NCustom scripts.
41  
42 =head1 DESCRIPTION
43  
44 =head1 OPTIONS
45  
46 =over 8
47  
48 =cut
49  
50 # ///////////////////////////////////////////////////////////////////
51 #<< CC: Code <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
52 # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
53 #====================================================================
54 # Inline testing
55 # setup only
56  
57 =begin testing
58  
59 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use File::Compare ;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use File::Copy ;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use File::Path ;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use File::Spec ;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use vars qw($ncustom $output $input);
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 # test setup
67 0 $output = File::Spec->rel2abs("./t/embedded-bin-ncustom.o");
68 0 $input = File::Spec->rel2abs("./t/embedded-bin-ncustom.i");
69 1 ▶ok( -d $input)
Branch: 1 branch
BranchTrueFalseTotal
&ok(-d $input) or diag("TEST:<test setup> requires the data input directory be present") 1 0 1
70 || diag("TEST:<test setup> requires the data input directory be present");
71  
72 1 ▶-d $input || die; # as if we have that wrong we could clobber allsorts
Branch: 1 branch
BranchTrueFalseTotal
-d $input or die 1 0 1
73 1 rmtree $output;
74 1 mkpath $output;
75 1 $ENV{HOME} = $output ; # lets be non-intrusive
76  
77 #system("ncustom") && $ncustom = "ncustom";
78 1 ▶if(-f "./bin/ncustom"){$ncustom = "./bin/ncustom" }
Branch: 1 branch
BranchTrueFalseTotal
if (-f "./bin/ncustom") 1 0 1
79 1 ▶ok($ncustom ne "")
Branch: 1 branch
BranchTrueFalseTotal
&ok($ncustom ne "") or diag("TEST:<test setup> must be able to find ncustom program") 1 0 1
80 || diag("TEST:<test setup> must be able to find ncustom program");
81  
82  
83 5 ▶sub test_reset {
Subroutine test_reset: called CC=1 SCAR=0.0
84 #Test::Inline doesnt execute test blocks in order
85 #it does all basic tests first (seemingly in declaration order),
86 #then examples tests (seemingly in declaration order).
87 #hmmm.. test_rest can erase "why test failed" data
88 1 rmtree $output;
89 1 mkpath $output;
90 5 system("cp -r $input/subject/* $output");
91 }
92  
93 7 ▶sub output {
Subroutine output: called CC=3 SCAR=11.1
94 5 ▶ $_STDOUT_ && diag($_STDOUT_);
Branch: 1 branch
BranchTrueFalseTotal
$_STDOUT_ and diag($_STDOUT_) 0 7 7
95 5 ▶ $_STDERR_ && diag($_STDERR_);
Branch: 1 branch
BranchTrueFalseTotal
$_STDERR_ and diag($_STDERR_) 2 5 7
96 }
97  
98 1 output();
99  
100 =end testing
101 1  
102 1 =cut
103  
104 #====================================================================
105  
106 1 use 5.008;
107 1 use strict qw(vars);
108 use warnings;
109 use Getopt::Long;
110 use Pod::Usage;
111  
112 # get opts
113 my ($initialise, @ncustom, @undo, $blat, %config );
114 my $rc = GetOptions (
115 "initialise" => \$initialise,
116 "ncustom=s" => \@ncustom,
117 "undo=s" => \@undo,
118 "blat" => \$blat,
119 "config=s" => \%config);
120 unless($rc){pod2usage(2) }
121  
122 # handle opts
123 my $run = 0 ;
124 my $ok = 1;
125 if($initialise ){ initialise() or $ok = 0; $run = 1; }
126 if($#ncustom >= 0){ ncustom() or $ok = 0; $run = 1; }
127 if($#undo >= 0){ undo() or $ok = 0; $run = 1; }
128 if($blat ){ blat() or $ok = 0; $run = 1; }
129 if(%config ){ config() or $ok = 0; $run = 1; }
130  
131 # finished
132 unless($run){pod2usage(2)}
133 exit (! $ok); #perl 1 == TRUE, shell 0 == TRUE
134  
135 #====================================================================
136 # general
137  
138 =begin testing
139  
140 1 test_reset();
141 1 my ($rc, $o);
142 1 $rc = system("$ncustom -junk > /dev/null 2>&1");
143 1 ▶ok($rc != 0)
Branch: 1 branch
BranchTrueFalseTotal
&ok($rc != 0) or diag("TEST:<general> returns error for invalid arguments") 1 0 1
144 || diag("TEST:<general> returns error for invalid arguments");
145 1 $o = `$ncustom 2>&1 `;
146 1 ▶like( $o, qr/Usage:/m)
Branch: 1 branch
BranchTrueFalseTotal
&like($o, qr/Usage:/m) or diag("TEST:<general> displays useage for invalid arguments") 0 1 1
147 || diag("TEST:<general> displays useage for invalid arguments");
148 1 $rc = system("$ncustom > /dev/null 2>&1");
149 1 ▶ok($rc != 0)
Branch: 1 branch
BranchTrueFalseTotal
&ok($rc != 0) or diag("TEST:<general> returns error for no arguments") 1 0 1
150 || diag("TEST:<general> returns error for no arguments");
151 1 $o = `$ncustom 2>&1 `;
152 1 ▶like( $o, qr/Usage:/m)
Branch: 1 branch
BranchTrueFalseTotal
&like($o, qr/Usage:/m) or diag("TEST:<general> displays useage for no arguments") 0 1 1
153 || diag("TEST:<general> displays useage for no arguments");
154  
155 1 output();
156  
157 =end testing
158 1  
159 1 =cut
160  
161 #====================================================================
162 # initialise
163 1  
164 1 =item B<-i, --initialise>
165  
166 Initialises (purges) the transaction archive. The transactions will no longer be able to be undone.
167  
168  
169 =begin testing
170  
171 1 test_reset();
172  
173 1 ▶ok(! -d "$output/.ncustom/save/all")
Branch: 1 branch
BranchTrueFalseTotal
&ok(!(-d "$output/.ncustom/save/all")) or diag("TEST:<test setup> require void setup, ie no ~/.ncustom...") 1 0 1
174 || diag("TEST:<test setup> require void setup, ie no ~/.ncustom...");
175  
176 1 my ($rc);
177 1 $rc = system("$ncustom -i");
178 1 ▶ok($rc == 0)
Branch: 1 branch
BranchTrueFalseTotal
&ok($rc == 0) or diag("TEST:<initialise> returns success for void initialisation") 0 1 1
179 || diag("TEST:<initialise> returns success for void initialisation");
180  
181 # NB: we just called $ncustom, which used NCustom, so ~/.ncustom now exists
182 1 ▶ok(-d "$output/.ncustom/save/all")
Branch: 1 branch
BranchTrueFalseTotal
&ok(-d "$output/.ncustom/save/all") or diag("TEST:<test setup> require ~/.ncustom...") 0 1 1
183 || diag("TEST:<test setup> require ~/.ncustom...");
184 1 system("mkdir -p $output/.ncustom/save/dummydir");
185 1 system("echo content > $output/.ncustom/save/dummydir/dummyfile");
186 1 ▶ok(-f "$output/.ncustom/save/dummydir/dummyfile")
Branch: 1 branch
BranchTrueFalseTotal
&ok(-f "$output/.ncustom/save/dummydir/dummyfile") or diag("TEST:<test setup> require dummy transaction") 1 0 1
187 || diag("TEST:<test setup> require dummy transaction");
188 1 $rc = system("$ncustom -i");
189 1 ▶ok($rc == 0)
Branch: 1 branch
BranchTrueFalseTotal
&ok($rc == 0) or diag("TEST:<initialise> returns success for initialisation") 0 1 1
190 || diag("TEST:<initialise> returns success for initialisation");
191 1 ▶ok(! -f "$output/.ncustom/save/dummydir/dummyfile")
Branch: 1 branch
BranchTrueFalseTotal
&ok(!(-f "$output/.ncustom/save/dummydir/dummyfile")) or diag("TEST:<initialise> purges transactions") 0 1 1
192 || diag("TEST:<initialise> purges transactions");
193 1 output();
194  
195 =end testing
196 1  
197 1 =cut
198  
199 #====================================================================
200 sub initialise {
201 1 require NCustom;
202 1 return NCustom::initialise();
203 }
204  
205 #====================================================================
206 # undo
207  
208 =item B<-u,--undo>
209  
210 =begin example
211  
212 1 test_reset();
213 1 my $eg ;
214  
215 =end example
216  
217 =for example begin
218  
219 1 $eg = <<' end_eg';
220
221 grep -c incomplete ~/file1 >> ~/log
222  
223 ncustom -n test5.ncus ;
224 grep -c incomplete ~/file1 >> ~/log
225  
226 ncustom -n test6.ncus ;
227 grep -c incomplete ~/file1 >> ~/log
228  
229 ncustom -u test6.ncus ;
230 grep -c incomplete ~/file1 >> ~/log
231
232 cat ~/log # 7,3,1,3
233  
234 end_eg
235  
236 =for example end
237  
238 Undo is followed by one or more transaction names, transaction names are sub-directoy names within the transaction archive directory.
239 There is a sub-directoy "all" that contains the entire journal of transactions since the last initialise.
240 Transaction names are created when using the NCusom module in NCustom scripts.
241 They are generaly named after the basename of the NCustom script, and generaly the is a one to one relationship, but the NCustom script may override these behaviours.
242  
243 =for example_testing
244 1 copy("$input/file1", "$output");
245 1 copy("$input/test5.ncus", "$output");
246 1 copy("$input/test6.ncus", "$output");
247 1 chmod 750, "$output/test5.ncus", "$output/test6.ncus";
248 #
249 1 $eg =~ s/ncustom/$ncustom/g ; # or insert ncustom in path ?
250 1 system("$eg");
251 #
252 1 open(LOG, "< $output/log");
253 1 my @lines = <LOG>;
254 1 close(LOG);
255 1 my @expected_lines = ("7\n", "3\n", "1\n", "3\n");
256 #
257 1 ▶eq_array( \@lines, \@expected_lines )
Branch: 1 branch
BranchTrueFalseTotal
eq_array(\@lines, \@expected_lines) or diag("TEST:<undo> undoes transactions") 0 1 1
258 || diag("TEST:<undo> undoes transactions");
259 #
260 1 output();
261  
262 1 =cut
263 1  
264 #====================================================================
265 sub undo {
266 1 my $dirs = join("\n", @undo);
267 1 require NCustom;
268 return NCustom::undo_files($dirs);
269 1 }
270 1  
271 1 #====================================================================
272 # ncustom
273  
274 =item B<-n,--ncustom>
275  
276 =begin example
277  
278 1 test_reset();
279 1 my $eg ;
280  
281 =end example
282  
283 =for example begin
284  
285 1 $eg = <<' end_eg';
286
287 # default_dir contains test2.ncus
288 # default_url contains test3.ncus
289
290 ncustom -n ~/dir20/test1.ncus -n test2.ncus ;
291 ncustom -n test3.ncus -n http://install/install/NCustom/test4.ncus ;
292
293 end_eg
294  
295 =for example end
296  
297 Ncustom is followed by one or more filenames, either local filenames or URLs.
298 The filenames are assumed to be NCustom scripts, are fetched, and executed.
299 If the filename is not an NCustom script, then transactions will not be journalled, and will not be able to be undone.
300 An unqualified NCustom script name will be searched for in the loaction(s) specified in NCustom::Config.
301 Settings in NCustom::Config may be overridden using ~/.ncustom/NCustom/MyConfig.pm.
302  
303 =for example_testing
304 1 mkpath "$output/dir20";
305 1 copy("$input/test1.ncus", "$output/dir20");
306 1 copy("$input/test2.ncus", "$output");
307 1 chmod 0750, "$output/dir20/test1.ncus", "$output/test2.ncus";
308 #now tell me again, how we are getting test3.ncus to url ?
309 #should make tests conditional on config - but hey test config test chicken egg
310 1 #
311 1 $eg =~ s/ncustom/$ncustom/g ; # or insert ncustom in path ?
312 1 system("$eg");
313 #
314 1 open(STUBSLOG, "< $output/stubs.log");
315 1 my @stubslog = <STUBSLOG>;
316 1 close(STUBSLOG);
317 #
318 1 ▶ok( grep( /ncustom test1.ncus/, @stubslog) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
&ok(grep(/ncustom test1.ncus/, @stubslog) > 0) or diag("TEST:<ncustom> fetches and executes file from given dir") 0 1 1
319 || diag("TEST:<ncustom> fetches and executes file from given dir");
320 1 ▶ok( grep( /ncustom test2.ncus/, @stubslog) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
&ok(grep(/ncustom test2.ncus/, @stubslog) > 0) or diag("TEST:<ncustom> fetches and executes file from default dir") 0 1 1
321 || diag("TEST:<ncustom> fetches and executes file from default dir");
322 1 ▶ok( grep( /ncustom test3.ncus/, @stubslog) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
&ok(grep(/ncustom test3.ncus/, @stubslog) > 0) or diag("TEST:<ncustom> fetches and executes file from default url") 0 1 1
323 || diag("TEST:<ncustom> fetches and executes file from default url");
324 1 ▶ok( grep( /ncustom test4.ncus/, @stubslog) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
&ok(grep(/ncustom test4.ncus/, @stubslog) > 0) or diag("TEST:<ncustom> fetches and executes file from given url") 0 1 1
325 || diag("TEST:<ncustom> fetches and executes file from given url");
326 #
327 1 output();
328  
329 1 =cut
330 1  
331 #====================================================================
332 sub ncustom {
333 1 my $files = join("\n", @ncustom);
334 1 require NCustom;
335 return NCustom::ncustom($files);
336 1 }
337 1  
338 1 #====================================================================
339 # blat
340  
341 =item B<-b, --blat>
342  
343 Blat overwrites the personal configuration profile with the global conf iguration profile. The personal configuration profile is "~/.ncustom/NCustom/MyConfig.pm".
344  
345  
346 =begin testing
347  
348 1 my $rc = system("$ncustom -b");
349 1 ▶ok($rc == 0)
Branch: 1 branch
BranchTrueFalseTotal
&ok($rc == 0) or diag("TEST:<blat_myconfig> returns success for void initialisation") 0 1 1
350 || diag("TEST:<blat_myconfig> returns success for void initialisation");
351 1 ▶is(compare("$output/.ncustom/NCustom/MyConfig.pm", "$input/Global.pm"), 0)
Branch: 1 branch
BranchTrueFalseTotal
&is(scalar compare("$output/.ncustom/NCustom/MyConfig.pm", "$input/Global.pm"), 0) or diag("TEST:<blat_myconfig> MyConfig.pm replaced by Config.pm") 0 1 1
352 || diag("TEST:<blat_myconfig> MyConfig.pm replaced by Config.pm");
353 1 output();
354  
355 =end testing
356 1  
357 1 =cut
358  
359  
360 1 #====================================================================
361 1 sub blat {
362 1 require NCustom;
363 return NCustom::blat_myconfig();
364 }
365  
366 #====================================================================
367 # config
368  
369 =item B<-c,--config>
370  
371 =begin example
372  
373 1 test_reset();
374 1 my $eg2 ;
375  
376 =end example
377  
378 1 =for example begin
379  
380 1 $eg2 = <<' end_eg';
381 1  
382 1 # modify existing values
383 ncustom -c src_fqdn=\"install.baneharbinger.com\" ;
384 ncustom -c test_url1=\"install.baneharbinger.com/index.html\" ;
385  
386 # add new values
387 ncustom -c my_number=5 -c my_text=\"blah\" ;
388  
389 # add new complex (eg hash) values
390 ncustom -c my_hosts='{ mew => "192.168.0.10", pikachu => "192.168.0.20" }' ;
391  
392 end_eg
393  
394 =for example end
395  
396 Config is followed by name vaule pairs. If there is a corresponding name in the personal configuration file, then its vaule shall be updated. If there is no corresponding name then the name value shall be added to the end of the file. If there is no file it shall be created. The personal configuration file is "~/.ncustom/NCustom/MyConfig.pm".
397  
398 If some configuration vlaues are defined in terms of other configuration values, then the order may be important.
399  
400 The current implementation is simplistic and erroneous in all but the simplest cases (eg the pre-existing name value assignment is only a one liner).
401  
402 =for example_testing
403 #
404 1 $eg2 =~ s/ncustom/$ncustom/g ; # or insert ncustom in path ?
405 1 system("$eg2");
406 #
407 1 my @lines ;
408 1 open(MYCFG, "< $output/.ncustom/NCustom/MyConfig.pm");
409 1 @lines = <MYCFG>;
410 1 close(MYCFG);
411 1 ▶ok( grep( /src_fqdn.*"install.baneharbinger.com"/, @lines) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
&ok(grep(/src_fqdn.*"install.baneharbinger.com"/, @lines) > 0) or diag("TEST:<config_edit> can edit(add) src_fqdn") 0 1 1
412 || diag("TEST:<config_edit> can edit(add) src_fqdn");
413 1 ▶ok( grep( /test_url1.*"install.baneharbinger.com\/index.html"/, @lines) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
&ok(grep(m[test_url1.*"install.baneharbinger.com/index.html"], @lines) > 0) or diag("TEST:<config_edit> can edit(add) test_url1") 0 1 1
414 || diag("TEST:<config_edit> can edit(add) test_url1");
415 #
416 1 output();
417  
418 1 =cut
419 1  
420 #====================================================================
421 sub config {
422 1 require NCustom;
423 1 return NCustom::config_edit(%config);
424 }
425  
426 #====================================================================
427  
428 # ///////////////////////////////////////////////////////////////////
429 #<< PP: POD end <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
430 # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
431  
432 =back
433  
434 =head1 SEE ALSO
435  
436 NCustom
437 NCustom::Config
438 ncustom
439  
440 http://baneharbinger.com/NCustom
441  
442 =head1 AUTHOR
443  
444 Bane Harbinger, E<lt>bane@baneharbinger.comE<gt>
445  
446 =head1 COPYRIGHT AND LICENSE
447  
448 Copyright 2003 by Bane Harbinger
449  
450 This library is free software; you can redistribute it and/or modify
451 it under the same terms as Perl itself.
452  
453 =cut
454  
« lib/NCustom/Config.pm ↑ Summary