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

Last change on this file since 1427 was 1044, checked in by nanardon, 12 years ago

Kill redundant LATMOS::Account::default_base()

Use $LA->base(undef) to get default base instead

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