source: trunk/LATMOS-Accounts/bin/la-query @ 1930

Last change on this file since 1930 was 1549, checked in by nanardon, 8 years ago

Fix I18N

  • Property svn:executable set to *
  • Property svn:keywords set to Id Rev
File size: 3.8 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
8use 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
23For the default object_type (user), obj_id = login.
24
25Example : la-query lambda
26
27=cut
28
29GetOptions(
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
50Use this configuration directory instead of the default one.
51
52=item -b|--base basename
53
54Query this specific base instead of the default one.
55
56=item -o|object object_type
57
58Query will be performed on this object. Default is the 'User' object.
59
60=item --with-unexp
61
62Take into account all objects (even non propagated ones, with attribute
63'exported'=0) (default)
64
65=item --wo-unexp
66
67Take 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
79Specify a format of output.
80
81If NOT present and "obj_id" is specified, all non-empty writable attributes will be printed (modified by -e and -ro).
82If NOT present and "obj_id" is NOT specified, only the list of "obj_id" will be printed.
83
84The "format" string may query tags in form:
85
86    %{ATTRIBUTE:printflike}
87
88Where ATTRIBUTE is the name of an attribute and the optional
89C<:printflike> is a printf like string to print the attribute content (without
90the C<%>).
91
92Examples 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
102It can be on more than one line, but all lines will be concatenated as one.
103Don't forget the "\n" if you really want seperate lines.
104
105Example of format-file:
106
107Name: %{sn}, First Name: %{givenName}\n
108     Department : %{department}\n
109
110Note : --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
116my $LA = LATMOS::Accounts->new($config, noacl => 1);
117my $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
122unless ($labase->is_supported_object($otype)) {
123    die "Unsupported object type `$otype'\n";
124}
125
126if ($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
135if (@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}
Note: See TracBrowser for help on using the repository browser.