source: trunk/LATMOS-Accounts/bin/la-create @ 2043

Last change on this file since 2043 was 1998, checked in by nanardon, 7 years ago

Remove checks for args

  • Property svn:executable set to *
File size: 3.4 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 USAGE
15
16    la-create [options] [obj_id]
17
18C<la-create> create objects inside database.
19
20If no input file is given C<$EDITOR> program is launch with an empty template.
21
22The object name must given except when creating C<user> object in SQL database
23for which a try will be done from C<sn> and C<givenName>.
24
25=cut
26
27GetOptions(
28    'c|config=s' => \my $config,
29    'b|base=s'   => \my $base,
30    'o|object=s' => \my $otype,
31    'f=s'        => \my $inputfile,
32    'ro'         => \my $with_ro,
33    'u|update'   => \my $allow_update,
34    'help'       => sub { pod2usage(0) },
35) or pod2usage();
36
37$otype ||= 'user';
38
39=head1 OPTIONS
40
41=over 4
42
43=item -c|--config configdir
44
45Use this configuration directory instead of the default one.
46
47=item -b|--base basename
48
49Query this specific base instead of the default one.
50
51=item -o|object object_type
52
53Query will be performed on this object. Default is the 'User' object.
54
55=item --ro
56
57Include also read-only attributes as comment
58
59=item -u|--update
60
61If the object already exists, then updating it
62
63=item -f FILE
64
65Use this file to get attributes (- use stdin)
66If this option is not present, you'll have a file opened in your prefered editor to edit.
67
68=back
69
70=cut
71
72my $LA = LATMOS::Accounts->new($config, noacl => 1);
73my $labase = $LA->base($base);
74$labase && $labase->load or die "Cannot load base";
75$labase->wexported(1);
76
77$labase->is_supported_object($otype) or die "$otype object unsupported\n";
78my $objname = $ARGV[0];
79
80sub input_from_handle {
81    my ($fh) = @_;
82    my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh);
83    if ($objname && (my $obj = $labase->get_object($otype, $objname))) {
84        if ($allow_update) {
85            my $res = $obj->set_c_fields(%attr);
86            if ($res) {
87                print "Changes applied\n";
88                $labase->commit;
89                $LA->call_batch_sync;
90            }
91            return $res;
92        } else {
93            die "Object $otype $objname already exists, aborting\n";
94        }
95    } else {
96        if ($objname) {
97            my $res = $labase->create_c_object($otype, $objname, %attr);
98            if($res) {
99                print "Changes applied\n";
100                $labase->commit;
101                $LA->call_batch_sync;
102                return 1;
103            }
104            return 0;
105        } else {
106            my $ochelper = $labase->ochelper($otype);
107
108            my $info = {
109                contents => { %attr },
110            };
111            if ($attr{name}) {
112                $info->{name}{content} = $attr{name};
113            }
114
115            $ochelper->Automate($info) or
116                die "Cannot create object (not enough information ?)\n";
117        } 
118    }
119}
120
121if ($inputfile) {
122    my $handle;
123    if ($inputfile eq '-') {
124        $handle = *STDIN;
125    } else {
126        open($handle, '<', $inputfile) or die
127        "Cannot open input file $@\n";
128    }
129    my $res = input_from_handle($handle);
130    close($handle);
131    exit(!$res);
132} else {
133    exit ! LATMOS::Accounts::Utils::dump_read_temp_file(
134        sub {
135            my ($fh) = @_;
136            $labase->text_empty_dump($fh, $otype,
137                {
138                    only_rw => !$with_ro,
139                }
140            );
141        },
142        sub {
143            my ($fh) = @_;
144            return input_from_handle($fh);
145        }
146    );
147}
Note: See TracBrowser for help on using the repository browser.