Changeset 339 for LATMOS-Accounts/bin/la-create
- Timestamp:
- 08/13/09 18:21:32 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
LATMOS-Accounts/bin/la-create
r132 r339 6 6 use Getopt::Long; 7 7 use Pod::Usage; 8 use File::Temp; 8 use 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 9 19 10 20 GetOptions( … … 12 22 'b|base=s' => \my $base, 13 23 'o|object=s' => \my $otype, 24 'f=s' => \my $inputfile, 25 'ro' => \my $with_ro, 26 'u|update' => \my $allow_update, 14 27 'help' => sub { pod2usage(0) }, 15 28 ) or pod2usage(); 16 29 17 30 $otype ||= 'user'; 31 32 =head1 OPTIONS 33 34 =over 4 35 36 =item -c|--config configfile 37 38 Use this cofngiuration file instead the default one 39 40 =item -b|--base basename 41 42 Query this specific base instead default. 43 44 =item -o|object object_type 45 46 Query should be performed on this objects. Default is to user 'User' object. 47 48 =item --ro 49 50 Include also read-only attributes as comment 51 52 =item -u|--update 53 54 If the object already exists, then updating it 55 56 =item -f FILE 57 58 Use this file to get attributes (- use stdin) 59 60 =back 61 62 =cut 18 63 19 64 my $LA = LATMOS::Accounts->new($config); … … 22 67 23 68 $labase->is_supported_object($otype) or die "$otype object unsupported\n"; 69 my $objname = $ARGV[0]; 24 70 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; 71 sub 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 } 41 85 } 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; 48 90 } 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; 72 92 } 73 93 } 94 95 if ($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.