Ignore:
Timestamp:
08/13/09 18:21:32 (15 years ago)
Author:
nanardon
Message:
  • update la-create to use same scheme than la-edit
File:
1 edited

Legend:

Unmodified
Added
Removed
  • LATMOS-Accounts/bin/la-create

    r132 r339  
    66use Getopt::Long; 
    77use Pod::Usage; 
    8 use File::Temp; 
     8use LATMOS::Accounts::Utils; 
     9 
     10=head1 NAME 
     11 
     12    la-create -- Tools to create object in LATMOS::Account system 
     13 
     14=head1 SYNOPSIS 
     15 
     16    la-create [options] obj_id 
     17 
     18=cut 
    919 
    1020GetOptions( 
     
    1222    'b|base=s'   => \my $base, 
    1323    'o|object=s' => \my $otype, 
     24    'f=s'        => \my $inputfile, 
     25    'ro'         => \my $with_ro, 
     26    'u|update'   => \my $allow_update, 
    1427    'help'       => sub { pod2usage(0) }, 
    1528) or pod2usage(); 
    1629 
    1730$otype ||= 'user'; 
     31 
     32=head1 OPTIONS 
     33 
     34=over 4 
     35 
     36=item -c|--config configfile 
     37 
     38Use this cofngiuration file instead the default one 
     39 
     40=item -b|--base basename 
     41 
     42Query this specific base instead default. 
     43 
     44=item -o|object object_type 
     45 
     46Query should be performed on this objects. Default is to user 'User' object. 
     47 
     48=item --ro 
     49 
     50Include also read-only attributes as comment 
     51 
     52=item -u|--update 
     53 
     54If the object already exists, then updating it 
     55 
     56=item -f FILE 
     57 
     58Use this file to get attributes (- use stdin) 
     59 
     60=back 
     61 
     62=cut 
    1863 
    1964my $LA = LATMOS::Accounts->new($config); 
     
    2267 
    2368$labase->is_supported_object($otype) or die "$otype object unsupported\n"; 
     69my $objname = $ARGV[0]; 
    2470 
    25 my @fields = $labase->list_canonical_fields($otype, 'w'); 
    26 my %attr; 
    27  
    28 while (1) { 
    29     my ($fh, $filename) = File::Temp::tempfile(); 
    30     printf $fh "## new object %s: %s\n", $otype, $ARGV[0]; 
    31     foreach (@fields) { 
    32         print $fh "$_:" . ($attr{$_} || '') . "\n"; 
    33     } 
    34     close($fh); 
    35     my @stat = stat($filename); 
    36     system('vi', $filename, '+ 1'); 
    37     if ((stat($filename))[9] == $stat[9]) { 
    38         warn "No change, aborting\n"; 
    39         unlink($filename); 
    40         last; 
     71sub input_from_handle { 
     72    my ($fh) = @_; 
     73    my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh); 
     74    if (my $obj = $labase->get_object($otype, $objname)) { 
     75        if ($allow_update) { 
     76            my $res = $obj->set_c_fields(%attr); 
     77            if ($res) { 
     78                print "Changes applied\n"; 
     79                $labase->commit; 
     80            } 
     81            return $res; 
     82        } else { 
     83            die "Object $otype $objname already exists, aborting\n"; 
     84        } 
    4185    } else { 
    42         open($fh, '<', $filename); 
    43         while (my $line = <$fh>) { 
    44             chomp($line); 
    45             $line =~ s/#.*//; 
    46             my ($fi, $val) = $line =~ /^(\w+)\s*:\s*(.*)/ or next; 
    47             $attr{$fi} = $val; 
     86        my $res = $labase->create_c_object($otype, $objname, %attr); 
     87        if($res) { 
     88            print "Changes applied\n"; 
     89            $labase->commit; 
    4890        } 
    49         close($fh); 
    50         unlink($filename); 
    51         $labase->create_c_object($otype, $ARGV[0], %attr) or do { 
    52             warn "Cannot create object: Redit ?\n"; 
    53             my $reply = <STDIN>; 
    54             $reply =~ /^[yY]/ or exit(1); 
    55             next; 
    56         }; 
    57         $labase->commit; 
    58  
    59         warn $LA->default_synchro; 
    60         if (!$base) { 
    61             if (my $sync = $LA->default_synchro) { 
    62                 for (1) { 
    63                     $sync->load_dest and do { 
    64                         warn  "Cannot load dest"; 
    65                         last; 
    66                     }; 
    67                     $sync->sync_object($otype, $ARGV[0]); 
    68                 } 
    69             } 
    70         } 
    71         last; 
     91        return $res; 
    7292    } 
    7393} 
     94 
     95if ($inputfile) { 
     96    my $handle; 
     97    if ($inputfile eq '-') { 
     98        $handle = *STDIN; 
     99    } else { 
     100        open($handle, '<', $inputfile) or die 
     101        "Cannot open input file $@\n"; 
     102    } 
     103    my $res = input_from_handle($handle); 
     104    close($handle); 
     105    exit(!$res); 
     106} else { 
     107    exit ! LATMOS::Accounts::Utils::dump_read_temp_file( 
     108        sub { 
     109            my ($fh) = @_; 
     110            $labase->text_empty_dump($fh, $otype, 
     111                { 
     112                    only_rw => !$with_ro, 
     113                } 
     114            ); 
     115        }, 
     116        sub { 
     117            my ($fh) = @_; 
     118            return input_from_handle($fh); 
     119        } 
     120    ); 
     121} 
Note: See TracChangeset for help on using the changeset viewer.