Skip to content

Commit

Permalink
Add configuration support.
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Apr 17, 2012
1 parent 9415210 commit 25d03d8
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Build.PL
Expand Up @@ -12,10 +12,14 @@ Module::Build->new(
'Module::Build' => '0.35',
'Test::MockModule' => '0.05',
'Test::More' => '0.17',
'File::Spec' => 0,
},
requires => {
'Class::Accessor::Fast' => '0.31',
'Config::INI::Reader' => 0,
'File::HomeDir' => 0,
'Getopt::Long' => 0,
'Path::Class' => 0,
},
recommends => {
'Test::Pod' => '1.41',
Expand Down
47 changes: 47 additions & 0 deletions lib/App/Sqitch.pm
Expand Up @@ -145,6 +145,53 @@ sub _pod2usage {
);
}

sub _global_config_root {
require Path::Class;
return Path::Class::dir($ENV{SQITCH_GLOBAL_CONFIG_ROOT})
if $ENV{SQITCH_GLOBAL_CONFIG_ROOT};

require File::HomeDir;
my $homedir = File::HomeDir->my_home
or croak("Could not determine home directory");

return Path::Class::dir($homedir)->subdir('.sqitch');
}

sub _load_config {
my $self = shift;

# Read the local and global configs.
my $local = $self->_read_ini('sqitch.ini');
my $global = $self->_read_ini( $self->_global_config_root->file('config.ini') );

if ($global && $local) {
# Merge them.
for my $section (keys %{ $local }) {
if ($global->{$section}) {
# Merge the section.
$global->{$section} = {
%{ $global->{$section} },
%{ $local->{$section} },
};
} else {
# Copy the section whole-hog.
$global->{$section} = $local->{$section};
}
}
return $global;
} else {
# Return whatever we've got.
return $global || $local || {};
}
}

sub _read_ini {
my ($self, $file) = @_;
return unless -f $file;
require Config::INI::Reader;
return Config::INI::Reader->read_file($file);
}

1;

__END__
Expand Down
11 changes: 11 additions & 0 deletions t/config.ini
@@ -0,0 +1,11 @@
[core.pg]
client = /opt/local/pgsql/bin/psql
username = postgres
host = localhost

[core.mysql]
client = /opt/local/mysql/bin/mysql
username = root

[core.sqlite]
client = /opt/local/bin/sqlite3
98 changes: 98 additions & 0 deletions t/config.t
@@ -0,0 +1,98 @@
#!/usr/bin/perl -w

use strict;
use warnings;
use Test::More tests => 5;
#use Test::More 'no_plan';
use File::Spec;

my $CLASS;
BEGIN {
$CLASS = 'App::Sqitch';
use_ok $CLASS or die;
}

is_deeply $CLASS->_load_config, {},
'Should have no config by default';

my $config_ini = {
'core.pg' => {
client => '/opt/local/pgsql/bin/psql',
username => 'postgres',
host => 'localhost',
},
'core.mysql' => {
client => '/opt/local/mysql/bin/mysql',
username => 'root',
},
'core.sqlite' => {
client => '/opt/local/bin/sqlite3',
}
};

# Test loding the global config.
GLOBAL: {
local $ENV{SQITCH_GLOBAL_CONFIG_ROOT} = 't';
is_deeply $CLASS->_load_config, $config_ini,
'Should load config.ini for global config';
}

my $sqitch_ini = {
"core" => {
db => "widgetopolis",
engine => "pg",
extension => "ddl",
sql_dir => "migrations",
},
"core.pg" => {
client => "/usr/local/pgsql/bin/psql",
username => "theory",
},
"revert" => {
to => "gamma",
},
"bundle" => {
dest_dir => "_build/sql",
from => "gamma",
tags_only => "yes",
},
};

chdir 't';
# Test loading local file.
is_deeply $CLASS->_load_config, $sqitch_ini,
'Should load sqitch.ini for local config';

my $both_ini = {
"core" => {
db => "widgetopolis",
engine => "pg",
extension => "ddl",
sql_dir => "migrations",
},
"core.pg" => {
client => "/usr/local/pgsql/bin/psql",
username => "theory",
host => 'localhost',
},
'core.mysql' => {
client => '/opt/local/mysql/bin/mysql',
username => 'root',
},
'core.sqlite' => {
client => '/opt/local/bin/sqlite3',
},
"revert" => {
to => "gamma",
},
"bundle" => {
dest_dir => "_build/sql",
from => "gamma",
tags_only => "yes",
},
};

# Test merging.
$ENV{SQITCH_GLOBAL_CONFIG_ROOT} = File::Spec->curdir;
is_deeply $CLASS->_load_config, $both_ini,
'Should merge both ini files with both present';
17 changes: 17 additions & 0 deletions t/sqitch.ini
@@ -0,0 +1,17 @@
[core]
engine = pg
db = widgetopolis
sql_dir = migrations
extension = ddl

[core.pg]
client = /usr/local/pgsql/bin/psql
username = theory

[revert]
to = gamma

[bundle]
from = gamma
tags_only = yes
dest_dir = _build/sql

0 comments on commit 25d03d8

Please sign in to comment.