source: LATMOS-Accounts/bin/la-query @ 849

Last change on this file since 849 was 849, checked in by nanardon, 14 years ago
  • make unxported mode options more explicit
  • fix POD according behavior
  • Property svn:executable set to *
  • Property svn:keywords set to Id Rev
File size: 3.7 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use 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
20For the default object_type (user), obj_id = login.
21
22Example : la-query lambda
23
24=cut
25
26GetOptions(
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    'no-unexp|wo-unexp' => \my $nounexp,
35    'with-unexp'        => \my $unexp,
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
47Use this configuration file instead of the default one.
48
49=item -b|--base basename
50
51Query this specific base instead of the default one.
52
53=item -o|object object_type
54
55Query will be performed on this object. Default is the 'User' object.
56
57=item --with-unexp
58
59Take into account all objects (even non propagated ones, with attribute
60'exported'=0) (default)
61
62=item --wo-unexp
63
64Take into account only propagated objects (attribute 'exported'=1)
65
66=item -e|--empty
67
68(Only if obj_id is specified) Include also unset attributes.
69
70=item --ro
71
72(Only if obj_id is specified) Include also read-only attributes as comments.
73
74=item --fmt format
75
76Specify a format of output.
77
78If NOT present and "obj_id" is specified, all non-empty writable attributes will be printed (modified by -e and -ro).
79If NOT present and "obj_id" is NOT specified, only the list of "obj_id" will be printed.
80
81The "format" string may query tags in form:
82
83    %{ATTRIBUTE:printflike}
84
85Where ATTRIBUTE is the name of an attribute and the optional
86C<:printflike> is a printf like string to print the attribute content (without
87the C<%>).
88
89Examples for users:
90
91    --fmt "Name %{sn}, First Name %{givenName}\n"
92
93    --fmt "Name %{sn:20s}, First Name %{givenName}\n"
94
95=item --filefmt format-file
96
97"format-file" is a text file with the same strings as "--fmt" option, WITHOUT quotes (").
98
99It can be on more than one line, but all lines will be concatenated as one.
100Don't forget the "\n" if you really want seperate lines.
101
102Example of format-file:
103
104Name: %{sn}, First Name: %{givenName}\n
105     Department : %{department}\n
106
107Note : --fmt and --filefmt can be used together, and will be concatenated (the "fmt" string first, then what is in the "format-file").
108
109=back
110
111=cut
112
113my $LA = LATMOS::Accounts->new($config, noacl => 1);
114my $labase = $base ? $LA->base($base) : $LA->default_base;
115$labase && $labase->load or die "Cannot load base";
116
117$labase->unexported($nounexp ? 0 : 1);
118
119if ($filefmt){
120        open(my $hfmt, '<', $filefmt) or die "Cannot open $filefmt\n";
121        $fmt ||= ''; # avoid undef warning
122        while (<$hfmt>) {
123                chomp($fmt .= $_);
124        }
125        close $hfmt;
126}
127
128if (@ARGV) {
129    foreach my $ouid (@ARGV) {
130        my $obj = $labase->get_object($otype, $ouid) or do {
131            die "Object $otype $ouid not found\n";
132        };
133        if ($fmt) {
134            print $obj->queryformat($fmt);
135        } else {
136            $obj->text_dump(*STDOUT,
137                {
138                    empty_attr => $empty_attr,
139                    only_rw => !$with_ro,
140                }
141            );
142        }
143    }
144} else {
145    foreach (sort $labase->list_objects($otype)) {
146        if ($fmt) {
147            my $o = $labase->get_object($otype, $_) or next;
148            print $o->queryformat($fmt);
149        } else {
150            print "$_\n";
151        }
152    }
153}
Note: See TracBrowser for help on using the repository browser.