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

Last change on this file since 376 was 339, checked in by nanardon, 15 years ago
  • update la-create to use same scheme than la-edit
  • Property svn:executable set to *
File size: 2.6 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 cofngiuration file instead the default one
39
40=item -b|--base basename
41
42Query this specific base instead default.
43
44=item -o|object object_type
45
46Query should be performed on this objects. Default is to user '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)
59
60=back
61
62=cut
63
64my $LA = LATMOS::Accounts->new($config);
65my $labase = $base ? $LA->base($base) : $LA->default_base;
66$labase && $labase->load or die "Cannot load base";
67
68$labase->is_supported_object($otype) or die "$otype object unsupported\n";
69my $objname = $ARGV[0];
70
71sub input_from_handle {
72    my ($fh) = @_;
73    my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh);
74    if (my $obj = $labase->get_object($otype, $objname)) {
75        if ($allow_update) {
76            my $res = $obj->set_c_fields(%attr);
77            if ($res) {
78                print "Changes applied\n";
79                $labase->commit;
80            }
81            return $res;
82        } else {
83            die "Object $otype $objname already exists, aborting\n";
84        }
85    } else {
86        my $res = $labase->create_c_object($otype, $objname, %attr);
87        if($res) {
88            print "Changes applied\n";
89            $labase->commit;
90        }
91        return $res;
92    }
93}
94
95if ($inputfile) {
96    my $handle;
97    if ($inputfile eq '-') {
98        $handle = *STDIN;
99    } else {
100        open($handle, '<', $inputfile) or die
101        "Cannot open input file $@\n";
102    }
103    my $res = input_from_handle($handle);
104    close($handle);
105    exit(!$res);
106} else {
107    exit ! LATMOS::Accounts::Utils::dump_read_temp_file(
108        sub {
109            my ($fh) = @_;
110            $labase->text_empty_dump($fh, $otype,
111                {
112                    only_rw => !$with_ro,
113                }
114            );
115        },
116        sub {
117            my ($fh) = @_;
118            return input_from_handle($fh);
119        }
120    );
121}
Note: See TracBrowser for help on using the repository browser.