1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use LATMOS::Accounts::Utils; |
---|
7 | use Getopt::Long; |
---|
8 | use Pod::Usage; |
---|
9 | use 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 | |
---|
23 | GetOptions( |
---|
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 | |
---|
39 | Use this configuration directory instead of the default one. |
---|
40 | |
---|
41 | =item -b|--base basename |
---|
42 | |
---|
43 | Query this specific base instead of the default one. |
---|
44 | |
---|
45 | =item -o|object object_type |
---|
46 | |
---|
47 | Query will be performed on this object. Default is the 'User' object. |
---|
48 | |
---|
49 | =item -t|--test |
---|
50 | |
---|
51 | If the underlying database support it don't commit change at the end (Works |
---|
52 | only on SQL base currently) |
---|
53 | |
---|
54 | =back |
---|
55 | |
---|
56 | =cut |
---|
57 | |
---|
58 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
59 | my $labase = $LA->base($base); |
---|
60 | $labase && $labase->load or die l("Cannot load base %s\n", $base); |
---|
61 | |
---|
62 | unless ($labase->is_supported_object($otype)) { |
---|
63 | die "Unsupported object type `$otype'\n"; |
---|
64 | } |
---|
65 | |
---|
66 | open(my $fh, '<', $ARGV[0]) or die "Cannot open $ARGV[0]: $!\n"; |
---|
67 | |
---|
68 | loadCSV( |
---|
69 | $fh, |
---|
70 | 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 | |
---|
87 | if($test) { |
---|
88 | warn "Changes not commited due to test option\n"; |
---|
89 | } else { |
---|
90 | $labase->commit(); |
---|
91 | } |
---|