Qgoda/Init.pm untested

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 207 CC 17 SCAR 36.5
CC
17
Cov
0%
init
110.0
__readInitYAML
12.0
command
12.0
CRAP 38.4
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Qgoda::Init;
20 $Qgoda::Init::VERSION = 'v0.9.2';
21 use strict;
22  
23 use Locale::TextDomain qw('qgoda');
24 use File::Spec;
25 use JSON '2.90';
26 use Scalar::Util qw(weaken reftype);
27  
28 use Qgoda::Util qw(read_file write_file yaml_error perl_class safe_yaml_load);
29 use Qgoda::Repository;
30  
31 sub new {
32 my ($class, $args, %options) = @_;
33  
34 my $q = Qgoda->new;
35 my $uri = @$args ? $args->[0] : 'http://github.com/gflohr/qgoda-default';
36 my $self = {
37 __logger => $q->logger,
38 __force => $options{force},
39 __config => $q->config,
40 __uri => $uri,
41 __options => \%options,
42 };
43  
44 bless $self, $class;
45 }
46  
47 sub init {
48 my ($self) = @_;
49  
50 my $logger = $self->{__logger};
51 my $config = $self->{__config};
52  
53 my $repo = Qgoda::Repository->new($self->{__uri});
54 my ($dir, $tmp) = $repo->fetch;
55  
56 my $init_yaml = File::Spec->catfile($dir, '_init.yaml');
57 $init_yaml = File::Spec->catfile($dir, '_init.yml')
58 if !-e $init_yaml;
59 my $init;
60 if (-e $init_yaml) {
61 $init = $self->__readInitYAML($init_yaml);
62 } else {
63 $logger->warning(__"Neither '_init.yaml' nor '_init.yml' found in"
64 . " repository, proceeding with defaults");
65 return {};
66 }
67  
68 $init->{_srcdir} = $dir;
69  
70 my $tasks = $init->{_tasks} || [];
71 unshift @$tasks, 'copy', 'config';
72 my %skip = map { $_ => 1 } @{$self->getOption('skip')};
73 foreach my $task (@$tasks) {
74 next if $skip{$task};
75 if (!perl_class $task) {
76 $logger->fatal(__x("Invalid task name '{name}'",
77 name => $task));
78 }
79 my $class = 'Qgoda::Init::' . $task;
80 my $module = $class;
81 $module =~ s{::}{/}g;
82 $module .= '.pm';
83 eval { require $module };
84 $logger->fatal(__x(("Cannot load task runner '{name}': {error}",
85 name => $task, error => $@)))
86 if $@;
87 }
88  
89 foreach my $task (@$tasks) {
90 next if $skip{$task};
91 my $class = 'Qgoda::Init::' . $task;
92 my $weak = $self;
93 weaken $weak;
94 my $runner = $class->new($weak);
95 $runner->run($init);
96 }
97  
98 $config = Qgoda->new->config;
99 my @helpers = keys %{$config->{helpers}};
100 if (@helpers) {
101 $logger->warning(__"IMPORTANT! The following external helper program"
102 . " will be automatically started by Qgoda:");
103 $logger->warning('-' x 30);
104 foreach my $helper (@helpers) {
105 my $args = $config->{helpers}->{$helper};
106 $args = [$args] unless ref $args && 'ARRAY' eq reftype $args;
107 my @pretty;
108 foreach my $arg (@$args) {
109 if ($arg =~ s{[\\\"]}{\\$1}) {
110 $arg =~ qq{"$arg"};
111 }
112 push @pretty, $arg;
113 }
114 my $pretty = join ' ', @pretty;
115 $logger->safeWarning(" $pretty");
116 }
117 $logger->warning('-' x 30);
118 $logger->warning(__"Please review the above list before you start qgoda!");
119 }
120  
121 return $self;
122 }
123  
124 sub getOption {
125 my ($self, $option) = @_;
126  
127 return $self->{__options}->{$option};
128 }
129  
130 sub __readInitYAML {
131 my ($self, $path) = @_;
132  
133 my $logger = $self->{__logger};
134 my $yaml = read_file $path;
135 if (!defined $yaml) {
136 $logger->fatal(__x("cannot read '{filename}': {error}",
137 filename => $path, error => $!));
138 }
139  
140 my $data = eval { safe_yaml_load $yaml };
141 $logger->fatal(yaml_error $path, $@) if $@;
142  
143 return $data;
144 }
145  
146 sub command(@) {
147 my ($self, @args) = @_;
148  
149 my @pretty;
150 foreach my $arg (@args) {
151 my $pretty = $arg;
152 $pretty =~ s{(["\\])}{\\$1}g;
153 $pretty = qq{"$pretty"} if $pretty =~ /[ \t]/;
154 push @pretty, $pretty;
155 }
156  
157 my $q = Qgoda->new;
158 my $logger = $q->logger;
159  
160 my $pretty = join ' ', @pretty;
161 $logger->info(__x("Running '{command}':", command => $pretty));
162  
163 if (0 != system @args) {
164 $logger->error(__x("Running '{command}' failed: {error}",
165 command => $pretty, error => $!));
166 return;
167 }
168  
169 return $self;
170 }
171  
172 sub __mkdir {
173 my ($self, $directory) = @_;
174  
175 return $self if -e $directory;
176  
177 my $q = Qgoda->new;
178 my $logger = $q->logger;
179  
180 $logger->info(__x("Creating directory '{directory}.",
181 directory => $directory));
182  
183 mkdir $directory
184 or $logger->fatal(__x("Error creating directory '{dir}': {error}!",
185 dir => $directory, error => $!));
186  
187 return $self;
188 }
189  
190 sub __write {
191 my ($self, $filename, $content) = @_;
192  
193 my $q = Qgoda->new;
194 my $logger = $q->logger;
195 my $config = $q->config;
196  
197 if (-e $filename && !$self->{__force}) {
198 $logger->warning(__x("Not overwriting '{filename}'!",
199 filename => $filename));
200 return $self;
201 }
202  
203 $content =~ s/\@([^\@]+)\@/$config->{$1}/g;
204  
205 $logger->info(__x("Initializing '{filename}'.", filename => $filename));
206  
207 open my $fh, '>', $filename
208 or $logger->fatal(__x("Cannot write '{filename}': {error}!\n",
209 filename => $filename, error => $!));
210 print $fh $content;
211 close $fh
212 or $logger->fatal(__x("Cannot write '{filename}': {error}!\n",
213 filename => $filename, error => $!));
214  
215 return $self;
216 }
217  
218 sub __trim {
219 my ($self, $content) = @_;
220  
221 $content =~ s/\n+$/\n/;
222  
223 return $content;
224 }
225  
226 1;
227  
228 =head1 NAME
229  
230 Qgoda::Init - Initialize Qgoda site in current directory.