Changeset 2386


Ignore:
Timestamp:
06/05/20 12:16:04 (4 years ago)
Author:
nanardon
Message:

Allow to use file as input in la-cli

File:
1 edited

Legend:

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

    r2374 r2386  
    3535=cut 
    3636 
     37sub _create_from_handle { 
     38    my ($self, $fh, $otype, $objname) = @_; 
     39 
     40    my $labase = $self->base; 
     41 
     42    my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh); 
     43    if ($objname && (my $obj = $labase->get_object($otype, $objname))) { 
     44         warn "Object $otype $objname already exists, aborting\n"; 
     45         return; 
     46    } else { 
     47        if ($objname) { 
     48            my $res = $labase->create_c_object($otype, $objname, %attr); 
     49            if($res) { 
     50                print "Changes applied\n"; 
     51                $labase->commit; 
     52                return 1; 
     53            } 
     54            return 0; 
     55        } else { 
     56            my $ochelper = $labase->ochelper($otype); 
     57 
     58            my $info = { 
     59                contents => { %attr }, 
     60            }; 
     61            if ($attr{name}) { 
     62                $info->{name}{content} = $attr{name}; 
     63            } 
     64 
     65            $ochelper->Automate($info) or do { 
     66                warn "Cannot create object:" . LATMOS::Accounts::Log::lastmessage() . "\n"; 
     67                return; 
     68            }; 
     69            return 1; 
     70        }  
     71    } 
     72} 
     73 
    3774sub BUILD { 
    3875    my ( $self ) = @_; 
     
    63100 
    64101                if ($filefmt){ 
    65                     open(my $hfmt, '<', $filefmt) or die "Cannot open $filefmt\n"; 
     102                    open(my $hfmt, '<', $filefmt) or do { 
     103                       warn "Cannot open $filefmt\n"; 
     104                       return; 
     105                    }; 
    66106                    $fmt ||= ''; # avoid undef warning 
    67107                    while (<$hfmt>) { 
     
    168208    $self->add_func('create', { 
    169209            code => sub { 
    170                 my ($self, $otype) = @_; 
    171                 my $helper = $self->base->ochelper($otype); 
    172                 my $info = undef; 
    173                 while (1) { 
    174                     my $status; 
    175                     ($status, $info) = $helper->step($info); 
    176  
    177                     if ($status ne 'NEEDINFO') { 
    178                         if ($status eq 'CREATED') { 
    179                             print $OUT "Object created\n"; 
    180                             $self->commit; 
    181                         } else { 
    182                             print $OUT "Nothing done\n"; 
    183                             $self->rollback; 
    184                         } 
     210                my $self = shift; 
     211                my ($otype, $objname) = $self->getoption( 
     212                    { 
     213                        'i'   => \my $interactive, 
     214                        'f=s' => \my $inputfile, 
     215                        'ro'  => \my $with_ro, 
     216                        'e'   => \my $empty_file, 
     217                    }, @_ 
     218                ); 
     219 
     220                if ( $interactive ) { 
     221                    my $helper = $self->base->ochelper($otype); 
     222                    my $info = undef; 
     223                    while (1) { 
     224                        my $status; 
     225                        ($status, $info) = $helper->step($info); 
     226 
     227                        if ($status ne 'NEEDINFO') { 
     228                            if ($status eq 'CREATED') { 
     229                                print $OUT "Object created\n"; 
     230                                $self->commit; 
     231                            } else { 
     232                                print $OUT "Nothing done\n"; 
     233                                $self->rollback; 
     234                            } 
     235                            return; 
     236                        } 
     237 
     238                        if ($info->{name}{ask}) { 
     239                            my $line = $self->Context->Term->readline("Name of the object ?"); 
     240                            $info->{name}{content} = $line; 
     241                        } 
     242                        foreach my $attr (@{$info->{ask} || []}) { 
     243                            $self->Context->Term->Attribs->{completion_function} = sub { 
     244                                $info->{contents}{$attr} 
     245                            }; 
     246                            my $line = $self->Context->Term->readline(sprintf('  %s %s? ', 
     247                                    $attr, 
     248                                    $info->{contents}{$attr} 
     249                                    ? '(' . $info->{contents}{$attr} . ') ' 
     250                                    : '' 
     251                                )); 
     252                            $info->{contents}{$attr} = $line if($line); 
     253                        } 
     254                    } 
     255                } elsif ($inputfile) { 
     256                    my $handle; 
     257                    open($handle, '<', $inputfile) or do { 
     258                        warn "Cannot open input file $@\n"; 
    185259                        return; 
    186                     } 
    187  
    188                     if ($info->{name}{ask}) { 
    189                         my $line = $self->Context->Term->readline("Name of the object ?"); 
    190                         $info->{name}{content} = $line; 
    191                     } 
    192                     foreach my $attr (@{$info->{ask} || []}) { 
    193                         $self->Context->Term->Attribs->{completion_function} = sub { 
    194                             $info->{contents}{$attr} 
    195                         }; 
    196                         my $line = $self->Context->Term->readline(sprintf('  %s %s? ', 
    197                                 $attr, 
    198                                 $info->{contents}{$attr} 
    199                                 ? '(' . $info->{contents}{$attr} . ') ' 
    200                                 : '' 
    201                             )); 
    202                         $info->{contents}{$attr} = $line if($line); 
    203                     } 
     260                    }; 
     261                    my $res = $self->_create_from_handle($handle, $otype, $objname); 
     262                    close($handle); 
     263                    $self->commit if($res); 
     264                    return($res); 
     265                } else { 
     266                    return LATMOS::Accounts::Utils::dump_read_temp_file( 
     267                        sub { 
     268                            my ($fh) = @_; 
     269                            $labase->text_empty_dump($fh, $otype, 
     270                                { 
     271                                    only_rw => !$with_ro, 
     272                                } 
     273                            ) unless($empty_file); 
     274                        }, 
     275                        sub { 
     276                            my ($fh) = @_; 
     277                            if (my $res = $self->_create_from_handle($fh, $otype, $objname)) { 
     278                                 $self->commit; 
     279                                 return $res; 
     280                             } else { 
     281                                 return; 
     282                             } 
     283                        } 
     284                    ); 
     285                } 
     286            }, 
     287            completion => sub { 
     288                my ($self, $carg, @args) = @_; 
     289                my @options = (); 
     290                push( @options, qw(-i -f)  ) unless ( grep { $_ =~ /^-[fi]$/ } @args ); 
     291                push( @options, qw(-e --ro)) unless ( grep { $_ eq '-f' } @args ); 
     292 
     293                if (($args[-1] || '') eq '-f') { 
     294                    my $attribs = $self->Context->Term->Attribs; 
     295                    return $self->Context->Term->completion_matches($carg, $attribs->{'filename_completion_function'}); 
     296                } else { 
     297                    return (@options, $self->base->list_supported_objects); 
    204298                } 
    205299            }, 
Note: See TracChangeset for help on using the changeset viewer.