1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# Copyright © 2010 Julian Blake Kongslie <jblake@omgwallhack.org>
# Licensed under the BSD 3-clause license.
use strict;
use warnings;
package Piny::Config;
use Moose;
use MooseX::StrictConstructor;
use Carp;
use Config::Simple qw( );
# Attributes
has 'confpath' =>
( is => 'ro'
, isa => 'Str'
, predicate => 'has_confpath'
);
has '_conf' =>
( is => 'ro'
, isa => 'HashRef[Str]'
, lazy_build => 1
, clearer => 'clear_conf'
, init_arg => undef
);
# Builder methods
# If constructed with just one argument, then treat it as a config path.
around BUILDARGS => sub {
my ( $orig, $class ) = ( shift, shift );
if ( @_ == 1 && ! ref $_[0] ) {
return $class->$orig( confpath => $_[0] );
} else {
return $class->$orig( @_ );
};
};
sub _build__conf {
my ( $s ) = @_;
my $conf;
if ( $s->has_confpath and -e $s->confpath ) {
$conf = Config::Simple->new( $s->confpath )->vars;
} else {
$conf = { };
};
if ( -e "/etc/piny-default.conf" ) {
my $default = Config::Simple->new( "/etc/piny-default.conf" )->vars;
foreach my $key ( keys %$default ) {
if ( not exists $conf->{$key} ) {
$conf->{$key} = $default->{$key};
};
};
};
if ( -e "/etc/piny-override.conf" ) {
my $override = Config::Simple->new( "/etc/piny-override.conf" )->vars;
foreach my $key ( keys %$override ) {
$conf->{$key} = $override->{$key};
};
};
return $conf;
};
# Save the config
sub save {
my ( $s ) = @_;
if ( not $s->has_confpath ) {
croak "Can't save a Piny::Config if the confpath is not set!";
};
if ( -e "/etc/piny-override.conf" ) {
my $override = Config::Simple->new( "/etc/piny-override.conf" )->vars;
foreach my $key ( keys %$override ) {
if ( exists $s->_conf->{$key} and $s->_conf->{$key} eq $override->{$key} ) {
delete $s->_conf->{$key};
};
};
};
if ( -e "/etc/piny-default.conf" ) {
my $default = Config::Simple->new( "/etc/piny-default.conf" )->vars;
foreach my $key ( keys %$default ) {
if ( exists $s->_conf->{$key} and $s->_conf->{$key} eq $default->{$key} ) {
delete $s->_conf->{$key};
};
};
};
my $cs = Config::Simple->new( syntax => "ini" );
foreach my $key ( keys %{$s->_conf} ) {
$cs->param( $key, $s->_conf->{$key} );
};
$cs->write( $s->confpath );
};
# Tweakable helper
sub tweakable {
my ( $attr, $default ) = @_;
my $attrname = $attr;
$attrname =~ s/_/./g;
has $attr =>
( is => 'rw'
, isa => 'Str'
, lazy_build => 1
, trigger => sub {
my ( $s, $new, $old ) = @_;
$s->_conf->{$attrname} = $new;
if ( $s->has_confpath ) {
$s->save;
$s->clear_conf;
} else {
carp "Attribute $attrname modification ignored!";
$s->clear_conf;
my $clearer = "clear_$attr";
$s->$clearer;
};
}
);
my $builder = sub {
my ( $s ) = @_;
if ( exists $s->_conf->{$attrname} ) {
return $s->_conf->{$attrname};
} else {
return $default;
};
};
{
no strict "refs";
*{"_build_" . $attr} = $builder;
};
};
# The tweakables
tweakable "piny_ikiwiki_destdir" => "/srv/www/piny.be/";
tweakable "piny_ikiwiki_srcdir" => "/srv/ikiwiki/";
tweakable "piny_ikiwiki_url" => "http://piny.be/";
tweakable "piny_ikiwiki_secure_url" => "https://secure.piny.be/";
tweakable "piny_ikiwiki_secure_path" => "/srv/www/secure.piny.be/";
tweakable "receive_denyNonFastforwards" => "true";
# Moose boilerplate
__PACKAGE__->meta->make_immutable;
1;
|