From c4fb9497982e2f51c1afefd43f94be8c6cc74edc Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Fri, 13 May 2011 15:15:07 -0700 Subject: Fixing a few bugs in config handling, and support default-to-user-config. --- libpiny/lib/Piny/Config.pm | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) (limited to 'libpiny') diff --git a/libpiny/lib/Piny/Config.pm b/libpiny/lib/Piny/Config.pm index 3c1ae3f..acf63ff 100644 --- a/libpiny/lib/Piny/Config.pm +++ b/libpiny/lib/Piny/Config.pm @@ -13,6 +13,8 @@ use MooseX::StrictConstructor; use Carp; use Config::Simple qw( -lc ); +use Piny::Environment; + # Types subtype 'GitBool' @@ -91,6 +93,20 @@ sub _build__conf { $conf = { }; }; + my $home = Piny::Environment->instance->user->home . "/.gitconfig"; + + if ( -s $home ) { + + my $userconf = Config::Simple->new( $home )->vars; + + foreach my $key ( keys %$userconf ) { + if ( not exists $conf->{$key} ) { + $conf->{$key} = $userconf->{$key}; + }; + }; + + }; + if ( -s "/etc/piny-default.conf" ) { my $default = Config::Simple->new( "/etc/piny-default.conf" )->vars; @@ -129,28 +145,30 @@ sub save { croak "Can't save a Piny::Config if the confpath is not set!"; }; - if ( -s "/etc/piny-override.conf" ) { + my ( $override, $userconf, $default ) = ( {}, {}, {} ); - my $override = Config::Simple->new( "/etc/piny-override.conf" )->vars; + if ( -s "/etc/piny-override.conf" ) { + $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}; - }; - }; + my $home = Piny::Environment->instance->user->home . "/.gitconfig"; + if ( -s $home ) { + $userconf = Config::Simple->new( $home )->vars; }; if ( -s "/etc/piny-default.conf" ) { + $default = Config::Simple->new( "/etc/piny-default.conf" )->vars; + }; - 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}; - }; + foreach my $key ( keys %{$s->_conf} ) { + if ( exists $override->{$key} ) { + delete $s->_conf->{$key}; + } elsif ( exists $userconf->{$key} ) { + delete $s->_conf->{$key} if $userconf->{$key} eq $s->_conf->{$key}; + } elsif ( exists $default->{$key} ) { + delete $s->_conf->{$key} if $default->{$key} eq $s->_conf->{$key}; }; - }; my $cs = Config::Simple->new( syntax => "ini" ); -- cgit v1.2.3