1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | la-edit - Tools to edit object in LATMOS::Accounts system |
---|
12 | |
---|
13 | =head1 SYNOPSIS |
---|
14 | |
---|
15 | la-edit [options] obj_id |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | GetOptions( |
---|
20 | 'c|config=s' => \my $config, |
---|
21 | 'b|base=s' => \my $base, |
---|
22 | 'o|object=s' => \my $otype, |
---|
23 | 'e|empty' => \my $empty_attr, |
---|
24 | 'ro' => \my $with_ro, |
---|
25 | 'f=s' => \my $inputfile, |
---|
26 | 'help' => sub { pod2usage(0) }, |
---|
27 | ) or pod2usage(); |
---|
28 | |
---|
29 | $otype ||= 'user'; |
---|
30 | |
---|
31 | =head1 OPTIONS |
---|
32 | |
---|
33 | =over 4 |
---|
34 | |
---|
35 | =item -c|--config configdir |
---|
36 | |
---|
37 | Use this configuration directory instead of the default one. |
---|
38 | |
---|
39 | =item -b|--base basename |
---|
40 | |
---|
41 | Query this specific base instead of the default one. |
---|
42 | |
---|
43 | =item -o|object object_type |
---|
44 | |
---|
45 | Query will be performed on this object. Default is the 'User' object. |
---|
46 | |
---|
47 | =item -e|--empty |
---|
48 | |
---|
49 | Include also unset attributes |
---|
50 | |
---|
51 | =item --ro |
---|
52 | |
---|
53 | Include also read-only attributes as comment |
---|
54 | |
---|
55 | =item -f FILE |
---|
56 | |
---|
57 | Use this file to get attributes (- use stdin) |
---|
58 | If this option is not present, you'll have a file opened in your prefered editor to edit. |
---|
59 | |
---|
60 | =back |
---|
61 | |
---|
62 | =cut |
---|
63 | |
---|
64 | if (!$ARGV[0]) {warn "You must specify 'obj_id', aborting\n"; pod2usage(); } |
---|
65 | |
---|
66 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
67 | my $labase = $LA->base($base); |
---|
68 | $labase && $labase->load or die "Cannot load base"; |
---|
69 | $labase->wexported(1); |
---|
70 | |
---|
71 | sub input_from_handle { |
---|
72 | my ($obj, $fh) = @_; |
---|
73 | my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh); |
---|
74 | my $res = $obj->set_c_fields(%attr); |
---|
75 | if($res) { |
---|
76 | print "Changes applied\n"; |
---|
77 | $labase->commit; |
---|
78 | $LA->call_batch_sync; |
---|
79 | } |
---|
80 | return |
---|
81 | $res ? 1 : 0; |
---|
82 | } |
---|
83 | |
---|
84 | if (my $ouid = shift(@ARGV)) { |
---|
85 | my $obj = $labase->get_object($otype, $ouid) or do { |
---|
86 | die "Object $otype $ouid not found\n"; |
---|
87 | }; |
---|
88 | |
---|
89 | if ($inputfile) { |
---|
90 | my $handle; |
---|
91 | if ($inputfile eq '-') { |
---|
92 | $handle = *STDIN; |
---|
93 | } else { |
---|
94 | open($handle, '<', $inputfile) or die |
---|
95 | "Cannot open input file $@\n"; |
---|
96 | } |
---|
97 | my $res = input_from_handle($obj, $handle); |
---|
98 | close($handle); |
---|
99 | exit(!$res); |
---|
100 | } else { |
---|
101 | exit ! LATMOS::Accounts::Utils::dump_read_temp_file( |
---|
102 | sub { |
---|
103 | my ($fh) = @_; |
---|
104 | $obj->text_dump($fh, |
---|
105 | { |
---|
106 | empty_attr => $empty_attr, |
---|
107 | only_rw => !$with_ro, |
---|
108 | } |
---|
109 | ); |
---|
110 | }, |
---|
111 | sub { |
---|
112 | my ($fh) = @_; |
---|
113 | return input_from_handle($obj, $fh); |
---|
114 | } |
---|
115 | ); |
---|
116 | } |
---|
117 | } else { |
---|
118 | pod2usage(); |
---|
119 | } |
---|