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