source: trunk/LATMOS-Accounts/bin/la-load-csv @ 2380

Last change on this file since 2380 was 1991, checked in by nanardon, 7 years ago

Use hash for loadCSV options to allow more parameters

  • Property svn:executable set to *
File size: 1.8 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use LATMOS::Accounts::Utils;
7use Getopt::Long;
8use Pod::Usage;
9use LATMOS::Accounts::I18N;
10
11=head1 NAME
12
13    la-load-csv - Create object from CSV file
14
15=head1 SYNOPSIS
16
17    la-load-csv Input.csv
18
19=head1 DESCRIPTION
20
21=cut
22
23GetOptions(
24    'c|config=s'        => \my $config,
25    'b|base=s'          => \my $base,
26    'o|object=s'        => \my $otype,
27    't|test'            => \my $test,
28    'help'              => sub { pod2usage(0) },
29) or pod2usage();
30
31$otype ||= 'user';
32
33=head1 OPTIONS
34
35=over 4
36
37=item -c|--config configdir
38
39Use this configuration directory instead of the default one.
40
41=item -b|--base basename
42
43Query this specific base instead of the default one.
44
45=item -o|object object_type
46
47Query will be performed on this object. Default is the 'User' object.
48
49=item -t|--test
50
51If the underlying database support it don't commit change at the end (Works
52only on SQL base currently)
53
54=back
55
56=cut
57
58my $LA = LATMOS::Accounts->new($config, noacl => 1);
59my $labase = $LA->base($base);
60$labase && $labase->load or die l("Cannot load base %s\n", $base);
61
62unless ($labase->is_supported_object($otype)) {
63    die "Unsupported object type `$otype'\n";
64}
65
66open(my $fh, '<', $ARGV[0]) or die "Cannot open  $ARGV[0]: $!\n";
67
68loadCSV(
69    $fh,
70    cb => sub {
71        my ($res, $linecount) = @_;
72
73        my $ochelper = $labase->ochelper($otype);
74       
75        my $info = {
76            contents => $res
77        };
78        if ($res->{name}) {
79            $info->{name}{content} = $res->{name};
80        }
81       
82        $ochelper->Automate($info) or
83            die "Cannot create object line $linecount (not enough information ?)\n";
84    },
85);
86
87if($test) {
88    warn "Changes not commited due to test option\n";
89} else {
90    $labase->commit();
91}
Note: See TracBrowser for help on using the repository browser.