Changeset 2423


Ignore:
Timestamp:
06/26/20 01:20:59 (4 years ago)
Author:
nanardon
Message:

la-cli: add collection command (save object list)

Location:
trunk/LATMOS-Accounts
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/bin/la-cli

    r2285 r2423  
    8181    $Env->run(@ARGV); 
    8282} else { 
    83      my $history = "$ENV{HOME}/.lacli_history"; 
    84      $Env->Context->Term->ReadHistory($history); 
    85      $Env->cli; 
    86      if (! $Env->Context->Term->WriteHistory($history)) { 
    87          warn "Cannot write history:  $!\n"; 
    88      } 
     83     $Env->topCli; 
    8984} 
    9085print "\n"; 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli.pm

    r2419 r2423  
    223223                    push(@objs, $obj); 
    224224                } 
    225                 $self->print("Selecting $otype " . join(', ', @ids) . "\n"); 
     225                $self->print("Selecting $otype " . join(', ', @objs) . "\n"); 
    226226                LATMOS::Accounts::Cli::Object->new( 
    227227                    Parent  => $self, 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli/Base.pm

    r2422 r2423  
    249249    } 
    250250 
     251=head3 collection 
     252 
     253Manage saved objects list: 
     254 
     255    collection list 
     256    collection save 
     257    collection load 
     258    collection delete 
     259 
     260=cut 
     261 
     262    $self->add_func( 'collection', 
     263        { 
     264            code => sub { 
     265                my ( $self, $subcommand, @args ) = @_; 
     266                $subcommand ||= ''; 
     267                $self->Context->Preferences->{Collections} ||= {}; 
     268                my $Collections = $self->Context->Preferences->{Collections}; 
     269                if ( $subcommand eq 'list' || !$subcommand ) { 
     270                    foreach my $c ( sort keys %{ $Collections } ) { 
     271                        my @objlist = @{ $Collections->{$c}{objs} || []}; 
     272                        $self->printf( 
     273                            "%s: %s, %s\n", 
     274                            $c, 
     275                            $Collections->{$c}{otype}, 
     276                            (@objlist >= 1 ? scalar(@objlist) . ' objs.' : $objlist[0]), 
     277                        ); 
     278                    } 
     279                } elsif ($subcommand eq 'load') { 
     280                    my $c = $args[0] or do { 
     281                        $self->print("No collection name given\n"); 
     282                        return; 
     283                    }; 
     284                    if (! $Collections->{$c} ) { 
     285                        $self->print("This collection does not exists\n"); 
     286                        return; 
     287                    } 
     288                    my $otype = $Collections->{ $c }{otype}; 
     289                    my @objs; 
     290                    foreach (@{ $Collections->{ $c }{ objs } || []}) { 
     291                        my $obj = $self->base->get_object($otype, $_) or do { 
     292                            $self->print("Cannot get $otype $_\n"); 
     293                            return; 
     294                        }; 
     295                        push(@objs, $obj); 
     296                    } 
     297                    if (@objs) { 
     298                        $self->print("Selecting $otype " . join(', ', map { $_->id } @objs) . "\n"); 
     299                        LATMOS::Accounts::Cli::Object->new( 
     300                            Parent  => $self, 
     301                            Context => $self->Context, 
     302                            otype   => $otype, 
     303                            objs    => \@objs, 
     304                        )->cli(); 
     305                    } else { 
     306                        $self->print("No objects to load\n"); 
     307                    } 
     308                } elsif ($subcommand eq 'clear') { 
     309                    $self->Context->Preferences->{Collections} = {}; 
     310                } elsif ($subcommand =~ m/^(delete|del)$/) { 
     311                    my ( $name ) = @args; 
     312                    delete( $Collections->{$name} ); 
     313                } elsif ($subcommand eq 'save' ) { 
     314                    my ( $name, $what ) = @args; 
     315                    $what ||= ''; 
     316 
     317                    if ( $what eq '@' ) { 
     318                        if (! $self->{_lastsearch} ) { 
     319                            $self->print("No previous search found, nothing saved\n"); 
     320                            return; 
     321                        } 
     322                        $Collections->{ $name } = { 
     323                            otype => $self->{_lastsearchtype}, 
     324                            objs =>  [ @{$self->{_lastsearch}} ], 
     325                        } 
     326                    } elsif ($self->can('otype')) { 
     327                        $Collections->{ $name } = { 
     328                            otype => $self->otype, 
     329                            objs =>  [ map { $_->id } @{ $self->objs || [] } ], 
     330                        }; 
     331                    } else { 
     332                        $self->print("No objects to save"); 
     333                    } 
     334                } 
     335            }, 
     336            completion => sub { 
     337                my ($self, undef, $command, $name, $what) = @_; 
     338                $command ||= ''; 
     339                my $Collections = $self->Context->Preferences->{Collections} || {}; 
     340                if ( ! $command ) { 
     341                    return qw(list load save delete clear); 
     342                } elsif ( $command =~ /^(load|delete|del)$/ ) { 
     343                    return sort keys %{ $Collections }; 
     344                } elsif ( $command eq 'save' ) { 
     345                    if ($name) { 
     346                        return qw( @ ); 
     347                    } 
     348                } else { 
     349                    return; 
     350                } 
     351            }, 
     352        } 
     353    ); 
     354 
    251355=head2 GLOBAL and OBJECTS FUNCTION 
    252356 
     
    525629    return ( $op, $shellLine, shellwords($internalLine) ); 
    526630 
     631} 
     632 
     633=head2 topCli 
     634 
     635Entry point for cli 
     636 
     637=cut 
     638 
     639sub topCli { 
     640    my ( $self ) = @_; 
     641 
     642    $self->Context->ReadHistory(); 
     643    $self->Context->ReadPreferences(); 
     644    $self->cli(); 
     645    if (! $self->Context->WriteHistory() ) { 
     646        warn "Cannot write history:  $!\n"; 
     647    } 
     648    $self->Context->WritePreferences(); 
    527649} 
    528650 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli/Context.pm

    r2399 r2423  
    1111use Text::ParseWords; 
    1212use Getopt::Long; 
     13use YAML; 
    1314use utf8; 
    1415use open qw( :std :utf8 ); 
     
    3940has TempOut => ( is => 'rw' ); 
    4041has Interractive => ( is => 'rw', isa => 'Bool', default => 1 ); 
     42has Preferences => ( is => 'rw', default => sub { {} } ); 
    4143 
    4244=head1 FUNCTIONS 
     
    5961    my $OUT = \*STDOUT; 
    6062 
     63    $term->ReadHistory( _historyFile() ); 
    6164 
    6265    return $class->$orig( Out => $OUT, Term => $term, @_ ); 
    6366 
    6467}; 
     68 
     69sub _historyFile { "$ENV{HOME}/.lacli_history" } 
     70 
     71sub ReadHistory { 
     72    my ( $self ) = @_; 
     73    $self->Term->ReadHistory( _historyFile() ); 
     74} 
     75 
     76sub WriteHistory { 
     77    my ( $self ) = @_; 
     78    $self->Term->WriteHistory( _historyFile() ); 
     79} 
     80 
     81sub _preferenceFile { "$ENV{HOME}/.lacli_preference" } 
     82 
     83sub ReadPreferences { 
     84    my ( $self ) = @_; 
     85 
     86    if (-f _preferenceFile() ) { 
     87        $self->Preferences( YAML::LoadFile( _preferenceFile() ) ); 
     88    } 
     89} 
     90 
     91sub WritePreferences { 
     92    my ( $self ) = @_; 
     93 
     94    YAML::DumpFile( _preferenceFile(), $self->Preferences() ); 
     95} 
    6596 
    6697=head2 print 
Note: See TracChangeset for help on using the changeset viewer.