bin/cobalt2

filter: Statementstmt 31.6% 31 / 98 Branchbran 3.3% 1 / 30 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 64.2% 9 / 14 Podpod n/a totaltotal 27.3% 41 / 150 SCAR 44.7
CC
13
Cov
24%
_start_cobalt
45.0
_check_dirs
12.0
_check_cfs
12.0
CRAP 87.0
« blib/lib/Bot/Cobalt/Serializer.pm ↑ Summary blib/lib/Bot/Cobalt/Conf/File/Plugins.pm »
1 #!perl
2 ## simplistic cobalt2 Bot::Cobalt::Core frontend
3  
4 1 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 ## Default rcfile location.
11 my $rcfile = $ENV{HOME} ?
12 1 ▶ File::Spec->catfile( $ENV{HOME}, ".cobalt2rc" )
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'HOME'} ? : 1 0 1
13 : ".cobalt2rc" ;
14  
15 1 my $opt_debug = 0;
16 1 my $opt_detach = 1;
17 1 my $loglevel = 'info';
18 1 my ($etcdir, $vardir, $basedir);
19  
20 # FIXME switch to Daemon::Control? drops Proc::PID::File
21 # and all the custom double-forkness
22 1 ▶use Proc::PID::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use Bot::Cobalt::Frontend::RC qw/rc_read/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 GetOptions(
28 ## Path to .cobalt2rc
29 'rcfile=s' => \$rcfile,
30 'config=s' => \$rcfile,
31  
32 ## Override .cobalt2rc
33 'rundir=s' => \$basedir,
34 'base=s' => \$basedir,
35  
36 ## Invocation opts
37 'debug:+' => \$opt_debug, ## Overrides loglevel=
38 'detach!' => \$opt_detach,
39 'daemon!' => \$opt_detach,
40 'loglevel=s' => \$loglevel,
41  
42 ## Informational
43 version => sub {
44 0 ▶ require Bot::Cobalt;
Subroutine __ANON__: not called CC=2 SCAR=17.9
45 0 ▶ my $vers = $Bot::Cobalt::VERSION // 'vcs';
Condition0%0/2
$Bot::Cobalt::VERSION // "vcs"
l!l
00
MC/DC0%0/1
$Bot::Cobalt::VERSION // "vcs"
$Bot::Cobalt::VERSION
Truth table0%0/2
A
A= $Bot::Cobalt::VERSION
Aresult
11
00
46 0 print "cobalt $vers\n";
47 0 exit 0
48 },
49  
50 1 help => \&show_help,
51 );
52  
53 sub show_help {
54 1 ▶ print(
Subroutine show_help: called CC=1 SCAR=0.0
55 "cobalt2 invocation help \n",
56 " --version \n",
57 " Display current Bot::Cobalt::Core version \n",
58  
59 "\n",
60 " Execution:\n",
61 " --nodetach / --nodaemon \n",
62 " Run in the foreground (do not daemonize) \n",
63 " --loglevel=LOGLEVEL \n",
64 " Specify log verbosity. Defaults to 'info' \n",
65 " Valid levels, most verbose to least: \n",
66 " debug info notice warn err crit alert emerg \n",
67 " --debug / --nodebug / --debug=LEVEL\n",
68 " Enable debug output. Overrides loglevel. \n",
69 " Higher levels offer more verbosity. \n",
70  
71 ## FIXME; note on POCOIRC_DEBUG and POE debug opts ?
72  
73 "\n",
74 " Paths:\n",
75 " --rcfile=/path/to/rcfile \n",
76 " Specify a rcfile. Defaults to \$HOME/.cobalt2rc \n",
77 " --base=/path/to/basedir \n",
78 " Specify base path for 'etc/' and 'var/' for this instance\n",
79 " Overrides rcfile. \n",
80 );
81  
82 1 exit 0
83 }
84  
85 sub _rc_check {
86 0 ▶ unless (-e $rcfile) {
Subroutine _rc_check: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (-e $rcfile) { } 0 0 0
87 0 say ">! rcfile $rcfile not found.";
88 0 say ">! You can specify one via --rcfile=";
89 0 say ">! If this is your first time running cobalt2, try `cobalt2-installer`";
90 0 die "rcfile not found"
91 } else {
92 0 return 1
93 }
94 }
95  
96 sub _check_dirs {
97 0 ▶ my %paths = (
Subroutine _check_dirs: not called CC=3 SCAR=24.8
98 etcdir => $etcdir,
99 vardir => $vardir,
100 );
101 0 for my $thisname (keys %paths) {
102 0 my $thispath = $paths{$thisname};
103 0 ▶ unless (-e $thispath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $thispath) 0 0 0
104 0 say ">! $thisname ($thispath) doesn't appear to exist.";
105 0 say ">! Your rcfile ($rcfile) may be broken.";
106 0 say ">! Perhaps try `cobalt2-installer`";
107 0 die "$thisname not a directory"
108 }
109 }
110 }
111  
112 sub _check_cfs {
113 ## Check if required confs exist in etcdir
114 ## Otherwise suggest cobalt2-installer
115  
116 0 ▶ my @required = qw/ cobalt.conf channels.conf plugins.conf /;
Subroutine _check_cfs: not called CC=3 SCAR=24.8
117  
118 0 for my $file (@required) {
119 0 ▶ unless (-e File::Spec->catfile($etcdir, $file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e "File::Spec"->catfile($etcdir, $file)) 0 0 0
120 0 say ">! Missing core conf: $file";
121 0 say ">! (etcdir: $etcdir)";
122 0 say ">! You may want to try `cobalt2-installer`";
123 0 die "missing core conf: $file"
124 }
125 }
126 }
127  
128  
129 sub _start_cobalt {
130 0 ▶ my $pid = Proc::PID::File->new(
Subroutine _start_cobalt: not called CC=7 SCAR=38.1
131 dir => $vardir,
132 name => 'cobalt',
133 );
134 0 ▶ die "cobalt appears to be already running\n"
Branch: 1 branch
BranchTrueFalseTotal
if $pid->alive 0 0 0
135 if $pid->alive;
136  
137 ## POSIX fork dance
138 1 ▶ use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
139 0 ▶ if ($opt_detach)
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_detach) 0 0 0
140 {
141 0 say "Starting cobalt in background";
142 0 my $fork = fork;
143 0 ▶ exit 1 if not defined $fork;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fork 0 0 0
144 0 ▶ exit 0 if $fork;
Branch: 1 branch
BranchTrueFalseTotal
if $fork 0 0 0
145 0 POSIX::setsid();
146 0 $fork = fork;
147 0 ▶ exit 1 if not defined $fork;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fork 0 0 0
148 0 ▶ exit 0 if $fork;
Branch: 1 branch
BranchTrueFalseTotal
if $fork 0 0 0
149 0 chdir('/');
150 0 open(STDIN, '<', '/dev/null');
151 0 open(STDOUT, '>>', '/dev/null');
152 0 open(STDERR, '>>', '/dev/null');
153 0 umask(022);
154 }
155 0 $pid->touch();
156  
157 0 require Bot::Cobalt::Conf;
158 0 require Bot::Cobalt::Core;
159  
160 ## FIXME could take paths to specific confs now
161 0 my $cfg = Bot::Cobalt::Conf->new(
162 etc => $etcdir,
163 debug => $opt_debug,
164 );
165  
166 0 Bot::Cobalt::Core->instance(
167 cfg => $cfg,
168 var => $vardir,
169 loglevel => $loglevel,
170 debug => $opt_debug,
171 detached => $opt_detach,
172 )->init;
173  
174 0 POE::Kernel->run;
175 }
176  
177  
178 0 ▶say "-> debug ON, overrides loglevel" if $opt_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_debug 0 0 0
179 ## Bot::Cobalt::Core does this anyway, but just for the validator:
180 0 ▶$loglevel = $opt_debug ? 'debug' : lc $loglevel ;
Branch: 1 branch
BranchTrueFalseTotal
$opt_debug ? : 0 0 0
181 ## Check specified loglevel
182 0 my @loglevels = qw/debug info notice warn warning
183 err error crit critical alert
184 emerg emergency/;
185  
186 0 ▶unless ($loglevel && grep { $_ eq $loglevel } @loglevels) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loglevel and grep {$_ eq $loglevel;} @loglevels) 0 0 0
Condition0%0/3
$loglevel and grep {$_ eq $loglevel;} @loglevels
!ll&&!rl&&r
000
MC/DC0%0/2
$loglevel and grep {$_ eq $loglevel;} @loglevels
$loglevel grep {$_ eq $loglevel;} @loglevels
Truth table0%0/3
A and B
A= $loglevelB= grep {$_ eq $loglevel;} @loglevels
ABresult
0X0
100
111
187 0 say("Invalid loglevel ($loglevel)");
188 0 say("Possible loglevels, most verbose to least: ".join(' ',@loglevels));
189 0 say("Setting loglevel to INFO");
190  
191 0 $loglevel = 'info';
192 }
193  
194  
195 0 ▶if ($basedir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basedir) { } 0 0 0
196 0 say ">! Using --basedir=${basedir}";
197 ## A basedir was specified, disregard cobalt2rc
198 0 ▶ unless (-e $basedir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $basedir) 0 0 0
199 0 die "basedir $basedir specified but nonexistant";
200 }
201  
202 0 $etcdir = File::Spec->catdir($basedir, "etc");
203 0 $vardir = File::Spec->catdir($basedir, "var");
204  
205 } else {
206 ## no basedir specified, try rcfile
207 0 _rc_check();
208 0 ($basedir, $etcdir, $vardir) = rc_read($rcfile);
209 }
210  
211  
212 0 _check_dirs();
213 0 _check_cfs();
214 0 _start_cobalt();
215  
216 __END__
« blib/lib/Bot/Cobalt/Serializer.pm ↑ Summary blib/lib/Bot/Cobalt/Conf/File/Plugins.pm »