Config.pm

filter: Statementstmt 78.6% 48 / 61 Branchbran 35.0% 7 / 20 Conditioncond 45.4% 5 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 66.0% 74 / 112 CC 15 SCAR 19.4
CC
15
Cov
65%
execute
13.1
dump
12.0
parse
4.8
CRAP 6.9
↑ Summary Config/Object.pm »
1 =head1 NAME
2  
3 Synapse::CLI::Config - configure and manage your application objects in a terminal
4  
5  
6 =head1 About Synapse's Open Telephony Toolkit
7  
8 L<Synapse::CLI::Config> is a part of Synapse's Wholesale Open Telephony
9 Toolkit.
10  
11 As we are refactoring our codebase, we at Synapse have decided to release a
12 substantial portion of our code under the GPL. We hope that as a developer, you
13 will find it as useful as we have found open source and CPAN to be of use to
14 us.
15  
16  
17 =head1 What is L<Synapse::CLI::Config> all about
18  
19 We strongly believe that prior to building graphical front ends, it is
20 necessary to build a robust and reliable command line interface in order to
21 configure software packages.
22  
23 The goal of this module is to provide something that will allow you to interact
24 with your application objects and classes using the command line, and make
25 object changes persistent on disk.
26  
27  
28 =head1 SYNOPSIS
29  
30 Say you create a MyAPP::User object.
31  
32  
33 =head2 Step 1
34  
35 Make MyAPP::User inherit from L<Synapse::CLI::Config::Object>:
36  
37 use base qw /Synapse::CLI::Config::Object/;
38  
39 Doing this means that your class will inherit L<Synapse::CLI::Config::Object> methods.
40  
41  
42 =head2 Step 2
43  
44 Write your own methods, for example in MyAPP::User, these could be something in
45 the lines of:
46  
47 sub password {
48 my $self = shift;
49 @_ and $self->{password} = shift;
50 return $self->{password};
51 }
52
53 sub email {
54 my $self = shift;
55 @_ and $self->{email} = shift;
56 return $self->{email};
57 }
58  
59  
60 Create a simple script, say myapp-cli, which declares aliases for your objects
61 and calls the Synapse::CLI::Config::execute() method.
62  
63 #!/usr/bin/perl
64 # this is myapp-cli. It should be installed in /usr/local/bin/myapp-cli
65 use Synapse::CLI::Config;
66 use YAML::XS;
67 use warning;
68 use strict;
69 $Synapse::CLI::Config::BASE_DIR = "/etc/myapp";
70 $Synapse::CLI::Config::ALIAS->{type} = 'Synapse::CLI::Config::Type';
71 $Synapse::CLI::Config::ALIAS->{user} = 'MyAPP::User';
72 print Dump (Synapse::CLI::Config::execute (@ARGV));
73  
74  
75 =head2 Step 3, congrats, you've built your own CLI interface!
76  
77 Now let's create a user and use these fancy methods.
78  
79 # create user 'foo' with label 'Foo Bar'
80 myapp-cli type user create foo "Foo Bar"
81
82 # change password and email
83 myapp-cli user foo email example@example.com
84 myapp-cli user foo password "very hard to remember"
85
86 # view result
87 myapp-cli user foo show
88
89 # rename foo
90 myapp-cli user foo rename bar
91
92 # get rid of it now
93 myapp-cli user bar remove
94
95  
96 =head1 API
97  
98 =cut
99 package Synapse::CLI::Config;
100 4 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104  
105 =head2 GLOBALS
106  
107 =over 4
108  
109 =item $Synapse::CLI::Config::VERSION - library version number
110  
111 =item $Synapse::CLI::Config::BASE_DIR - points to the directory where object
112 configuration files live.
113  
114 =item $Synapse::CLI::Config::ALIAS - alias => package name mapping.
115  
116 =item @Synapse::CLI::Config::BUFFER - where execute() commands are logged prior to
117 flushing() everything to disk.
118  
119 =back
120  
121 =cut
122 3 our $VERSION = 0.1;
123 3 ▶our $BASE_DIR = $ENV{CLI_CONFIG_BASEDIR} || "/etc/cli-config";
Condition50%1/2
$ENV{'CLI_CONFIG_BASEDIR'} || '/etc/cli-config'
l!l
03
MC/DC0%0/1
$ENV{'CLI_CONFIG_BASEDIR'} || '/etc/cli-config'
$ENV{'CLI_CONFIG_BASEDIR'}
Truth table50%1/2
A
A= $ENV{'CLI_CONFIG_BASEDIR'}
Aresult
11
00
124 3 our $ALIAS = {
125 type => 'Synapse::CLI::Config::Type',
126 };
127  
128  
129  
130 =head2 Synapse::CLI::Config::base_dir()
131  
132 Returns $BASE_DIR
133  
134 =cut
135 19 ▶sub base_dir {
Subroutine base_dir: called CC=1 SCAR=0.0
Pod documented
136 19 return $BASE_DIR;
137 }
138  
139  
140  
141 =head2 Synapse::CLI::Config::debug($msg)
142  
143 Debug hook. Sends messages to STDERR.
144  
145 =cut
146 1 ▶sub debug($) {
Subroutine debug: called CC=1 SCAR=0.0
Pod documented
147 1 my $msg = shift;
148 1 print STDERR ' -- ';
149 1 print STDERR scalar gmtime;
150 1 print STDERR ' -- ';
151 1 print STDERR $msg;
152 1 print STDERR "\n";
153 }
154  
155  
156 =head2 Synapse::CLI::Config::parse($file)
157  
158 Turns a YAML file into a Perl structure and returns it.
159  
160 =cut
161 1 ▶sub parse {
Subroutine parse: called CC=3 SCAR=15.7
Pod documented
162 1 my $file = shift;
163 1 ▶ -e $file || return;
Branch: 1 branch
BranchTrueFalseTotal
-e $file or return 0 0 0
164 1 ▶ open FILE, "<$file" or do {
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$file" or do { &debug("cannot read open $file"); return } 0 0 0
165 1 debug ("cannot read open $file");
166 1 return;
167 };
168 0 my $yaml = join '', <FILE>;
169 0 close FILE;
170 0 return Load $yaml;
171 }
172  
173  
174 =head2 Synapse::CLI::Config::dump($file, $scalar)
175  
176 Turns Perl $scalar (which will most often be a reference) into a YAML file.
177  
178 =cut
179 1 ▶sub dump {
Subroutine dump: called CC=3 SCAR=24.8
Pod documented
180 0 my $file = shift;
181 0 my $obj = shift;
182 0 ▶ -e $file || return;
Branch: 1 branch
BranchTrueFalseTotal
-e $file or return 0 0 0
183 0 ▶ open FILE, ">file" or do {
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">file" or do { &debug("cannot write open $file"); return } 0 0 0
184 0 debug ("cannot write open $file");
185 0 return;
186 };
187 0 print FILE Dump $obj;
188 0 close FILE;
189 }
190  
191  
192 =head2 Synapse::CLI::Config::execute (@args)
193  
194 Executes command, and saves it in the corresponding file if object is changed.
195  
196 For instance:
197  
198 $Synapse::CLI::Config::ALIASES->{foo} = "My::Foo";
199 Synapse::CLI::Config::execute ("My::Foo", "bar", "baz");
200  
201 Is like saying:
202  
203 My::Foo->new ('bar')->baz();
204  
205 On the cli it should look like this:
206  
207 myapp-cli foo bar baz
208  
209 =cut
210 9 ▶sub execute {
Subroutine execute: called CC=11 SCAR=25.8
Pod documented
211 9 ▶ my $type = shift || die "usage: $0 <objType> <objid> <method> arg1 ... argN";
Condition50%1/2
shift() || die("usage: $0 <objType> <objid> <method> arg1 ... argN")
l!l
90
MC/DC0%0/1
shift() || die("usage: $0 <objType> <objid> <method> arg1 ... argN")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
212
213 9 ▶ my $package = $Synapse::CLI::Config::ALIAS->{$type} || $type;
Condition33%1/3
$Synapse::CLI::Config::ALIAS->{$type} || $type
l!l&&r!l&&!r
900
MC/DC0%0/2
$Synapse::CLI::Config::ALIAS->{$type} || $type
$Synapse::CLI::Config::ALIAS->{$type} $type
Truth table33%1/3
A || B
A= $Synapse::CLI::Config::ALIAS->{$type}B= $type
ABresult
1X1
011
000
214 9 ▶ eval "use $package";
Subroutine BEGIN: called CC=1 SCAR=0.0
215
216 9 my $name = shift;
217 9 ▶ defined $name || die "object name unspecified";
Branch: 1 branch
BranchTrueFalseTotal
defined $name or die "object name unspecified" 9 0 9
218
219 9 ▶ my $object = $package->new ($name) || die "$name does not exist";
Condition50%1/2
$package->new($name) || die("$name does not exist")
l!l
90
MC/DC0%0/1
$package->new($name) || die("$name does not exist")
$package->new($name)
Truth table50%1/2
A
A= $package->new($name)
Aresult
11
00
220 9 ▶ my $method = shift || die "method unspecified";
Condition50%1/2
shift() || die('method unspecified')
l!l
90
MC/DC0%0/1
shift() || die('method unspecified')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
221 9 $method =~ s/-/_/g;
222 9 ▶ $object->can ($method) || die "no such method";
Branch: 1 branch
BranchTrueFalseTotal
$object->can($method) or die "no such method" 9 0 9
223
224 9 my $before = YAML::XS::Dump ($object);
225 9 my $res;
226 9 eval { $res = $object->$method (@_) };
227 9 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 9 9
228 9 my $after = YAML::XS::Dump ($object);
229
230 9 my $FORCE_SAVE = $method . "_FORCE_SAVE";
231 9 my $FORCE_NOSAVE = $method . "_FORCE_NOSAVE";
232
233 # if $object->method_FORCE_SAVE() exists, always logs
234 9 ▶ if ($object->can ($FORCE_SAVE)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($object->can($FORCE_SAVE)) { } 0 9 9
elsif ($object->can($FORCE_NOSAVE)) { } 1 8 9
235 0 $object->__save__ ($method => @_);
236 }
237
238 # if $object->method_FORCE_NOSAVE() exists, never logs
239 elsif ($object->can ($FORCE_NOSAVE)) {
240 }
241
242 # otherwise, logs only if object has changed
243 else {
244 0 ▶ $before ne $after and do { $object->__save__ ($method => @_) }
Branch: 1 branch
BranchTrueFalseTotal
$before ne $after and do { $object->__save__($method, @_) } 0 8 8
245 }
246
247 9 return $res;
248 }
249  
250  
251 3 1;
252  
253  
254 __END__
↑ Summary Config/Object.pm »