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

Last change on this file since 1888 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
RevLine 
[78]1#!/bin/env perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
[339]8use LATMOS::Accounts::Utils;
[78]9
[339]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
[78]20GetOptions(
21    'c|config=s' => \my $config,
22    'b|base=s'   => \my $base,
23    'o|object=s' => \my $otype,
[339]24    'f=s'        => \my $inputfile,
25    'ro'         => \my $with_ro,
26    'u|update'   => \my $allow_update,
[78]27    'help'       => sub { pod2usage(0) },
28) or pod2usage();
29
30$otype ||= 'user';
31
[339]32=head1 OPTIONS
33
34=over 4
35
[861]36=item -c|--config configdir
[339]37
[861]38Use this configuration directory instead of the default one.
[339]39
40=item -b|--base basename
41
[594]42Query this specific base instead of the default one.
[339]43
44=item -o|object object_type
45
[594]46Query will be performed on this object. Default is the 'User' object.
[339]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)
[595]59If this option is not present, you'll have a file opened in your prefered editor to edit.
[339]60
61=back
62
63=cut
64
[656]65if (!$ARGV[0]) {warn "You must specify 'obj_id', aborting\n"; pod2usage(); }
[655]66
[457]67my $LA = LATMOS::Accounts->new($config, noacl => 1);
[1044]68my $labase = $LA->base($base);
[78]69$labase && $labase->load or die "Cannot load base";
[664]70$labase->wexported(1);
[78]71
72$labase->is_supported_object($otype) or die "$otype object unsupported\n";
[339]73my $objname = $ARGV[0];
[78]74
[339]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;
[602]84                $LA->call_batch_sync;
[339]85            }
86            return $res;
87        } else {
88            die "Object $otype $objname already exists, aborting\n";
89        }
[78]90    } else {
[339]91        my $res = $labase->create_c_object($otype, $objname, %attr);
92        if($res) {
93            print "Changes applied\n";
94            $labase->commit;
[602]95            $LA->call_batch_sync;
[672]96            return 1;
[78]97        }
[672]98        return 0;
[339]99    }
100}
[78]101
[339]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,
[78]120                }
[339]121            );
122        },
123        sub {
124            my ($fh) = @_;
125            return input_from_handle($fh);
[78]126        }
[339]127    );
[78]128}
Note: See TracBrowser for help on using the repository browser.