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

Last change on this file since 655 was 655, checked in by vivat, 14 years ago

Ajout d'un message d'erreur lorsqu'un parametre obligatoire n'est pas fourni

  • Property svn:executable set to *
File size: 2.8 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
65if (!$ARGV[0]) {print "You must specify 'obj_id', aborting\n"; pod2usage(); }
66
67my $LA = LATMOS::Accounts->new($config, noacl => 1);
68my $labase = $base ? $LA->base($base) : $LA->default_base;
69$labase && $labase->load or die "Cannot load base";
70
71$labase->is_supported_object($otype) or die "$otype object unsupported\n";
72my $objname = $ARGV[0];
73
74sub input_from_handle {
75    my ($fh) = @_;
76    my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh);
77    if (my $obj = $labase->get_object($otype, $objname)) {
78        if ($allow_update) {
79            my $res = $obj->set_c_fields(%attr);
80            if ($res) {
81                print "Changes applied\n";
82                $labase->commit;
83                $LA->call_batch_sync;
84            }
85            return $res;
86        } else {
87            die "Object $otype $objname already exists, aborting\n";
88        }
89    } else {
90        my $res = $labase->create_c_object($otype, $objname, %attr);
91        if($res) {
92            print "Changes applied\n";
93            $labase->commit;
94            $LA->call_batch_sync;
95        }
96        return $res;
97    }
98}
99
100if ($inputfile) {
101    my $handle;
102    if ($inputfile eq '-') {
103        $handle = *STDIN;
104    } else {
105        open($handle, '<', $inputfile) or die
106        "Cannot open input file $@\n";
107    }
108    my $res = input_from_handle($handle);
109    close($handle);
110    exit(!$res);
111} else {
112    exit ! LATMOS::Accounts::Utils::dump_read_temp_file(
113        sub {
114            my ($fh) = @_;
115            $labase->text_empty_dump($fh, $otype,
116                {
117                    only_rw => !$with_ro,
118                }
119            );
120        },
121        sub {
122            my ($fh) = @_;
123            return input_from_handle($fh);
124        }
125    );
126}
Note: See TracBrowser for help on using the repository browser.