[Bps-public-commit] r13926 - in Prophet/trunk: . lib/Prophet/CLI lib/Prophet/CLI/Command
sartak at bestpractical.com
sartak at bestpractical.com
Wed Jul 9 15:52:45 EDT 2008
Author: sartak
Date: Wed Jul 9 15:52:44 2008
New Revision: 13926
Added:
Prophet/trunk/lib/Prophet/CLI/
Prophet/trunk/lib/Prophet/CLI/Command/
Prophet/trunk/lib/Prophet/CLI/Command.pm
Prophet/trunk/lib/Prophet/CLI/Command/Create.pm
Prophet/trunk/lib/Prophet/CLI/Command/Delete.pm
Prophet/trunk/lib/Prophet/CLI/Command/Export.pm
Prophet/trunk/lib/Prophet/CLI/Command/Merge.pm
Prophet/trunk/lib/Prophet/CLI/Command/NotFound.pm
Prophet/trunk/lib/Prophet/CLI/Command/Pull.pm
Prophet/trunk/lib/Prophet/CLI/Command/Push.pm
Prophet/trunk/lib/Prophet/CLI/Command/Search.pm
Prophet/trunk/lib/Prophet/CLI/Command/Server.pm
Prophet/trunk/lib/Prophet/CLI/Command/Show.pm
Prophet/trunk/lib/Prophet/CLI/Command/Update.pm
Prophet/trunk/lib/Prophet/CLI/RecordCommand.pm
Modified:
Prophet/trunk/ (props changed)
Prophet/trunk/lib/Prophet/CLI.pm
Log:
r63981 at onn: sartak | 2008-07-09 15:52:16 -0400
Split up Prophet::CLI's inner packages into more files
Modified: Prophet/trunk/lib/Prophet/CLI.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/CLI.pm (original)
+++ Prophet/trunk/lib/Prophet/CLI.pm Wed Jul 9 15:52:44 2008
@@ -6,6 +6,7 @@
use Prophet::Record;
use Prophet::Collection;
use Prophet::Replica;
+use Prophet::CLI::Command;
use List::Util 'first';
@@ -232,445 +233,5 @@
__PACKAGE__->meta->make_immutable;
no Moose;
-package Prophet::CLI::RecordCommand;
-use Moose::Role;
-
-has type => (
- is => 'rw',
- isa => 'Str',
- required => 0
-);
-
-has uuid => (
- is => 'rw',
- isa => 'Str',
- required => 0
-);
-
-has record_class => (
- is => 'rw',
- isa => 'Prophet::Record',
-);
-
-
-sub _get_record_class {
- my $self = shift;
- my $args = { handle => $self->cli->app_handle->handle, type => $self->type };
- if (my $class = $self->record_class ) {
- Prophet::App->require_module($class);
- return $class->new( $args);
- } elsif ( $self->type ) {
- return $self->_type_to_record_class( $self->type )->new($args);
- } else { Carp::confess("I was asked to get a record object, but I have neither a type nor a record class")}
-
-}
-
-sub _load_record {
- my $self = shift;
- my $record = $self->_get_record_class;
- $record->load( uuid => $self->uuid )
- || $self->fatal_error("I couldn't find the record " . $self->uuid);
- return $record;
-}
-
-sub _type_to_record_class {
- my $self = shift;
- my $type = shift;
- my $try = $self->cli->app_class . "::Model::" . ucfirst( lc($type) );
- Prophet::App->require_module($try); # don't care about fails
- return $try if ( $try->isa('Prophet::Record') );
-
- $try = $self->cli->app_class . "::Record";
- Prophet::App->require_module($try); # don't care about fails
- return $try if ( $try->isa('Prophet::Record') );
- return 'Prophet::Record';
-}
-
-no Moose::Role;
-
-package Prophet::CLI::Command;
-use Moose;
-
-has cli => (
- is => 'rw',
- isa => 'Prophet::CLI',
- weak_ref => 1,
- handles => [qw/args set_arg arg has_arg delete_arg app_handle/],
-);
-
-sub fatal_error {
- my $self = shift;
- my $reason = shift;
- die $reason . "\n";
-
-}
-
-
-=head2 edit_text [text] -> text
-
-Filters the given text through the user's C<$EDITOR> using
-L<Proc::InvokeEditor>.
-
-=cut
-
-sub edit_text {
- my $self = shift;
- my $text = shift;
-
- require Proc::InvokeEditor;
- return scalar Proc::InvokeEditor->edit($text);
-}
-
-=head2 edit_hash hashref -> hashref
-
-Filters the hash through the user's C<$EDITOR> using L<Proc::InvokeEditor>.
-
-No validation is done on the input or output.
-
-=cut
-
-sub edit_hash {
- my $self = shift;
- my $hash = shift;
-
- my $input = join "\n", map { "$_: $hash->{$_}\n" } keys %$hash;
- my $output = $self->edit_text($input);
-
- my $filtered = {};
- while ($output =~ m{^(\S+?):\s*(.*)$}mg) {
- $filtered->{$1} = $2;
- }
-
- return $filtered;
-}
-
-=head2 edit_args [arg], defaults -> hashref
-
-Returns a hashref of the command arguments mixed in with any default arguments.
-If the "arg" argument is specified, (default "edit", use C<undef> if you only want default arguments), then L</edit_hash> is
-invoked on the argument list.
-
-=cut
-
-sub edit_args {
- my $self = shift;
- my $arg = shift || 'edit';
-
- my $edit_hash;
- if ($self->has_arg($arg)) {
- $self->delete_arg($arg);
- $edit_hash = 1;
- }
-
- my %args;
- if (@_ == 1) {
- %args = (%{ $self->args }, %{ $_[0] });
- }
- else {
- %args = (%{ $self->args }, @_);
- }
-
- if ($edit_hash) {
- return $self->edit_hash(\%args);
- }
-
- return \%args;
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
-package Prophet::CLI::Command::Create;
-use Moose;
-extends 'Prophet::CLI::Command';
-with 'Prophet::CLI::RecordCommand';
-has '+uuid' => ( required => 0);
-
-sub run {
- my $self = shift;
- my $record = $self->_get_record_class;
- my ($val, $msg) = $record->create( props => $self->edit_args );
- if (!$val) {
- warn $msg ."\n";
- }
- if (!$record->uuid) {
- warn "Failed to create " . $record->record_type . "\n";
- return;
- }
-
- print "Created " . $record->record_type . " " . $record->luid . " (".$record->uuid.")"."\n";
-
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
-package Prophet::CLI::Command::Search;
-use Moose;
-extends 'Prophet::CLI::Command';
-with 'Prophet::CLI::RecordCommand';
-has '+uuid' => ( required => 0);
-
-sub get_collection_object {
- my $self = shift;
-
- my $class = $self->_get_record_class->collection_class;
- Prophet::App->require_module($class);
- my $records = $class->new(
- handle => $self->app_handle->handle,
- type => $self->type
- );
-
- return $records;
-}
-
-sub get_search_callback {
- my $self = shift;
-
- if ( my $regex = $self->arg('regex') ) {
- return sub {
- my $item = shift;
- my $props = $item->get_props;
- map { return 1 if $props->{$_} =~ $regex } keys %$props;
- return 0;
- }
- } else {
- return sub {1}
- }
-}
-sub run {
- my $self = shift;
-
- my $records = $self->get_collection_object();
- my $search_cb = $self->get_search_callback();
- $records->matching($search_cb);
-
- for ( sort { $a->uuid cmp $b->uuid } $records->items ) {
- if ( $_->summary_props ) {
- print $_->format_summary . "\n";
- } else {
- # XXX OLD HACK TO MAKE TESTS PASS
- printf( "%s %s %s \n", $_->uuid, $_->prop('summary') || "(no summary)", $_->prop('status') || '(no status)' );
- }
- }
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
-package Prophet::CLI::Command::Update;
-use Moose;
-extends 'Prophet::CLI::Command';
-with 'Prophet::CLI::RecordCommand';
-
-sub edit_record {
- my $self = shift;
- my $record = shift;
-
- if ($self->has_arg('edit')) {
- my $props = $record->get_props;
- return $self->edit_hash($props);
- }
- else {
- return $self->args;
- }
-}
-
-sub run {
- my $self = shift;
-
- my $record = $self->_load_record;
- my $result = $record->set_props( props => $self->edit_record($record) );
- if ($result) {
- print $record->type . " " . $record->uuid . " updated.\n";
-
- } else {
- print "SOMETHING BAD HAPPENED "
- . $record->type . " "
- . $record->uuid
- . " not updated.\n";
-
- }
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
-package Prophet::CLI::Command::Delete;
-use Moose;
-extends 'Prophet::CLI::Command';
-with 'Prophet::CLI::RecordCommand';
-
-sub run {
- my $self = shift;
-
- my $record = $self->_load_record;
- if ( $record->delete ) {
- print $record->type . " " . $record->uuid . " deleted.\n";
- } else {
- print $record->type . " " . $record->uuid . "could not be deleted.\n";
- }
-
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
-package Prophet::CLI::Command::Show;
-use Moose;
-extends 'Prophet::CLI::Command';
-with 'Prophet::CLI::RecordCommand';
-
-
-sub run {
- my $self = shift;
-
- my $record = $self->_load_record;
- print "id: ".$record->luid." (" .$record->uuid.")\n";
- my $props = $record->get_props();
- for ( keys %$props ) {
- print $_. ": " . $props->{$_} . "\n";
- }
-
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
-package Prophet::CLI::Command::Merge;
-use Moose;
-extends 'Prophet::CLI::Command';
-
-sub run {
-
- my $self = shift;
-
- my $source = Prophet::Replica->new( { url => $self->arg('from') } );
- my $target = Prophet::Replica->new( { url => $self->arg('to') } );
-
- $target->import_resolutions_from_remote_replica( from => $source );
-
- $self->_do_merge( $source, $target );
-
- print "Merge complete.\n";
-}
-
-sub _do_merge {
- my ( $self, $source, $target ) = @_;
- if ( $target->uuid eq $source->uuid ) {
- $self->fatal_error(
- "You appear to be trying to merge two identical replicas. "
- . "Either you're trying to merge a replica to itself or "
- . "someone did a bad job cloning your database" );
- }
-
- my $prefer = $self->arg('prefer') || 'none';
-
- if ( !$target->can_write_changesets ) {
- $self->fatal_error( $target->url
- . " does not accept changesets. Perhaps it's unwritable or something"
- );
- }
-
- $target->import_changesets(
- from => $source,
- resdb => $self->app_handle->resdb_handle,
- $ENV{'PROPHET_RESOLVER'}
- ? ( resolver_class => 'Prophet::Resolver::' . $ENV{'PROPHET_RESOLVER'} )
- : ( ( $prefer eq 'to'
- ? ( resolver_class => 'Prophet::Resolver::AlwaysTarget' )
- : ()
- ),
- ( $prefer eq 'from'
- ? ( resolver_class => 'Prophet::Resolver::AlwaysSource' )
- : ()
- )
- )
- );
-
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
-package Prophet::CLI::Command::Push;
-use Moose;
-extends 'Prophet::CLI::Command::Merge';
-
-sub run {
- my $self = shift;
-
- my $source_me = $self->app_handle->handle;
- my $other = shift @ARGV;
- my $source_other = Prophet::Replica->new( { url => $other } );
- my $resdb = $source_me->import_resolutions_from_remote_replica(
- from => $source_other );
-
- $self->_do_merge( $source_me, $source_other );
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
-package Prophet::CLI::Command::Export;
-use Moose;
-extends 'Prophet::CLI::Command';
-
-sub run {
- my $self = shift;
-
- $self->app_handle->handle->export_to( path => $self->arg('path') );
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
-package Prophet::CLI::Command::Pull;
-use Moose;
-extends 'Prophet::CLI::Command::Merge';
-
-sub run {
-
- my $self = shift;
- my $other = shift @ARGV;
- my $source_other = Prophet::Replica->new( { url => $other } );
- $self->app_handle->handle->import_resolutions_from_remote_replica(
- from => $source_other );
-
- $self->_do_merge( $source_other, $self->app_handle->handle );
-
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
-package Prophet::CLI::Command::Server;
-use Moose;
-extends 'Prophet::CLI::Command';
-
-sub run {
-
- my $self = shift;
-
- require Prophet::Server::REST;
- my $server = Prophet::Server::REST->new( $self->arg('port') || 8080 );
- $server->prophet_handle( $self->app_handle->handle );
- $server->run;
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
-package Prophet::CLI::Command::NotFound;
-use Moose;
-extends 'Prophet::CLI::Command';
-
-sub run {
- my $self = shift;
- $self->fatal_error( "The command you ran could not be found. Perhaps running '$0 help' would help?" );
-}
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-
1;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,92 @@
+package Prophet::CLI::Command;
+use Moose;
+
+has cli => (
+ is => 'rw',
+ isa => 'Prophet::CLI',
+ weak_ref => 1,
+ handles => [qw/args set_arg arg has_arg delete_arg app_handle/],
+);
+
+sub fatal_error {
+ my $self = shift;
+ my $reason = shift;
+ die $reason . "\n";
+
+}
+
+
+=head2 edit_text [text] -> text
+
+Filters the given text through the user's C<$EDITOR> using
+L<Proc::InvokeEditor>.
+
+=cut
+
+sub edit_text {
+ my $self = shift;
+ my $text = shift;
+
+ require Proc::InvokeEditor;
+ return scalar Proc::InvokeEditor->edit($text);
+}
+
+=head2 edit_hash hashref -> hashref
+
+Filters the hash through the user's C<$EDITOR> using L<Proc::InvokeEditor>.
+
+No validation is done on the input or output.
+
+=cut
+
+sub edit_hash {
+ my $self = shift;
+ my $hash = shift;
+
+ my $input = join "\n", map { "$_: $hash->{$_}\n" } keys %$hash;
+ my $output = $self->edit_text($input);
+
+ my $filtered = {};
+ while ($output =~ m{^(\S+?):\s*(.*)$}mg) {
+ $filtered->{$1} = $2;
+ }
+
+ return $filtered;
+}
+
+=head2 edit_args [arg], defaults -> hashref
+
+Returns a hashref of the command arguments mixed in with any default arguments.
+If the "arg" argument is specified, (default "edit", use C<undef> if you only want default arguments), then L</edit_hash> is
+invoked on the argument list.
+
+=cut
+
+sub edit_args {
+ my $self = shift;
+ my $arg = shift || 'edit';
+
+ my $edit_hash;
+ if ($self->has_arg($arg)) {
+ $self->delete_arg($arg);
+ $edit_hash = 1;
+ }
+
+ my %args;
+ if (@_ == 1) {
+ %args = (%{ $self->args }, %{ $_[0] });
+ }
+ else {
+ %args = (%{ $self->args }, @_);
+ }
+
+ if ($edit_hash) {
+ return $self->edit_hash(\%args);
+ }
+
+ return \%args;
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command/Create.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Create.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,27 @@
+package Prophet::CLI::Command::Create;
+use Moose;
+extends 'Prophet::CLI::Command';
+with 'Prophet::CLI::RecordCommand';
+has '+uuid' => ( required => 0);
+
+sub run {
+ my $self = shift;
+ my $record = $self->_get_record_class;
+ my ($val, $msg) = $record->create( props => $self->edit_args );
+ if (!$val) {
+ warn $msg ."\n";
+ }
+ if (!$record->uuid) {
+ warn "Failed to create " . $record->record_type . "\n";
+ return;
+ }
+
+ print "Created " . $record->record_type . " " . $record->luid . " (".$record->uuid.")"."\n";
+
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command/Delete.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Delete.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,22 @@
+package Prophet::CLI::Command::Delete;
+use Moose;
+extends 'Prophet::CLI::Command';
+with 'Prophet::CLI::RecordCommand';
+
+sub run {
+ my $self = shift;
+
+ my $record = $self->_load_record;
+ if ( $record->delete ) {
+ print $record->type . " " . $record->uuid . " deleted.\n";
+ } else {
+ print $record->type . " " . $record->uuid . "could not be deleted.\n";
+ }
+
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command/Export.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Export.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,15 @@
+package Prophet::CLI::Command::Export;
+use Moose;
+extends 'Prophet::CLI::Command';
+
+sub run {
+ my $self = shift;
+
+ $self->app_handle->handle->export_to( path => $self->arg('path') );
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command/Merge.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Merge.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,58 @@
+package Prophet::CLI::Command::Merge;
+use Moose;
+extends 'Prophet::CLI::Command';
+
+sub run {
+
+ my $self = shift;
+
+ my $source = Prophet::Replica->new( { url => $self->arg('from') } );
+ my $target = Prophet::Replica->new( { url => $self->arg('to') } );
+
+ $target->import_resolutions_from_remote_replica( from => $source );
+
+ $self->_do_merge( $source, $target );
+
+ print "Merge complete.\n";
+}
+
+sub _do_merge {
+ my ( $self, $source, $target ) = @_;
+ if ( $target->uuid eq $source->uuid ) {
+ $self->fatal_error(
+ "You appear to be trying to merge two identical replicas. "
+ . "Either you're trying to merge a replica to itself or "
+ . "someone did a bad job cloning your database" );
+ }
+
+ my $prefer = $self->arg('prefer') || 'none';
+
+ if ( !$target->can_write_changesets ) {
+ $self->fatal_error( $target->url
+ . " does not accept changesets. Perhaps it's unwritable or something"
+ );
+ }
+
+ $target->import_changesets(
+ from => $source,
+ resdb => $self->app_handle->resdb_handle,
+ $ENV{'PROPHET_RESOLVER'}
+ ? ( resolver_class => 'Prophet::Resolver::' . $ENV{'PROPHET_RESOLVER'} )
+ : ( ( $prefer eq 'to'
+ ? ( resolver_class => 'Prophet::Resolver::AlwaysTarget' )
+ : ()
+ ),
+ ( $prefer eq 'from'
+ ? ( resolver_class => 'Prophet::Resolver::AlwaysSource' )
+ : ()
+ )
+ )
+ );
+
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command/NotFound.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/NotFound.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,14 @@
+package Prophet::CLI::Command::NotFound;
+use Moose;
+extends 'Prophet::CLI::Command';
+
+sub run {
+ my $self = shift;
+ $self->fatal_error( "The command you ran could not be found. Perhaps running '$0 help' would help?" );
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command/Pull.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Pull.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,21 @@
+package Prophet::CLI::Command::Pull;
+use Moose;
+extends 'Prophet::CLI::Command::Merge';
+
+sub run {
+
+ my $self = shift;
+ my $other = shift @ARGV;
+ my $source_other = Prophet::Replica->new( { url => $other } );
+ $self->app_handle->handle->import_resolutions_from_remote_replica(
+ from => $source_other );
+
+ $self->_do_merge( $source_other, $self->app_handle->handle );
+
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command/Push.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Push.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,21 @@
+package Prophet::CLI::Command::Push;
+use Moose;
+extends 'Prophet::CLI::Command::Merge';
+
+sub run {
+ my $self = shift;
+
+ my $source_me = $self->app_handle->handle;
+ my $other = shift @ARGV;
+ my $source_other = Prophet::Replica->new( { url => $other } );
+ my $resdb = $source_me->import_resolutions_from_remote_replica(
+ from => $source_other );
+
+ $self->_do_merge( $source_me, $source_other );
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command/Search.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Search.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,55 @@
+package Prophet::CLI::Command::Search;
+use Moose;
+extends 'Prophet::CLI::Command';
+with 'Prophet::CLI::RecordCommand';
+has '+uuid' => ( required => 0);
+
+sub get_collection_object {
+ my $self = shift;
+
+ my $class = $self->_get_record_class->collection_class;
+ Prophet::App->require_module($class);
+ my $records = $class->new(
+ handle => $self->app_handle->handle,
+ type => $self->type
+ );
+
+ return $records;
+}
+
+sub get_search_callback {
+ my $self = shift;
+
+ if ( my $regex = $self->arg('regex') ) {
+ return sub {
+ my $item = shift;
+ my $props = $item->get_props;
+ map { return 1 if $props->{$_} =~ $regex } keys %$props;
+ return 0;
+ }
+ } else {
+ return sub {1}
+ }
+}
+sub run {
+ my $self = shift;
+
+ my $records = $self->get_collection_object();
+ my $search_cb = $self->get_search_callback();
+ $records->matching($search_cb);
+
+ for ( sort { $a->uuid cmp $b->uuid } $records->items ) {
+ if ( $_->summary_props ) {
+ print $_->format_summary . "\n";
+ } else {
+ # XXX OLD HACK TO MAKE TESTS PASS
+ printf( "%s %s %s \n", $_->uuid, $_->prop('summary') || "(no summary)", $_->prop('status') || '(no status)' );
+ }
+ }
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command/Server.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Server.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,19 @@
+package Prophet::CLI::Command::Server;
+use Moose;
+extends 'Prophet::CLI::Command';
+
+sub run {
+
+ my $self = shift;
+
+ require Prophet::Server::REST;
+ my $server = Prophet::Server::REST->new( $self->arg('port') || 8080 );
+ $server->prophet_handle( $self->app_handle->handle );
+ $server->run;
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command/Show.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Show.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,23 @@
+package Prophet::CLI::Command::Show;
+use Moose;
+extends 'Prophet::CLI::Command';
+with 'Prophet::CLI::RecordCommand';
+
+
+sub run {
+ my $self = shift;
+
+ my $record = $self->_load_record;
+ print "id: ".$record->luid." (" .$record->uuid.")\n";
+ my $props = $record->get_props();
+ for ( keys %$props ) {
+ print $_. ": " . $props->{$_} . "\n";
+ }
+
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
Added: Prophet/trunk/lib/Prophet/CLI/Command/Update.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Update.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,40 @@
+package Prophet::CLI::Command::Update;
+use Moose;
+extends 'Prophet::CLI::Command';
+with 'Prophet::CLI::RecordCommand';
+
+sub edit_record {
+ my $self = shift;
+ my $record = shift;
+
+ if ($self->has_arg('edit')) {
+ my $props = $record->get_props;
+ return $self->edit_hash($props);
+ }
+ else {
+ return $self->args;
+ }
+}
+
+sub run {
+ my $self = shift;
+
+ my $record = $self->_load_record;
+ my $result = $record->set_props( props => $self->edit_record($record) );
+ if ($result) {
+ print $record->type . " " . $record->uuid . " updated.\n";
+
+ } else {
+ print "SOMETHING BAD HAPPENED "
+ . $record->type . " "
+ . $record->uuid
+ . " not updated.\n";
+
+ }
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
Added: Prophet/trunk/lib/Prophet/CLI/RecordCommand.pm
==============================================================================
--- (empty file)
+++ Prophet/trunk/lib/Prophet/CLI/RecordCommand.pm Wed Jul 9 15:52:44 2008
@@ -0,0 +1,58 @@
+package Prophet::CLI::RecordCommand;
+use Moose::Role;
+
+has type => (
+ is => 'rw',
+ isa => 'Str',
+ required => 0
+);
+
+has uuid => (
+ is => 'rw',
+ isa => 'Str',
+ required => 0
+);
+
+has record_class => (
+ is => 'rw',
+ isa => 'Prophet::Record',
+);
+
+
+sub _get_record_class {
+ my $self = shift;
+ my $args = { handle => $self->cli->app_handle->handle, type => $self->type };
+ if (my $class = $self->record_class ) {
+ Prophet::App->require_module($class);
+ return $class->new( $args);
+ } elsif ( $self->type ) {
+ return $self->_type_to_record_class( $self->type )->new($args);
+ } else { Carp::confess("I was asked to get a record object, but I have neither a type nor a record class")}
+
+}
+
+sub _load_record {
+ my $self = shift;
+ my $record = $self->_get_record_class;
+ $record->load( uuid => $self->uuid )
+ || $self->fatal_error("I couldn't find the record " . $self->uuid);
+ return $record;
+}
+
+sub _type_to_record_class {
+ my $self = shift;
+ my $type = shift;
+ my $try = $self->cli->app_class . "::Model::" . ucfirst( lc($type) );
+ Prophet::App->require_module($try); # don't care about fails
+ return $try if ( $try->isa('Prophet::Record') );
+
+ $try = $self->cli->app_class . "::Record";
+ Prophet::App->require_module($try); # don't care about fails
+ return $try if ( $try->isa('Prophet::Record') );
+ return 'Prophet::Record';
+}
+
+no Moose::Role;
+
+1;
+
More information about the Bps-public-commit
mailing list