Permalink
Browse files

Write configured settings to init config as comments.

Already done for engine settings, now done for core settings.

While at it, to keep things consistent, App::Sqitch no longer merges configuration and options before construction. Instead, the individaul accessors' defaults now read from the config. This gets around the previous need to shove the config into the sqitch object's underlying hash; it is just passed to the constructor.
  • Loading branch information...
1 parent 46a2898 commit beeb12cccf5501511f2435f02bc35fef554ad238 @theory committed Apr 27, 2012
Showing with 194 additions and 52 deletions.
  1. +1 −0 Build.PL
  2. +3 −3 Changes
  3. +35 −15 lib/App/Sqitch.pm
  4. +45 −20 lib/App/Sqitch/Command/init.pm
  5. +1 −2 t/base.t
  6. +7 −4 t/config.t
  7. +0 −1 t/configuration.t
  8. +99 −6 t/init.t
  9. +0 −1 t/sqitch.conf
  10. +3 −0 t/user.conf
View
@@ -12,6 +12,7 @@ Module::Build->new(
'Module::Build' => '0.35',
'Test::Dir' => 0,
'Test::File' => 0,
+ 'Test::File::Contents' => '0.05',
'Test::Exception' => 0,
'Test::MockModule' => '0.05',
'Test::More' => '0.17',
View
@@ -3,9 +3,9 @@ Revision history for Perl extension App::Sqitch
0.12
- Added `--local` option to `sqitch config`.
- Renamed `project_file()` to `--local_file()` in App::Sqitch::Config.
- - `sqitch init` now writes engine config settings with default values to
- the configuration file. This makes it easier for folks to get started
- editing it.
+ - `sqitch init` now writes core and engine config settings with default
+ values to the configuration file. This makes it easier for folks to get
+ started editing it.
0.11 2012-04-27T06:44:54Z
- Implemented `init` command.
View
@@ -20,7 +20,9 @@ has plan_file => (is => 'ro', required => 1, default => sub {
file 'sqitch.plan';
});
-has _engine => (is => 'ro', isa => enum [qw(pg mysql sqlite)]);
+has _engine => (is => 'ro', lazy => 1, isa => maybe_type(enum [qw(pg mysql sqlite)]), default => sub {
+ shift->config->get(key => 'core.engine');
+});
has engine => (is => 'ro', isa => 'Maybe[App::Sqitch::Engine]', lazy => 1, default => sub {
my $self = shift;
my $name = $self->_engine or return;
@@ -35,34 +37,53 @@ has username => (is => 'ro', isa => 'Str');
has host => (is => 'ro', isa => 'Str');
has port => (is => 'ro', isa => 'Int');
-has sql_dir => (is => 'ro', required => 1, lazy => 1, default => sub { dir 'sql' });
+has sql_dir => (is => 'ro', required => 1, lazy => 1, default => sub {
+ dir shift->config->get(key => 'core.sql_dir') || 'sql';
+});
has deploy_dir => (is => 'ro', required => 1, lazy => 1, default => sub {
- shift->sql_dir->subdir('deploy');
+ my $self = shift;
+ if (my $dir = $self->config->get(key => 'core.deploy_dir')) {
+ return dir $dir;
+ }
+ $self->sql_dir->subdir('deploy');
});
has revert_dir => (is => 'ro', required => 1, lazy => 1, default => sub {
- shift->sql_dir->subdir('revert');
+ my $self = shift;
+ if (my $dir = $self->config->get(key => 'core.revert_dir')) {
+ return dir $dir;
+ }
+ $self->sql_dir->subdir('revert');
});
has test_dir => (is => 'ro', required => 1, lazy => 1, default => sub {
- shift->sql_dir->subdir('test');
+ my $self = shift;
+ if (my $dir = $self->config->get(key => 'core.test_dir')) {
+ return dir $dir;
+ }
+ $self->sql_dir->subdir('test');
});
-has extension => (is => 'ro', isa => 'Str', default => 'sql');
+has extension => (is => 'ro', isa => 'Str', lazy => 1, default => sub {
+ shift->config->get(key => 'core.extension') || 'sql';
+});
has dry_run => (is => 'ro', isa => 'Bool', required => 1, default => 0);
-has verbosity => (is => 'ro', required => 1, default => 1);
+has verbosity => (is => 'ro', required => 1, lazy => 1, default => sub {
+ shift->config->get(key => 'core.verbosity') // 1;
+});
has config => (is => 'ro', isa => 'App::Sqitch::Config', lazy => 1, default => sub {
App::Sqitch::Config->new
});
has editor => (is => 'ro', lazy => 1, default => sub {
- return $ENV{SQITCH_EDITOR} || $ENV{EDITOR} || (
- $^O eq 'MSWin32' ? 'notepad.exe' : 'vi'
- );
+ return $ENV{SQITCH_EDITOR} || $ENV{EDITOR}
+ || shift->config->get(key => 'core.editor')
+ || ($^O eq 'MSWin32' ? 'notepad.exe' : 'vi')
+ ;
});
sub go {
@@ -72,16 +93,15 @@ sub go {
my ($core_args, $cmd, $cmd_args) = $class->_split_args(@ARGV);
# 2. Parse core options.
- my $core_opts = $class->_parse_core_opts($core_args);
+ my $opts = $class->_parse_core_opts($core_args);
# 3. Load config.
my $config = App::Sqitch::Config->new;
# 4. Instantiate Sqitch.
- my $params = merge $core_opts, $config->get_section(section => 'core');
- $params->{_engine} = delete $params->{engine} if $params->{engine};
- my $sqitch = $class->new($params);
- $sqitch->{config} = $config;
+ $opts->{_engine} = delete $opts->{engine} if $opts->{engine};
+ $opts->{config} = $config;
+ my $sqitch = $class->new($opts);
# 5. Instantiate the command object.
my $command = App::Sqitch::Command->load({
@@ -47,6 +47,20 @@ sub write_config {
return $self;
}
+ my @comments;
+ # start with the engine.
+ my $engine = $sqitch->engine;
+ if ($engine) {
+ $config->set(
+ key => "core.engine",
+ value => $engine->name,
+ filename => $file,
+ );
+ } else {
+ push @comments => "\tengine = ";
+ }
+
+ # Add in the other stuff.
for my $name (qw(
plan_file
sql_dir
@@ -60,27 +74,36 @@ sub write_config {
my $attr = $meta->find_attribute_by_name($name)
or die "Cannot find App::Sqitch attribute $name";
my $val = $attr->get_value($sqitch);
+ my $def = $attr->default($sqitch);
+ my $var = $config->get(key => "core.$name");
+
no warnings 'uninitialized';
- $config->set(
- key => "core.$name",
- value => $val,
- filename => $file,
- ) if $val ne $attr->default($sqitch)
- && $val ne $config->get(key => "core.$name");
+ if ($val ne $def && $val ne $var) {
+ # It was specified on the command-line, so write it out.
+ $config->set(
+ key => "core.$name",
+ value => $val,
+ filename => $file,
+ );
+ } else {
+ $var //= $def // '';
+ push @comments => "\t$name = $var";
+ }
}
- if (my $engine = $sqitch->engine) {
- $config->set(
- key => "core.engine",
- value => $engine->name,
- filename => $file,
- );
+ # Emit the comments.
+ $config->add_comment(
+ filename => $file,
+ indented => 1,
+ comment => join "\n" => @comments,
+ ) if @comments;
+ if ($engine) {
# Write out the core.$engine section.
my $ekey = 'core.' . $engine->name;
my %config_vars = $engine->config_vars;
my $emeta = $engine->meta;
- my @comments;
+ my @ecomments;
while (my ($key, $type) = each %config_vars) {
# Was it passed as an option?
@@ -104,21 +127,23 @@ sub write_config {
# No value, but add it as a comment.
if (my $attr = $emeta->find_attribute_by_name($key)) {
# Add it as a comment, possibly with a default.
- my $def = $attr->default($engine);
- $def = '' unless defined $def;
- push @comments => "\t$key = $def";
+ my $def = $attr->default($engine)
+ // $config->get(key => "$ekey.$key")
+ // '';
+ push @ecomments => "\t$key = $def";
} else {
- # Add it as a comment, but we don't know of a default.
- push @comments => "\t$key = ";
+ # Add it as a comment, with the config, if possible.
+ my $val = $config->get(key => "$ekey.$key") // '';
+ push @ecomments => "\t$key = $val";
}
}
# Emit the comments.
$config->add_comment(
filename => $file,
indented => 1,
- comment => join "\n" => @comments,
- ) if @comments;
+ comment => join "\n" => @ecomments,
+ ) if @ecomments;
}
$self->info("Created $file");
View
@@ -2,7 +2,7 @@
use strict;
use warnings;
-use Test::More tests => 33;
+use Test::More tests => 32;
#use Test::More 'no_plan';
use Test::MockModule;
use Path::Class;
@@ -77,7 +77,6 @@ GO: {
is $sqitch->_engine, 'sqlite', 'Engine should be set by option';
# isa $sqitch->engine, 'App::Sqitch::Engine::sqlite',
# 'Engine object should be constructable';
- is $sqitch->db_name, 'widgetopolis', 'db_name should be set by config';
is $sqitch->extension, 'ddl', 'ddl should be set by config';
ok my $config = $sqitch->config, 'Get the Sqitch config';
is $config->get(key => 'core.pg.client'), '/usr/local/pgsql/bin/psql',
View
@@ -429,17 +429,19 @@ is_deeply \@emit, [[
"bundle.dest_dir=_build/sql
bundle.from=gamma
bundle.tags_only=true
-core.db_name=widgetopolis
core.engine=pg
core.extension=ddl
core.mysql.client=/opt/local/mysql/bin/mysql
+core.mysql.sqitch_prefix=meta
core.mysql.username=root
core.pg.client=/opt/local/pgsql/bin/psql
core.pg.db_name=widgets
core.pg.host=localhost
+core.pg.sqitch_schema=meta
core.pg.username=postgres
core.sql_dir=migrations
core.sqlite.client=/opt/local/bin/sqlite3
+core.sqlite.sqitch_prefix=meta
revert.count=2
revert.revision=1.1
revert.to=gamma
@@ -462,7 +464,6 @@ CONTEXT: {
"bundle.dest_dir=_build/sql
bundle.from=gamma
bundle.tags_only=true
-core.db_name=widgetopolis
core.engine=pg
core.extension=ddl
core.pg.client=/usr/local/pgsql/bin/psql
@@ -485,11 +486,14 @@ revert.to=gamma
ok $cmd->execute, 'List the user config';
is_deeply \@emit, [[
"core.mysql.client=/opt/local/mysql/bin/mysql
+core.mysql.sqitch_prefix=meta
core.mysql.username=root
core.pg.client=/opt/local/pgsql/bin/psql
core.pg.host=localhost
+core.pg.sqitch_schema=meta
core.pg.username=postgres
core.sqlite.client=/opt/local/bin/sqlite3
+core.sqlite.sqitch_prefix=meta
"
]], 'Should only have emitted the user config list';
@emit = ();
@@ -745,8 +749,7 @@ ok $cmd = App::Sqitch::Command::config->new({
action => 'get-regex',
}), 'Create system config get_regex command';
ok $cmd->execute('core\\..+'), 'Call get_regex on core\\..+';
-is_deeply \@emit, [[q{core.db_name=widgetopolis
-core.engine=funky
+is_deeply \@emit, [[q{core.engine=funky
core.extension=ddl
core.foo=[bar, baz]
core.pg.client=/usr/local/pgsql/bin/psql
View
@@ -48,7 +48,6 @@ SQITCH_CONFIG: {
chdir 't';
is_deeply $config->get_section(section => 'core'), {
- db_name => "widgetopolis",
engine => "pg",
extension => "ddl",
sql_dir => "migrations",
Oops, something went wrong.

0 comments on commit beeb12c

Please sign in to comment.