source: trunk/LATMOS-Accounts/bin/la-edit @ 1634

Last change on this file since 1634 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.5 KB
RevLine 
[334]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
[601]15    la-edit [options] obj_id
[334]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
[861]35=item -c|--config configdir
[334]36
[861]37Use this configuration directory instead of the default one.
[334]38
39=item -b|--base basename
40
[594]41Query this specific base instead of the default one.
[334]42
43=item -o|object object_type
44
[594]45Query will be performed on this object. Default is the 'User' object.
[334]46
47=item -e|--empty
48
49Include also unset attributes
50
51=item --ro
52
53Include also read-only attributes as comment
54
[339]55=item -f FILE
56
57Use this file to get attributes (- use stdin)
[596]58If this option is not present, you'll have a file opened in your prefered editor to edit.
[339]59
[334]60=back
61
62=cut
63
[656]64if (!$ARGV[0]) {warn "You must specify 'obj_id', aborting\n"; pod2usage(); }
[655]65
[457]66my $LA = LATMOS::Accounts->new($config, noacl => 1);
[1044]67my $labase = $LA->base($base);
[334]68$labase && $labase->load or die "Cannot load base";
[664]69$labase->wexported(1);
[334]70
71sub input_from_handle {
72    my ($obj, $fh) = @_;
73    my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh);
74    my $res = $obj->set_c_fields(%attr);
75    if($res) {
76        print "Changes applied\n";
77        $labase->commit;
[602]78        $LA->call_batch_sync;
[334]79    }
80    return
[357]81    $res ? 1 : 0;
[334]82}
83
84if (my $ouid = shift(@ARGV)) {
85    my $obj = $labase->get_object($otype, $ouid) or do {
86        die "Object $otype $ouid not found\n";
87    };
88
89    if ($inputfile) {
90        my $handle;
91        if ($inputfile eq '-') {
92            $handle = *STDIN;
93        } else {
94            open($handle, '<', $inputfile) or die
95                "Cannot open input file $@\n";
96        }
97        my $res = input_from_handle($obj, $handle);
98        close($handle);
99        exit(!$res);
100    } else {
101        exit ! LATMOS::Accounts::Utils::dump_read_temp_file(
102            sub {
103                my ($fh) = @_;
104                $obj->text_dump($fh,
105                    {
106                        empty_attr => $empty_attr,
107                        only_rw => !$with_ro,
108                    }
109                );
110            },
111            sub {
112                my ($fh) = @_;
113                return input_from_handle($obj, $fh);
114            }
115        );
116    }
117} else {
[601]118    pod2usage();
[334]119}
Note: See TracBrowser for help on using the repository browser.