source: LATMOS-Accounts/bin/la-edit @ 656

Last change on this file since 656 was 656, checked in by nanardon, 14 years ago
  • error messages goes to STDERR
  • Property svn:executable set to *
File size: 2.5 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
8
9=head1 NAME
10
11    la-edit - Tools to edit object in LATMOS::Accounts system
12
13=head1 SYNOPSIS
14
15    la-edit [options] obj_id
16
17=cut
18
19GetOptions(
20    'c|config=s' => \my $config,
21    'b|base=s'   => \my $base,
22    'o|object=s' => \my $otype,
23    'e|empty'    => \my $empty_attr,
24    'ro'         => \my $with_ro,
25    'f=s'        => \my $inputfile,
26    'help'       => sub { pod2usage(0) },
27) or pod2usage();
28
29$otype ||= 'user';
30
31=head1 OPTIONS
32
33=over 4
34
35=item -c|--config configfile
36
37Use this configuration file instead of the default one.
38
39=item -b|--base basename
40
41Query this specific base instead of the default one.
42
43=item -o|object object_type
44
45Query will be performed on this object. Default is the 'User' object.
46
47=item -e|--empty
48
49Include also unset attributes
50
51=item --ro
52
53Include also read-only attributes as comment
54
55=item -f FILE
56
57Use this file to get attributes (- use stdin)
58If this option is not present, you'll have a file opened in your prefered editor to edit.
59
60=back
61
62=cut
63
64if (!$ARGV[0]) {warn "You must specify 'obj_id', aborting\n"; pod2usage(); }
65
66my $LA = LATMOS::Accounts->new($config, noacl => 1);
67my $labase = $base ? $LA->base($base) : $LA->default_base;
68$labase && $labase->load or die "Cannot load base";
69
70sub input_from_handle {
71    my ($obj, $fh) = @_;
72    my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh);
73    my $res = $obj->set_c_fields(%attr);
74    if($res) {
75        print "Changes applied\n";
76        $labase->commit;
77        $LA->call_batch_sync;
78    }
79    return
80    $res ? 1 : 0;
81}
82
83if (my $ouid = shift(@ARGV)) {
84    my $obj = $labase->get_object($otype, $ouid) or do {
85        die "Object $otype $ouid not found\n";
86    };
87
88    if ($inputfile) {
89        my $handle;
90        if ($inputfile eq '-') {
91            $handle = *STDIN;
92        } else {
93            open($handle, '<', $inputfile) or die
94                "Cannot open input file $@\n";
95        }
96        my $res = input_from_handle($obj, $handle);
97        close($handle);
98        exit(!$res);
99    } else {
100        exit ! LATMOS::Accounts::Utils::dump_read_temp_file(
101            sub {
102                my ($fh) = @_;
103                $obj->text_dump($fh,
104                    {
105                        empty_attr => $empty_attr,
106                        only_rw => !$with_ro,
107                    }
108                );
109            },
110            sub {
111                my ($fh) = @_;
112                return input_from_handle($obj, $fh);
113            }
114        );
115    }
116} else {
117    pod2usage();
118}
Note: See TracBrowser for help on using the repository browser.