Changeset 847


Ignore:
Timestamp:
03/28/10 22:37:26 (14 years ago)
Author:
nanardon
Message:
  • more function in la-cli
File:
1 edited

Legend:

Unmodified
Added
Removed
  • LATMOS-Accounts/lib/LATMOS/Accounts/Cli.pm

    r846 r847  
    44use warnings; 
    55use LATMOS::Accounts::Log; 
     6use LATMOS::Accounts::Utils; 
    67use Term::ReadLine; 
    78use Text::ParseWords; 
     
    1718            help => 'ls object_type - list object of type object_type',  
    1819            completion => sub { 
    19                 if(!$_[3]) { 
    20                     return grep { /^\Q$_[1]\E/ } $_[0]->base->list_supported_objects 
     20                if(!$_[2]) { 
     21                    return $_[0]->base->list_supported_objects 
    2122                } else { () } 
    2223            }, 
     
    2930            }, 
    3031        }); 
     32    $env->add_func('search', { 
     33            help => 'search objecttype filter1 [filter2...] - search object according filter', 
     34            completion => sub { 
     35                if(!$_[2]) { 
     36                    return $_[0]->base->list_supported_objects 
     37                } else { return() } 
     38            }, 
     39            code => sub { 
     40                my ($env, @args) = @_; 
     41                if ($_[1]) { 
     42                    my @res = $env->base->search_objects(@args); 
     43                    print $OUT map { "$_\n" } @res; 
     44                    $env->{_lastsearch} = \@res; 
     45                    $env->{_lastsearchtype} = $args[0]; 
     46                } else { 
     47                    print $OUT "Object type missing"; 
     48                } 
     49            }, 
     50        }); 
    3151    $env->add_func('select', { 
    3252            help => 'select object_type - select objects to perform action on it', 
     
    3555                    return $_[0]->base->list_objects($_[2]); 
    3656                } else { 
    37                     return $_[0]->base->list_supported_objects; 
     57                    return '@', $_[0]->base->list_supported_objects; 
    3858                } 
    3959            }, 
     
    4161                my ($env, $otype, @ids) = @_; 
    4262                my @objs; 
     63                if ($otype eq '@') { 
     64                    if (@{$env->{_lastsearch}}) { 
     65                        $otype = $env->{_lastsearchtype}; 
     66                        @ids = @{$env->{_lastsearch}}; 
     67                    } else { 
     68                        print $OUT "No results store from previous search"; 
     69                        return; 
     70                    } 
     71                } 
    4372                if (!@ids) { 
    4473                    print $OUT 'not enough arguments'; 
     
    96125        }, 
    97126    }); 
     127    $objenv->add_func('unset', { 
     128        help => 'unset attribute - unset specified attribute', 
     129        code => sub { 
     130            my ($env, $attr) = @_; 
     131            $attr or do { 
     132                print $OUT "Attributes must be specified"; 
     133                return; 
     134            }; 
     135            foreach (@{$env->{_objects}}) { 
     136                defined $_->set_c_fields($attr => undef) or do { 
     137                    print $OUT "cannot unset attributes $attr for " . $_->id; 
     138                    return; 
     139                }; 
     140            } 
     141            $env->base->commit; 
     142            print $OUT "Changes applied"; 
     143        }, 
     144        completion => sub { 
     145            my ($env, $lastw, @args) = @_; 
     146            if (!$args[0]) { 
     147                return $env->base->list_canonical_fields($env->{_otype}, 'w') 
     148            } 
     149        }, 
     150    }); 
    98151    $objenv->add_func('set', { 
    99152        help => 'set attribute value - set an attributes to single value "value"', 
    100153        code => sub { 
    101             my ($env, $attr, $value) = @_; 
     154            my ($env, $attr, @value) = @_; 
     155            @value or do { 
     156                print $OUT "attribute and value must be specified"; 
     157                return; 
     158            }; 
    102159            foreach (@{$env->{_objects}}) { 
    103                 defined $_->set_c_fields($attr => $value) or do { 
     160                defined $_->set_c_fields($attr => @value <= 1 ? $value[0] : 
     161                    \@value) or do { 
    104162                    $_->base->rollback; 
    105                     print $OUT "Cannot set $attr to $value for " . $_->id; 
     163                    printf $OUT "Cannot set $attr to %s for %s", join(', ', 
     164                        @value), $_->id; 
    106165                    return; 
    107166                }; 
     
    115174                return $env->base->list_canonical_fields($env->{_otype}, 'w') 
    116175            } else { 
     176                if ($env->base->obj_attr_allowed_values($env->{_otype}, $args[0])) { 
     177                    return $env->base->obj_attr_allowed_values($env->{_otype}, $args[0]) 
     178                } 
    117179                for ($args[0]) { 
    118180                    /^manager|managedBy$/ and return 
     
    120182                    /^department$/ and return 
    121183                        $env->base->search_objects('group', 'sutype=dpmt'); 
     184                    /^contratType$/ and return 
     185                        $env->base->search_objects('group', 'sutype=contrattype'); 
    122186                    if (@{$env->{_objects}} == 1) { 
    123187                        return $env->{_objects}[0]->get_attributes($args[0]); 
     
    127191        }, 
    128192    }); 
     193    $objenv->add_func('list', { 
     194        help => 'list current selected objects', 
     195        code => sub { 
     196            printf $OUT "%s: %s", $_[0]->{_otype}, join(', ', map { $_->id } 
     197            @{$_[0]->{_objects}}); 
     198        } 
     199    }); 
     200    $objenv->add_func('edit', { 
     201            help => 'edit [object] - edit selected object using vi', 
     202            completion => sub { 
     203                return map { $_->id } @{$_[0]->{_objects}} 
     204            }, 
     205            code => sub { 
     206                my ($env, $id) = @_; 
     207                my $obj; 
     208                if ($id) { 
     209                    $obj = grep { $_->id = $id } @{$env->{_objects}} or do { 
     210                        print $OUT "$id is not part of selected objects"; 
     211                        return; 
     212                    }; 
     213                } elsif (@{$env->{_objects}} == 1) { 
     214                    $obj = $env->{_objects}[0] 
     215                } else { 
     216                    print $OUT "multiple objects selected but can edit only one," 
     217                    . "please specify which one"; 
     218                    return; 
     219                } 
     220                my $res = LATMOS::Accounts::Utils::dump_read_temp_file( 
     221                    sub { 
     222                        my ($fh) = @_; 
     223                        $obj->text_dump($fh, 
     224                            { 
     225                                empty_attr => 1, 
     226                                only_rw => 1, 
     227                            } 
     228                        ); 
     229                    }, 
     230                    sub { 
     231                        my ($fh) = @_; 
     232                        my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh); 
     233                        my $res = $obj->set_c_fields(%attr); 
     234                        if ($res) { 
     235                            print $OUT "Changes applied"; 
     236                            $env->base->commit; 
     237                        } 
     238                        else { print $OUT "Error applying changes" } 
     239                        return $res ? 1 : 0; 
     240                    } 
     241                ); 
     242            }, 
     243        }); 
     244 
    129245    if (lc($otype) eq 'user') { 
    130246        $objenv->add_func('group', { 
     
    200316} 
    201317 
    202  
    203318sub new { 
    204319    my ($class, $env, $labase) = @_; 
     
    207322    $env->add_func('quit', { help => 'quit - exit the tool', 
    208323            code => sub { print "\n"; exit(0) }, }); 
     324    $env->add_func('exit', { help => "exit current mode", 
     325            code => sub { return "EXIT" }, }); 
    209326    $env; 
    210327} 
     
    219336        }; 
    220337        defined (my $line = $term->readline($self->prompt)) or return; 
    221         $self->run(shellwords($line)); 
     338        my $res = $self->run(shellwords($line)); 
    222339        $self->base->rollback; 
     340        if ($res && $res eq 'EXIT') { return } 
    223341    } 
    224342} 
Note: See TracChangeset for help on using the changeset viewer.