Changeset 2423
- Timestamp:
- 06/26/20 01:20:59 (4 years ago)
- Location:
- trunk/LATMOS-Accounts
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LATMOS-Accounts/bin/la-cli
r2285 r2423 81 81 $Env->run(@ARGV); 82 82 } 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; 89 84 } 90 85 print "\n"; -
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli.pm
r2419 r2423 223 223 push(@objs, $obj); 224 224 } 225 $self->print("Selecting $otype " . join(', ', @ ids) . "\n");225 $self->print("Selecting $otype " . join(', ', @objs) . "\n"); 226 226 LATMOS::Accounts::Cli::Object->new( 227 227 Parent => $self, -
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli/Base.pm
r2422 r2423 249 249 } 250 250 251 =head3 collection 252 253 Manage 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 251 355 =head2 GLOBAL and OBJECTS FUNCTION 252 356 … … 525 629 return ( $op, $shellLine, shellwords($internalLine) ); 526 630 631 } 632 633 =head2 topCli 634 635 Entry point for cli 636 637 =cut 638 639 sub 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(); 527 649 } 528 650 -
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli/Context.pm
r2399 r2423 11 11 use Text::ParseWords; 12 12 use Getopt::Long; 13 use YAML; 13 14 use utf8; 14 15 use open qw( :std :utf8 ); … … 39 40 has TempOut => ( is => 'rw' ); 40 41 has Interractive => ( is => 'rw', isa => 'Bool', default => 1 ); 42 has Preferences => ( is => 'rw', default => sub { {} } ); 41 43 42 44 =head1 FUNCTIONS … … 59 61 my $OUT = \*STDOUT; 60 62 63 $term->ReadHistory( _historyFile() ); 61 64 62 65 return $class->$orig( Out => $OUT, Term => $term, @_ ); 63 66 64 67 }; 68 69 sub _historyFile { "$ENV{HOME}/.lacli_history" } 70 71 sub ReadHistory { 72 my ( $self ) = @_; 73 $self->Term->ReadHistory( _historyFile() ); 74 } 75 76 sub WriteHistory { 77 my ( $self ) = @_; 78 $self->Term->WriteHistory( _historyFile() ); 79 } 80 81 sub _preferenceFile { "$ENV{HOME}/.lacli_preference" } 82 83 sub ReadPreferences { 84 my ( $self ) = @_; 85 86 if (-f _preferenceFile() ) { 87 $self->Preferences( YAML::LoadFile( _preferenceFile() ) ); 88 } 89 } 90 91 sub WritePreferences { 92 my ( $self ) = @_; 93 94 YAML::DumpFile( _preferenceFile(), $self->Preferences() ); 95 } 65 96 66 97 =head2 print
Note: See TracChangeset
for help on using the changeset viewer.