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

Last change on this file since 827 was 827, checked in by nanardon, 14 years ago
  • make open less ambigous
  • Property svn:executable set to *
  • Property svn:keywords set to Id Rev
File size: 3.6 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    '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
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 --noexp
58
59Take into account all objects (even non propagated ones, with attribute 'exported'=0)
60
61=item --exp
62
63Take 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
75Specify a format of output.
76
77If NOT present and "obj_id" is specified, all non-empty writable attributes will be printed (modified by -e and -ro).
78If NOT present and "obj_id" is NOT specified, only the list of "obj_id" will be printed.
79
80The "format" string may query tags in form:
81
82    %{ATTRIBUTE:printflike}
83
84Where ATTRIBUTE is the name of an attribute and the optional
85C<:printflike> is a printf like string to print the attribute content (without
86the C<%>).
87
88Examples 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
98It can be on more than one line, but all lines will be concatenated as one.
99Don't forget the "\n" if you really want seperate lines.
100
101Example of format-file:
102
103Name: %{sn}, First Name: %{givenName}\n
104     Department : %{department}\n
105
106Note : --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
112my $LA = LATMOS::Accounts->new($config, noacl => 1);
113my $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
118if ($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
127if (@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}
Note: See TracBrowser for help on using the repository browser.