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