1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | use LATMOS::Accounts::I18N; |
---|
9 | |
---|
10 | =head1 NAME |
---|
11 | |
---|
12 | la-query - Tools to query base in LATMOS::Accounts system |
---|
13 | |
---|
14 | =head1 SYNOPSIS |
---|
15 | |
---|
16 | la-query [options] [obj_id] |
---|
17 | |
---|
18 | =head1 DESCRIPTION |
---|
19 | |
---|
20 | [obj_id] : If present, all set attributes (rw) will be displayed for that obj_id. |
---|
21 | If none is given, all obj_ids will be printed. |
---|
22 | |
---|
23 | For the default object_type (user), obj_id = login. |
---|
24 | |
---|
25 | Example : la-query lambda |
---|
26 | |
---|
27 | =cut |
---|
28 | |
---|
29 | GetOptions( |
---|
30 | 'c|config=s' => \my $config, |
---|
31 | 'b|base=s' => \my $base, |
---|
32 | 'o|object=s' => \my $otype, |
---|
33 | 'e|empty' => \my $empty_attr, |
---|
34 | 'fmt=s' => \my $fmt, |
---|
35 | 'filefmt=s' => \my $filefmt, |
---|
36 | 'ro' => \my $with_ro, |
---|
37 | 'no-unexp|wo-unexp' => \my $nounexp, |
---|
38 | 'with-unexp' => \my $unexp, |
---|
39 | 'help' => sub { pod2usage(0) }, |
---|
40 | ) or pod2usage(); |
---|
41 | |
---|
42 | $otype ||= 'user'; |
---|
43 | |
---|
44 | =head1 OPTIONS |
---|
45 | |
---|
46 | =over 4 |
---|
47 | |
---|
48 | =item -c|--config configdir |
---|
49 | |
---|
50 | Use this configuration directory instead of the default one. |
---|
51 | |
---|
52 | =item -b|--base basename |
---|
53 | |
---|
54 | Query this specific base instead of the default one. |
---|
55 | |
---|
56 | =item -o|object object_type |
---|
57 | |
---|
58 | Query will be performed on this object. Default is the 'User' object. |
---|
59 | |
---|
60 | =item --with-unexp |
---|
61 | |
---|
62 | Take into account all objects (even non propagated ones, with attribute |
---|
63 | 'exported'=0) (default) |
---|
64 | |
---|
65 | =item --wo-unexp |
---|
66 | |
---|
67 | Take into account only propagated objects (attribute 'exported'=1) |
---|
68 | |
---|
69 | =item -e|--empty |
---|
70 | |
---|
71 | (Only if obj_id is specified) Include also unset attributes. |
---|
72 | |
---|
73 | =item --ro |
---|
74 | |
---|
75 | (Only if obj_id is specified) Include also read-only attributes as comments. |
---|
76 | |
---|
77 | =item --fmt format |
---|
78 | |
---|
79 | Specify a format of output. |
---|
80 | |
---|
81 | If NOT present and "obj_id" is specified, all non-empty writable attributes will be printed (modified by -e and -ro). |
---|
82 | If NOT present and "obj_id" is NOT specified, only the list of "obj_id" will be printed. |
---|
83 | |
---|
84 | The "format" string may query tags in form: |
---|
85 | |
---|
86 | %{ATTRIBUTE:printflike} |
---|
87 | |
---|
88 | Where ATTRIBUTE is the name of an attribute and the optional |
---|
89 | C<:printflike> is a printf like string to print the attribute content (without |
---|
90 | the C<%>). |
---|
91 | |
---|
92 | Examples for users: |
---|
93 | |
---|
94 | --fmt "Name %{sn}, First Name %{givenName}\n" |
---|
95 | |
---|
96 | --fmt "Name %{sn:20s}, First Name %{givenName}\n" |
---|
97 | |
---|
98 | =item --filefmt format-file |
---|
99 | |
---|
100 | "format-file" is a text file with the same strings as "--fmt" option, WITHOUT quotes ("). |
---|
101 | |
---|
102 | It can be on more than one line, but all lines will be concatenated as one. |
---|
103 | Don't forget the "\n" if you really want seperate lines. |
---|
104 | |
---|
105 | Example of format-file: |
---|
106 | |
---|
107 | Name: %{sn}, First Name: %{givenName}\n |
---|
108 | Department : %{department}\n |
---|
109 | |
---|
110 | Note : --fmt and --filefmt can be used together, and will be concatenated (the "fmt" string first, then what is in the "format-file"). |
---|
111 | |
---|
112 | =back |
---|
113 | |
---|
114 | =cut |
---|
115 | |
---|
116 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
117 | my $labase = $LA->base($base); |
---|
118 | $labase && $labase->load or die l("Cannot load base %s\n", $base); |
---|
119 | |
---|
120 | $labase->unexported($nounexp ? 0 : 1); |
---|
121 | |
---|
122 | unless ($labase->is_supported_object($otype)) { |
---|
123 | die "Unsupported object type `$otype'\n"; |
---|
124 | } |
---|
125 | |
---|
126 | if ($filefmt){ |
---|
127 | open(my $hfmt, '<', $filefmt) or die "Cannot open $filefmt\n"; |
---|
128 | $fmt ||= ''; # avoid undef warning |
---|
129 | while (<$hfmt>) { |
---|
130 | chomp($fmt .= $_); |
---|
131 | } |
---|
132 | close $hfmt; |
---|
133 | } |
---|
134 | |
---|
135 | if (@ARGV) { |
---|
136 | foreach my $ouid (@ARGV) { |
---|
137 | my $obj = $labase->get_object($otype, $ouid) or do { |
---|
138 | die "Object $otype $ouid not found\n"; |
---|
139 | }; |
---|
140 | if ($fmt) { |
---|
141 | print $obj->queryformat($fmt); |
---|
142 | } else { |
---|
143 | $obj->text_dump(*STDOUT, |
---|
144 | { |
---|
145 | empty_attr => $empty_attr, |
---|
146 | only_rw => !$with_ro, |
---|
147 | } |
---|
148 | ); |
---|
149 | } |
---|
150 | } |
---|
151 | } else { |
---|
152 | foreach (sort $labase->list_objects($otype)) { |
---|
153 | if ($fmt) { |
---|
154 | my $o = $labase->get_object($otype, $_) or next; |
---|
155 | print $o->queryformat($fmt); |
---|
156 | } else { |
---|
157 | print "$_\n"; |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|