Changeset 847
- Timestamp:
- 03/28/10 22:37:26 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
LATMOS-Accounts/lib/LATMOS/Accounts/Cli.pm
r846 r847 4 4 use warnings; 5 5 use LATMOS::Accounts::Log; 6 use LATMOS::Accounts::Utils; 6 7 use Term::ReadLine; 7 8 use Text::ParseWords; … … 17 18 help => 'ls object_type - list object of type object_type', 18 19 completion => sub { 19 if(!$_[ 3]) {20 return grep { /^\Q$_[1]\E/ }$_[0]->base->list_supported_objects20 if(!$_[2]) { 21 return $_[0]->base->list_supported_objects 21 22 } else { () } 22 23 }, … … 29 30 }, 30 31 }); 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 }); 31 51 $env->add_func('select', { 32 52 help => 'select object_type - select objects to perform action on it', … … 35 55 return $_[0]->base->list_objects($_[2]); 36 56 } else { 37 return $_[0]->base->list_supported_objects;57 return '@', $_[0]->base->list_supported_objects; 38 58 } 39 59 }, … … 41 61 my ($env, $otype, @ids) = @_; 42 62 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 } 43 72 if (!@ids) { 44 73 print $OUT 'not enough arguments'; … … 96 125 }, 97 126 }); 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 }); 98 151 $objenv->add_func('set', { 99 152 help => 'set attribute value - set an attributes to single value "value"', 100 153 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 }; 102 159 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 { 104 162 $_->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; 106 165 return; 107 166 }; … … 115 174 return $env->base->list_canonical_fields($env->{_otype}, 'w') 116 175 } 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 } 117 179 for ($args[0]) { 118 180 /^manager|managedBy$/ and return … … 120 182 /^department$/ and return 121 183 $env->base->search_objects('group', 'sutype=dpmt'); 184 /^contratType$/ and return 185 $env->base->search_objects('group', 'sutype=contrattype'); 122 186 if (@{$env->{_objects}} == 1) { 123 187 return $env->{_objects}[0]->get_attributes($args[0]); … … 127 191 }, 128 192 }); 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 129 245 if (lc($otype) eq 'user') { 130 246 $objenv->add_func('group', { … … 200 316 } 201 317 202 203 318 sub new { 204 319 my ($class, $env, $labase) = @_; … … 207 322 $env->add_func('quit', { help => 'quit - exit the tool', 208 323 code => sub { print "\n"; exit(0) }, }); 324 $env->add_func('exit', { help => "exit current mode", 325 code => sub { return "EXIT" }, }); 209 326 $env; 210 327 } … … 219 336 }; 220 337 defined (my $line = $term->readline($self->prompt)) or return; 221 $self->run(shellwords($line));338 my $res = $self->run(shellwords($line)); 222 339 $self->base->rollback; 340 if ($res && $res eq 'EXIT') { return } 223 341 } 224 342 }
Note: See TracChangeset
for help on using the changeset viewer.