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

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