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 --recur |
---|
39 | |
---|
40 | Dump objects recursively |
---|
41 | |
---|
42 | =item --subotype OTYPE1 [--subotype OTYPE2] |
---|
43 | |
---|
44 | The list of subobject type to dump |
---|
45 | |
---|
46 | =item --fmt format |
---|
47 | |
---|
48 | Specify a format of output. |
---|
49 | |
---|
50 | =item FILTER |
---|
51 | |
---|
52 | Filter are in form "[ATTRIBUTE][OPERATOR][VALUE]" |
---|
53 | |
---|
54 | where attribute is a supported attribute. |
---|
55 | |
---|
56 | OPERATOR can be: |
---|
57 | |
---|
58 | =over 4 |
---|
59 | |
---|
60 | =item C<=> |
---|
61 | |
---|
62 | Search object having attribute strictely equal to VALUE. If VALUE is C<*> match |
---|
63 | any non empty value. |
---|
64 | |
---|
65 | =item C<~> |
---|
66 | |
---|
67 | Search object having attribute containing VALUE. |
---|
68 | |
---|
69 | =back |
---|
70 | |
---|
71 | =back |
---|
72 | |
---|
73 | =cut |
---|
74 | |
---|
75 | GetOptions( |
---|
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 | |
---|
91 | my $LA = LATMOS::Accounts->new($config, noacl => $username ? 0 : 1); |
---|
92 | my $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 | |
---|
98 | my @result = $labase->search_objects($otype, @ARGV); |
---|
99 | |
---|
100 | my @Dump = (); |
---|
101 | |
---|
102 | foreach (@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 | |
---|
115 | foreach 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 | |
---|
175 | exit(0); |
---|