source: trunk/LATMOS-Accounts/bin/la-dump @ 2321

Last change on this file since 2321 was 2321, checked in by nanardon, 4 years ago

Doc completion

File size: 3.6 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-dump - Extract information
12
13=head1 SYNOPSIS
14
15    la-dump [options] filter [filter2 [...]]
16
17=head1 OPTIONS
18
19=over 4
20
21=item --config configdir
22
23Use this configuration directory instead default
24
25=item --base basename
26
27Perform search in this specific base instead default
28
29=item --with-unexp
30
31Take into account all objects (even non propagated ones, with attribute
32'exported'=0) (default)
33
34=item --wo-unexp
35
36Take into account only propagated objects (attribute 'exported'=1)
37
38=item --recur
39
40Dump objects recursively
41
42=item --subotype OTYPE1 [--subotype OTYPE2]
43
44The list of subobject type to dump
45
46=item --fmt format
47
48Specify a format of output.
49
50=item FILTER
51
52Filter are in form "[ATTRIBUTE][OPERATOR][VALUE]"
53
54where attribute is a supported attribute.
55
56OPERATOR can be:
57
58=over 4
59
60=item C<=>
61
62Search object having attribute strictely equal to VALUE. If VALUE is C<*> match
63any non empty value.
64
65=item C<~>
66
67Search object having attribute containing VALUE.
68
69=back
70
71=back
72
73=cut
74
75GetOptions(
76    'c|config=s'        => \my $config,
77    'b|base=s'          => \my $base,
78    'o|object=s'        => \my $otype,
79    'fmt=s'             => \my @fmt,
80    'recur'             => \my $recur,
81    'subotype=s'        => \my @SubOtype,
82    'no-unexp|wo-unexp' => \my $nounexp,
83    'with-unexp'        => \my $unexp,
84    'u|username=s'      => \my $username,
85    'help'              => sub { pod2usage(0) },
86    'f=s'               => \my $filename,
87) or pod2usage();
88
89$otype ||= 'user';
90
91my $LA = LATMOS::Accounts->new($config, noacl => $username ? 0 : 1);
92my $labase = $LA->base($base);
93$labase && $labase->load or die "Cannot load base";
94
95$labase->unexported($nounexp ? 0 : 1);
96$labase->SetConnectedUser($username) if ($username);
97
98my @result = $labase->search_objects($otype, @ARGV);
99
100my @Dump = ();
101
102foreach (@result) {
103    my $o = $labase->get_object($otype, $_) or next;
104    push(@Dump, $o->DataDump(
105            {
106                recur => $recur,
107                SubOtype => \@SubOtype,
108                noSchema => 1,
109            }
110    ));
111}
112
113@fmt = qw(xml) unless (@fmt);
114
115foreach my $Format (@fmt) {
116
117    my $string = '';
118
119    for ($Format) {
120        /^xml$/ and do {
121            eval {
122                require XML::Simple;
123            };
124            if ($@) {
125                warn "Cannot output xml: XML::Simple perl module missing";
126                next;
127            }
128            my $xml = XML::Simple->new();
129            $string = $xml->XMLout(\@Dump);
130            next;
131        };
132        /^json$/ and do {
133            eval {
134                require JSON;
135            };
136            if ($@) {
137                warn "Cannot output xml: JSON perl module missing";
138                next;
139            }
140            my $json = JSON->new;
141            $json->indent(1);
142            $string = $json->encode(\@Dump);
143            next;
144        };
145        /^yaml$/ and do {
146            eval {
147                require YAML;
148            };
149            if ($@) {
150                warn "Cannot output xml: YAML perl module missing";
151                next;
152            }
153            $string = YAML::Dump(\@Dump);
154            next;
155        };
156        /^pl$/ and do {
157            require Data::Dumper;
158            $string = Data::Dumper::Dumper(\@Dump);
159        }
160    }
161
162    if ($filename) {
163        my $realFilename = $filename . '.' . $Format;
164        open(my $handle, '>', $realFilename) or do {
165            warn "Cannot open $realFilename for wrinting";
166            next;
167        };
168        print $handle $string;
169        close($handle);
170    } else {
171        print $string;
172    }
173}
174
175exit(0);
Note: See TracBrowser for help on using the repository browser.