diff options
author | Julian Blake Kongslie <jblake@omgwallhack.org> | 2010-10-11 21:53:06 -0700 |
---|---|---|
committer | Julian Blake Kongslie <jblake@omgwallhack.org> | 2010-10-11 21:53:06 -0700 |
commit | 0e5be768a7128745bd1f4a6a3904eb67a131e719 (patch) | |
tree | 5cd7ce823f97a45ed3cb853b251084d4e81c17a3 /usr/src/libpiny/lib/Piny/Config.pm | |
parent | 2d90167e054a7dbbf2ccb1bb68e0160c355b1b8c (diff) | |
download | piny-code-0e5be768a7128745bd1f4a6a3904eb67a131e719.tar.gz piny-code-0e5be768a7128745bd1f4a6a3904eb67a131e719.zip |
Making libpiny saner
This is a rewrite of libpiny to make much better use of Moose, and have a
cleaner overall architecture.
Signed-off-by: Julian Blake Kongslie <jblake@omgwallhack.org>
Diffstat (limited to 'usr/src/libpiny/lib/Piny/Config.pm')
-rw-r--r-- | usr/src/libpiny/lib/Piny/Config.pm | 175 |
1 files changed, 105 insertions, 70 deletions
diff --git a/usr/src/libpiny/lib/Piny/Config.pm b/usr/src/libpiny/lib/Piny/Config.pm index ecc6d89..6cca2f6 100644 --- a/usr/src/libpiny/lib/Piny/Config.pm +++ b/usr/src/libpiny/lib/Piny/Config.pm @@ -1,119 +1,154 @@ # Copyright © 2010 Julian Blake Kongslie <jblake@omgwallhack.org> # Licensed under the BSD 3-clause license. +use strict; +use warnings; + package Piny::Config; -use MooseX::Singleton; +use Moose; +use MooseX::StrictConstructor; -use YAML::Tiny qw( ); +use Carp; +use Config::Simple qw( ); # Attributes -has '_conf' => - ( is => 'ro' - , isa => 'HashRef[Str]' - , lazy_build => 1 - , init_arg => undef - ); - -has 'ikiwiki_destdir' => +has 'confpath' => ( is => 'ro' , isa => 'Str' - , lazy_build => 1 - , init_arg => undef + , predicate => 'has_confpath' ); -has 'ikiwiki_srcdir' => - ( is => 'ro' - , isa => 'Str' - , lazy_build => 1 - , init_arg => undef - ); - -has 'ikiwiki_url' => - ( is => 'ro' - , isa => 'Str' - , lazy_build => 1 - , init_arg => undef - ); - -has 'ikiwiki_secure_url' => - ( is => 'ro' - , isa => 'Str' - , lazy_build => 1 - , init_arg => undef - ); - -has 'ikiwiki_secure_path' => +has '_conf' => ( is => 'ro' - , isa => 'Str' + , isa => 'HashRef[Str]' , lazy_build => 1 , init_arg => undef ); # Builder methods -sub _build__conf { - my ( $s ) = @_; +# If constructed with just one argument, then treat it as a config path. +around BUILDARGS => sub { + my ( $orig, $class ) = ( shift, shift ); - if ( -e "/etc/piny.conf" ) { - my $yaml = YAML::Tiny->new; - return $yaml->read( "/etc/piny.conf" )->[0]; + if ( @_ == 1 && ! ref $_[0] ) { + return $class->$orig( confpath => $_[0] ); } else { - return { }; + return $class->$orig( @_ ); }; }; -sub _build_ikiwiki_destdir { +sub _build__conf { my ( $s ) = @_; - if ( exists $s->_conf->{"ikiwiki_destdir"} ) { - return $s->_conf->{"ikiwiki_destdir"}; + my $conf; + + if ( $s->has_confpath and -e $s->confpath ) { + $conf = Config::Simple->new( $s->confpath )->vars; } else { - return "/srv/www/piny.be/"; + $conf = { }; }; -}; -sub _build_ikiwiki_srcdir { - my ( $s ) = @_; + 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 ( exists $s->_conf->{"ikiwiki_srcdir"} ) { - return $s->_conf->{"ikiwiki_srcdir"}; - } else { - return "/srv/ikiwiki/"; }; -}; -sub _build_ikiwiki_url { - my ( $s ) = @_; + 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}; + }; - if ( exists $s->_conf->{"ikiwiki_url"} ) { - return $s->_conf->{"ikiwiki_url"}; - } else { - return "http://piny.be/"; }; + + return $conf; }; -sub _build_ikiwiki_secure_url { +# Save the config + +sub save { my ( $s ) = @_; - if ( exists $s->_conf->{"ikiwiki_secure_url"} ) { - return $s->_conf->{"ikiwiki_secure_url"}; - } else { - return "https://secure.piny.be/"; + if ( not $s->has_confpath ) { + croak "Can't save a Piny::Config if the confpath is not set!"; + }; + + my $cs = Config::Simple->new( syntax => "ini" ); + + foreach my $key ( keys %{$s->_conf} ) { + $cs->param( $key, $s->_conf->{$key} ); }; + + $cs->write( $s->confpath ); }; -sub _build_ikiwiki_secure_path { - my ( $s ) = @_; +# 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, $old, $new ) = @_; + + $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; + }; + }; - if ( exists $s->_conf->{"ikiwiki_secure_path"} ) { - return $s->_conf->{"ikiwiki_secure_path"}; - } else { - return "/srv/www/secure.piny.be/"; + { + 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; |