File Coverage

blib/lib/App/TeleGramma/Config.pm
Criterion Covered Total %
statement 33 39 84.6
branch 4 6 66.6
condition n/a
subroutine 13 15 86.6
pod 0 11 0.0
total 50 71 70.4


line stmt bran cond sub pod time code
1             package App::TeleGramma::Config;
2             $App::TeleGramma::Config::VERSION = '0.12';
3             # ABSTRACT: TeleGramma and TeleGramma plugin configuration
4              
5 3     3   433 use Mojo::Base -base;
  3         9  
  3         33  
6 3     3   410 use File::Spec::Functions qw/catdir/;
  3         9  
  3         167  
7 3     3   1161 use Config::INI::Writer 0.025;
  3         32918  
  3         91  
8 3     3   1154 use Config::INI::Reader 0.025;
  3         57857  
  3         1461  
9              
10             has config => sub { {} };
11             has path_base => sub { catdir($ENV{HOME}, '.telegramma') };
12              
13 45     45 0 140 sub path_config { catdir(shift->path_base, 'telegramma.ini') }
14 4     4 0 37 sub path_plugins { catdir(shift->path_base, 'plugins') }
15 1     1 0 12 sub path_logs { catdir(shift->path_base, 'logs') }
16 1     1 0 12 sub path_plugin_data { catdir(shift->path_base, 'plugindata') }
17 0     0 0 0 sub path_pid { catdir(shift->path_base, 'telegramma.pid') }
18              
19             sub read {
20 36     36 0 201 my $self = shift;
21 36         83 $self->config( Config::INI::Reader->read_file( $self->path_config ) );
22             }
23              
24             sub write {
25 7     7 0 882 my $self = shift;
26 7         19 Config::INI::Writer->write_file($self->config, $self->path_config);
27             }
28              
29             sub default_config {
30             [
31 1     1 0 14 '_' => [ bot_token => 'please change me, see: https://telegram.me/BotFather' ],
32             ],
33             }
34              
35             sub create_if_necessary {
36 1     1 0 1057 my $self = shift;
37              
38 1         7 foreach my $path (
39             $self->path_base,
40             $self->path_plugins,
41             $self->path_plugin_data,
42             catdir($self->path_plugins, 'App'),
43             catdir($self->path_plugins, 'App/TeleGramma'),
44             catdir($self->path_plugins, 'App/TeleGramma/Plugin'),
45             $self->path_logs ) {
46              
47 7 100       95 if (! -e $path) {
    50          
48 6         160 mkdir $path, 0700 || die "cannot create $path: $!\n";
49             }
50             elsif (! -d $path) {
51 0         0 die "$path is not a directory?\n";
52             }
53             }
54              
55 1         8 my $config_path = $self->path_config;
56 1 50       20 if (! -e $config_path) {
57 1         5 $self->create_default_config;
58 1         5 return 1;
59             }
60              
61 0         0 return 0;
62             }
63              
64             sub create_default_config {
65 1     1 0 3 my $self = shift;
66 1         3 my $path = $self->path_config;
67              
68 1         10 Config::INI::Writer->write_file($self->default_config, $path);
69 1         9518 chmod 0600, $path;
70             }
71              
72             sub config_created_message {
73 0     0 0   my $self = shift;
74 0           my $path = $self->path_config;
75              
76             return <<EOF
77             Your new config has been created in $path
78              
79             Please edit it now and update the Telegram Bot token, then
80             re-run $0.
81              
82             The configuration will have an entry for each plugin currently available on
83             your system, but disabled.
84             EOF
85 0           }
86              
87              
88              
89             1;
90              
91             __END__
92              
93             =pod
94              
95             =encoding UTF-8
96              
97             =head1 NAME
98              
99             App::TeleGramma::Config - TeleGramma and TeleGramma plugin configuration
100              
101             =head1 VERSION
102              
103             version 0.12
104              
105             =head1 AUTHOR
106              
107             Justin Hawkins <justin@eatmorecode.com>
108              
109             =head1 COPYRIGHT AND LICENSE
110              
111             This software is copyright (c) 2017 by Justin Hawkins <justin@eatmorecode.com>.
112              
113             This is free software; you can redistribute it and/or modify it under
114             the same terms as the Perl 5 programming language system itself.
115              
116             =cut