Changeset 2218 for trunk


Ignore:
Timestamp:
02/22/19 02:06:49 (5 years ago)
Author:
nanardon
Message:

Add '!' command, merge la-sql-log in la-cli

'!' command run a shell or execute command line by shell

Location:
trunk/LATMOS-Accounts
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/bin/la-sql-log

    r1311 r2218  
    66use Getopt::Long; 
    77use Pod::Usage; 
     8use LATMOS::Accounts::Cli::Context; 
     9use LATMOS::Accounts::Cli; 
    810 
    911=head1 NAME 
     
    1719=cut 
    1820 
     21Getopt::Long::Configure("pass_through"); 
    1922GetOptions( 
    2023    'c|config=s' => \my $config, 
    2124    'b|base=s'   => \my $base, 
    22     'o|object=s' => \my $otype, 
    2325    'help'       => sub { pod2usage(0) }, 
    2426) or pod2usage(); 
    25  
    26 $otype ||= 'user'; 
    2727 
    2828=head1 OPTIONS 
     
    5151$labase->wexported(1); 
    5252 
    53 my @logs = $ARGV[0]  
    54     ? $labase->getobjectlogs($otype, $ARGV[0]) 
    55     : $labase->getlogs(); 
     53my $Env = LATMOS::Accounts::Cli->new( 
     54    Context => LATMOS::Accounts::Cli::Context->new( 
     55        base => $labase, 
     56    ), 
     57); 
    5658 
    57 foreach(@logs) { 
    58     printf("%s (%d), %s: %s/%s (%d) %s\n", 
    59         $_->{logdate}, 
    60         $_->{irev} || -1, 
    61         $_->{username}, 
    62         $_->{otype}, 
    63         $_->{name}, 
    64         $_->{ikey}, 
    65         $_->{message}, 
    66     ); 
     59if (@ARGV) { 
     60    $Env->run('log', @ARGV); 
     61} else { 
     62    $Env->run('log'); 
    6763} 
     64 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm

    r2210 r2218  
    11191119} 
    11201120 
    1121 =head2 getobjectlogs($otype, $name) 
     1121=head2 getobjectlogs($otype, @names) 
    11221122 
    11231123Return logs for object type C<$otype> having C<$name>. 
     
    11261126 
    11271127sub getobjectlogs { 
    1128     my ($self, $otype, $name) = @_; 
     1128    my ($self, $otype, @names) = @_; 
    11291129 
    11301130    my $sth = $self->db->prepare(q{ 
     
    11331133        group by ikey 
    11341134    }); 
    1135     $sth->execute($otype, $name); 
    11361135    my @ids; 
    1137     while (my $res = $sth->fetchrow_hashref) { 
    1138         push(@ids, $res->{ikey}); 
     1136    foreach my $name (@names) { 
     1137        $sth->execute($otype, $name); 
     1138        while (my $res = $sth->fetchrow_hashref) { 
     1139            push(@ids, $res->{ikey}); 
     1140        } 
    11391141    } 
    11401142    @ids or return; 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli/Base.pm

    r2217 r2218  
    7979        ); 
    8080    } 
    81     if ($self->base->can('CreateAlias')) { 
    82         $self->add_func( 
    83             'newalias', { 
    84                 help => 'Create an alias object', 
    85                 code => sub { 
    86                     my ($self, $otype, $name, $for) = @_; 
    87                     if ($self->base->CreateAlias($otype, $name, $for)) { 
    88                         print $OUT "Alias $otype/$name Created\n"; 
    89                         $self->commit; 
    90                     } 
    91                 }, 
    92                 completion => sub { 
    93                     if ($_[3]) { 
    94                         return $_[0]->base->list_objects($_[2]); 
    95                     } elsif (!$_[2]) { 
    96                         return $_[0]->base->list_supported_objects; 
    97                     } else { 
    98                         return; 
    99                     } 
    100                 } 
    101             }, 
    102         ); 
    103         $self->add_func( 
    104             'rmalias', { 
    105                 help => 'Remove an alias object', 
    106                 code => sub { 
    107                     my ($self, $otype, $name) = @_; 
    108                     if ($self->base->RemoveAlias($otype, $name)) { 
    109                         print $OUT "Alias $otype/$name Removed\n"; 
    110                         $self->commit; 
    111                     } 
    112                 }, 
    113                 completion => sub { 
    114                     if (!$_[2]) { 
    115                         return $_[0]->base->list_supported_objects; 
    116                     } else { 
    117                         return $_[0]->base->search_objects($_[2], 'oalias=*'); 
    118                     } 
    119                 } 
    120             }, 
    121         ); 
    122         $self->add_func( 
    123             'updalias', { 
    124                 help => 'Update an alias object', 
    125                 code => sub { 
    126                     my ($self, $otype, $name, $for) = @_; 
    127                     my $obj = $self->base->GetAlias($otype, $name) or do { 
    128                         print $OUT "No alias $otype/$name found"; 
    129                         return; 
    130                     }; 
    131                     if ($obj->set_c_fields(oalias => $for)) { 
    132                         print $OUT "Alias $otype/$name Updated\n"; 
    133                         $self->commit; 
    134                     } 
    135                 }, 
    136                 completion => sub { 
    137                     if ($_[3]) { 
    138                         return $_[0]->base->list_objects($_[2]); 
    139                     } elsif($_[2]) { 
    140                         return $_[0]->base->search_objects($_[2], 'oalias=*'); 
    141                     } else { 
    142                         return $_[0]->base->list_supported_objects; 
    143                     } 
    144                 } 
    145             }, 
    146         ); 
    147     } 
    14881    $self->add_func('quit', { help => 'quit - exit the tool', 
    14982            code => sub { print "\n"; exit(0) }, }); 
    15083    $self->add_func('exit', { help => "exit current mode", 
    15184            code => sub { return "EXIT" }, }); 
     85    $self->add_func('!', { 
     86            code => sub { 
     87                my ($env, $name, @args) = @_; 
     88                if ($name) { 
     89                    system('/bin/bash', '-ic', $env->Context->{_line}); 
     90                } else { 
     91                    system('/bin/bash', '-i'); 
     92                } 
     93            }, 
     94    } ); 
    15295 
    15396 
     
    222165=head2 log 
    223166 
    224     log [[-o otype ] object] 
     167    log [[-o otype ] object [object [...]]] 
    225168 
    226169Show global log or log for the object given in arguments 
     
    238181            $otype ||= 'user'; 
    239182 
     183            if (!@args && $env->Context->{objs}) { 
     184                @args = map { $_->id } @{ $env->Context->{objs} }; 
     185                $otype = $env->Context->{objs}[0]->type; 
     186            } 
     187 
    240188            my @logs = @args 
    241                 ? $self->base->getobjectlogs($otype, $args[0]) 
     189                ? $self->base->getobjectlogs($otype, @args) 
    242190                : $self->base->getlogs(); 
    243191 
     
    255203            } 
    256204        }, 
    257     } ); 
    258  
     205    } ) if ($self->base->can('getobjectlogs')); 
     206 
     207    if ($self->base->can('CreateAlias')) { 
     208        $self->add_func( 
     209            'newalias', { 
     210                help => 'Create an alias object', 
     211                code => sub { 
     212                    my ($self, $otype, $name, $for) = @_; 
     213                    if ($self->base->CreateAlias($otype, $name, $for)) { 
     214                        print $OUT "Alias $otype/$name Created\n"; 
     215                        $self->commit; 
     216                    } 
     217                }, 
     218                completion => sub { 
     219                    if ($_[3]) { 
     220                        return $_[0]->base->list_objects($_[2]); 
     221                    } elsif (!$_[2]) { 
     222                        return $_[0]->base->list_supported_objects; 
     223                    } else { 
     224                        return; 
     225                    } 
     226                } 
     227            }, 
     228        ); 
     229        $self->add_func( 
     230            'rmalias', { 
     231                help => 'Remove an alias object', 
     232                code => sub { 
     233                    my ($self, $otype, $name) = @_; 
     234                    if ($self->base->RemoveAlias($otype, $name)) { 
     235                        print $OUT "Alias $otype/$name Removed\n"; 
     236                        $self->commit; 
     237                    } 
     238                }, 
     239                completion => sub { 
     240                    if (!$_[2]) { 
     241                        return $_[0]->base->list_supported_objects; 
     242                    } else { 
     243                        return $_[0]->base->search_objects($_[2], 'oalias=*'); 
     244                    } 
     245                } 
     246            }, 
     247        ); 
     248        $self->add_func( 
     249            'updalias', { 
     250                help => 'Update an alias object', 
     251                code => sub { 
     252                    my ($self, $otype, $name, $for) = @_; 
     253                    my $obj = $self->base->GetAlias($otype, $name) or do { 
     254                        print $OUT "No alias $otype/$name found"; 
     255                        return; 
     256                    }; 
     257                    if ($obj->set_c_fields(oalias => $for)) { 
     258                        print $OUT "Alias $otype/$name Updated\n"; 
     259                        $self->commit; 
     260                    } 
     261                }, 
     262                completion => sub { 
     263                    if ($_[3]) { 
     264                        return $_[0]->base->list_objects($_[2]); 
     265                    } elsif($_[2]) { 
     266                        return $_[0]->base->search_objects($_[2], 'oalias=*'); 
     267                    } else { 
     268                        return $_[0]->base->list_supported_objects; 
     269                    } 
     270                } 
     271            }, 
     272        ); 
     273    } 
    259274} 
    260275 
     
    298313            return; 
    299314        }; 
     315        $_[0]->Context->{_line} = $line; 
    300316        $term->addhistory($line); 
    301317        my $res = $self->run(shellwords($line)); 
     
    390406    my ($self, $lastw, $name, @args) = @_; 
    391407    if (!$name) { 
     408        $lastw ||= ''; # avoid undef warning 
    392409        if ($lastw =~ m!^(\.\./*)(.*)$!) { 
    393410            if ($self->Parent) { 
    394                 return map { "$1/$_" } $self->Parent->complete($2, $name, @args); 
     411                my $dot = $1; 
     412                $dot .= '/' unless($dot =~ m!/$!); 
     413                return map { "$dot$_" } $self->Parent->complete($2, $name, @args); 
    395414            } else { 
    396415                return (); 
Note: See TracChangeset for help on using the changeset viewer.