[Bps-public-commit] r14278 - in Prophet/trunk/lib/Prophet: CLI/Command

jesse at bestpractical.com jesse at bestpractical.com
Fri Jul 18 15:12:30 EDT 2008


Author: jesse
Date: Fri Jul 18 15:12:25 2008
New Revision: 14278

Added:
   Prophet/trunk/lib/Prophet/CLI/Command/Config.pm
Modified:
   Prophet/trunk/lib/Prophet/App.pm
   Prophet/trunk/lib/Prophet/Config.pm

Log:
* Getting 'config' in shape to use

Modified: Prophet/trunk/lib/Prophet/App.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/App.pm	(original)
+++ Prophet/trunk/lib/Prophet/App.pm	Fri Jul 18 15:12:25 2008
@@ -28,13 +28,13 @@
     },
 );
 
-has _config => (
+has config => (
     is      => 'rw',
     isa     => 'Prophet::Config',
-    lazy    => 1,
     default => sub {
+        my $self = shift;
         Prophet::Config->require;
-        return Prophet::Config->new;
+        return Prophet::Config->new(app_handle => $self);
     },
 );
 
@@ -83,25 +83,10 @@
 
 =head2 config
 
-If called with no arguments, returns the L<Prophet::Config> instance.
-
-If called with one arguments, returns the value of that config setting.
-
-If called with two arguments, sets the value of that config setting.
+Returns the L<Prophet::Config> instance for the running application
 
 =cut
 
-sub config {
-    my $self = shift;
-
-    return $self->_config if @_ == 0;
-
-    my $key = shift;
-    return $self->_config->get($key) if @_ == 0;
-
-    my $value = shift;
-    return $self->_config->set($key => $value);
-}
 
 __PACKAGE__->meta->make_immutable;
 no Moose;

Added: Prophet/trunk/lib/Prophet/CLI/Command/Config.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Config.pm	Fri Jul 18 15:12:25 2008
@@ -0,0 +1,33 @@
+package Prophet::CLI::Command::Config;
+use Moose;
+extends 'Prophet::CLI::Command';
+
+sub run {
+
+    my $self = shift;
+
+    my $config = $self->app_handle->config;
+   
+    print "Configuration:\n\n";
+    my @files =@{$config->config_files};
+    if (!scalar @files) {
+        print "No configuration files found. ".
+            " Either create a file called 'prophetrc' inside of ". $self->app_handle->handle->fs_root ." or set the PROPHET_APP_CONFIG environement variable.\n\n";
+        return;
+    }
+    for my $file (@files) {
+        print "Config files:\n\n";
+            print "$file\n";    
+    }
+    print "\nYour configuration:\n\n";
+    for my $item ($config->list) {
+        print $item ." = ".$config->get($item)."\n";
+    }
+
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+

Modified: Prophet/trunk/lib/Prophet/Config.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Config.pm	(original)
+++ Prophet/trunk/lib/Prophet/Config.pm	Fri Jul 18 15:12:25 2008
@@ -3,53 +3,67 @@
 use MooseX::AttributeHelpers;
 use Path::Class;
 
+
+has app_handle => (
+    is => 'ro',
+    weak_ref => 1,
+    isa => 'Prophet::App',
+    required => 0
+);
+has config_files => ( 
+    is => 'rw',
+    isa => 'ArrayRef' ,
+    default =>sub  {[]}
+);
+
 has config => (
     metaclass => 'Collection::Hash',
     is        => 'rw',
     isa       => 'HashRef',
-    lazy      => 1,
-    default   => sub { shift->load_config_files },
+    lazy      => 0,
+    default   => sub { shift->load_from_files },
     provides  => {
         get   => 'get',
         set   => 'set',
+        keys  => 'list'
     },
 );
 
-sub prophet_config_file { dir($ENV{HOME}, ".prophetrc") }
-sub app_config_file { dir($ENV{PROPHET_REPO}, "prophetrc") }
+#sub prophet_config_file { dir($ENV{HOME}, ".prophetrc") }
+sub app_config_file { 
+    my $self = shift;
+    $ENV{'PROPHET_APP_CONFIG'} || file( $self->app_handle->handle->fs_root => "prophetrc" ) 
 
-my $singleton;
-around new => sub {
-    return $singleton if $singleton;
-    my $orig = shift;
-    return $singleton = $orig->(@_);
-};
+}
+
+#my $singleton;
+#around new => sub { return $singleton if $singleton; my $orig = shift; return $singleton = $orig->(@_); };
 
-sub load_config_files {
+sub load_from_files {
     my $self = shift;
     my @config = @_;
-    @config = grep { -f $_ } $self->prophet_config_file, $self->app_config_file
-        if !@config;
+    @config = grep { -f $_ } $self->app_config_file if !@config;
+    #@config = grep { -f $_ } $self->prophet_config_file, $self->app_config_file if !@config;
 
     my $config = {};
 
     for my $file (@config) {
-        $self->load_config_file($file, $config);
+        $self->load_from_file(file($file), $config);
+        push @{$self->config_files}, $file;
     }
 
     return $config;
 }
 
-sub load_config_file {
+sub load_from_file {
     my $self   = shift;
     my $file   = shift;
     my $config = shift || {};
 
     for my $line ($file->slurp) {
-        s/\#.*//; # strip comments
-        if ($line =~ /^([^:]+):\s*(.*)$/) {
+        $line =~ s/\#.*$//; # strip comments
+        next unless ($line =~ /^([^:]+?)\s*=\s*(.*)$/);
             $config->{$1} = $2;
-        }
     }
 }
 
@@ -93,12 +107,12 @@
 The file which controls configuration for this application.
 C<$PROPHET_REPO/prophetrc>.
 
-=head2 load_config_files [files]
+=head2 load_from_files [files]
 
 Loads the given config files. If no files are passed in, it will use the
 default of L</prophet_config_file> and L</app_config_file>.
 
-=head2 load_config_file file
+=head2 load_from_file file
 
 Loads the given config file.
 
@@ -110,5 +124,9 @@
 
 Sets a specific config setting.
 
+=head2 list
+
+List all configuration options
+
 =cut
 



More information about the Bps-public-commit mailing list