Improve addTrEntry

This commit is contained in:
Yadd 2021-04-07 15:22:03 +02:00
parent f93c1bc394
commit 2e4fe68246

View File

@ -3,7 +3,7 @@
use strict; use strict;
use JSON; use JSON;
use Getopt::Long; use Getopt::Long;
my ( $portal, $modify, $help, $delete ); my ( $portal, $modify, $help, $delete, $reorder );
my $json = my $json =
JSON->new->utf8->pretty()->canonical()->space_before(0)->space_after(0); JSON->new->utf8->pretty()->canonical()->space_before(0)->space_after(0);
@ -11,13 +11,29 @@ GetOptions(
"portal|p" => \$portal, "portal|p" => \$portal,
"modify|m" => \$modify, "modify|m" => \$modify,
"delete|d" => \$delete, "delete|d" => \$delete,
"reorder|r" => \$reorder,
"help|h" => \$help, "help|h" => \$help,
); );
usage() if $help or !@ARGV; usage() if $help or ( !@ARGV and !$reorder );
my $key = shift @ARGV or usage(); my $key = shift @ARGV;
my $enText = shift @ARGV or $delete or usage(); my $enText = shift @ARGV;
my $frText = shift(@ARGV) || $enText; my $frText = shift(@ARGV) || $enText;
# Check args
usage() if $delete and $modify;
if ($key) {
usage() if $reorder;
if ($delete) {
usage() if $enText;
}
else {
usage() unless $enText;
}
}
else {
usage() unless $reorder;
}
# Main # Main
my $wdir = my $wdir =
'lemonldap-ng-' 'lemonldap-ng-'
@ -36,12 +52,11 @@ for my $lang (@langs) {
close $file; close $file;
} }
my $jsonObj = $json->decode($content); my $jsonObj = $json->decode($content);
if ( !$jsonObj->{$key} and $delete ) { if ($key) {
print STDERR "$key does not exist, aborting deletion\n"; if ( $jsonObj->{$key} xor( $delete or $modify ) ) {
usage(); print STDERR ( $jsonObj->{$key}
} ? "key already exists\n"
elsif ( $jsonObj->{$key} and $modify ) { : "key doesn't exit\n" );
print STDERR "$key already exists, aborting\n";
usage(); usage();
} }
if ($delete) { if ($delete) {
@ -50,6 +65,7 @@ for my $lang (@langs) {
else { else {
$jsonObj->{$key} = ( $lang eq 'fr.json' ? $frText : $enText ); $jsonObj->{$key} = ( $lang eq 'fr.json' ? $frText : $enText );
} }
}
$content = $json->encode($jsonObj); $content = $json->encode($jsonObj);
$content =~ s/\n\s+/\n/sg; $content =~ s/\n\s+/\n/sg;
open $file, '>', "$wdir/$lang" or die $!; open $file, '>', "$wdir/$lang" or die $!;
@ -68,6 +84,7 @@ Options:
--portal -p: add entry in portal translation instead of manager --portal -p: add entry in portal translation instead of manager
--modify -m: modify an existing entry --modify -m: modify an existing entry
--delete -d: delete an existing key --delete -d: delete an existing key
--reorder -r: reorder files
--help -h: display this --help -h: display this
EOT EOT
exit( $help ? 0 : 1 ); exit( $help ? 0 : 1 );