source: LATMOS-Accounts/bin/la-create @ 614

Last change on this file since 614 was 602, checked in by nanardon, 15 years ago
  • ask manager to sync base after changes
  • Property svn:executable set to *
File size: 2.7 KB
Line 
1#!/bin/env perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
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
19
20GetOptions(
21    'c|config=s' => \my $config,
22    'b|base=s'   => \my $base,
23    'o|object=s' => \my $otype,
24    'f=s'        => \my $inputfile,
25    'ro'         => \my $with_ro,
26    'u|update'   => \my $allow_update,
27    'help'       => sub { pod2usage(0) },
28) or pod2usage();
29
30$otype ||= 'user';
31
32=head1 OPTIONS
33
34=over 4
35
36=item -c|--config configfile
37
38Use this configuration file instead of the default one.
39
40=item -b|--base basename
41
42Query this specific base instead of the default one.
43
44=item -o|object object_type
45
46Query will be performed on this object. Default is the '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)
59If this option is not present, you'll have a file opened in your prefered editor to edit.
60
61=back
62
63=cut
64
65my $LA = LATMOS::Accounts->new($config, noacl => 1);
66my $labase = $base ? $LA->base($base) : $LA->default_base;
67$labase && $labase->load or die "Cannot load base";
68
69$labase->is_supported_object($otype) or die "$otype object unsupported\n";
70my $objname = $ARGV[0];
71
72sub input_from_handle {
73    my ($fh) = @_;
74    my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh);
75    if (my $obj = $labase->get_object($otype, $objname)) {
76        if ($allow_update) {
77            my $res = $obj->set_c_fields(%attr);
78            if ($res) {
79                print "Changes applied\n";
80                $labase->commit;
81                $LA->call_batch_sync;
82            }
83            return $res;
84        } else {
85            die "Object $otype $objname already exists, aborting\n";
86        }
87    } else {
88        my $res = $labase->create_c_object($otype, $objname, %attr);
89        if($res) {
90            print "Changes applied\n";
91            $labase->commit;
92            $LA->call_batch_sync;
93        }
94        return $res;
95    }
96}
97
98if ($inputfile) {
99    my $handle;
100    if ($inputfile eq '-') {
101        $handle = *STDIN;
102    } else {
103        open($handle, '<', $inputfile) or die
104        "Cannot open input file $@\n";
105    }
106    my $res = input_from_handle($handle);
107    close($handle);
108    exit(!$res);
109} else {
110    exit ! LATMOS::Accounts::Utils::dump_read_temp_file(
111        sub {
112            my ($fh) = @_;
113            $labase->text_empty_dump($fh, $otype,
114                {
115                    only_rw => !$with_ro,
116                }
117            );
118        },
119        sub {
120            my ($fh) = @_;
121            return input_from_handle($fh);
122        }
123    );
124}
Note: See TracBrowser for help on using the repository browser.