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

Last change on this file since 656 was 612, checked in by vivat, 14 years ago

Mise a jour de l'aide refletant la derniere version

  • Property svn:executable set to *
  • Property svn:keywords set to Id Rev
File size: 3.3 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    'help'       => sub { pod2usage(0) },
35) or pod2usage();
36
37$otype ||= 'user';
38
39=head1 OPTIONS
40
41=over 4
42
43=item -c|--config configfile
44
45Use this configuration file instead of the default one.
46
47=item -b|--base basename
48
49Query this specific base instead of the default one.
50
51=item -o|object object_type
52
53Query will be performed on this object. Default is the 'User' object.
54
55=item -e|--empty
56
57(Only if obj_id is specified) Include also unset attributes.
58
59=item --ro
60
61(Only if obj_id is specified) Include also read-only attributes as comments.
62
63=item --fmt format
64
65Specify a format of output.
66
67If NOT present and "obj_id" is specified, all non-empty writable attributes will be printed (modified by -e and -ro).
68If NOT present and "obj_id" is NOT specified, only the list of "obj_id" will be printed.
69
70The "format" string may query tags in form:
71
72    %{ATTRIBUTE:printflike}
73
74Where ATTRIBUTE is the name of an attribute and the optional
75C<:printflike> is a printf like string to print the attribute content (without
76the C<%>).
77
78Examples for users:
79
80    --fmt "Name %{sn}, First Name %{givenName}\n"
81
82    --fmt "Name %{sn:20s}, First Name %{givenName}\n"
83
84=item --filefmt format-file
85
86"format-file" is a text file with the same strings as "--fmt" option, WITHOUT quotes (").
87
88It can be on more than one line, but all lines will be concatenated as one.
89Don't forget the "\n" if you really want seperate lines.
90
91Example of format-file:
92
93Name: %{sn}, First Name: %{givenName}\n
94     Department : %{department}\n
95
96Note : --fmt and --filefmt can be used together, and will be concatenated (the "fmt" string first, then what is in the "format-file").
97
98=back
99
100=cut
101
102my $LA = LATMOS::Accounts->new($config, noacl => 1);
103my $labase = $base ? $LA->base($base) : $LA->default_base;
104$labase && $labase->load or die "Cannot load base";
105
106if ($filefmt){
107        open my $hfmt, $filefmt or die "Cannot open $filefmt\n";
108        $fmt ||= ''; # avoid undef warning
109        while (<$hfmt>) {
110                chomp($fmt .= $_);
111        }
112        close $hfmt;
113}
114
115if (my $ouid = shift(@ARGV)) {
116    my $obj = $labase->get_object($otype, $ouid) or do {
117        die "Object $otype $ouid not found\n";
118    };
119    if ($fmt) {
120        print $obj->queryformat($fmt);
121    } else {
122        $obj->text_dump(*STDOUT,
123            {
124                empty_attr => $empty_attr,
125                only_rw => !$with_ro,
126            }
127        );
128    }
129} else {
130    foreach (sort $labase->list_objects($otype)) {
131        if ($fmt) {
132            my $o = $labase->get_object($otype, $_) or next;
133            print $o->queryformat($fmt);
134        } else {
135            print "$_\n";
136        }
137    }
138}
Note: See TracBrowser for help on using the repository browser.