From 8f80bf5deef8bfe8b38d42390915e3c36c521b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Oudot?= Date: Thu, 25 Aug 2016 14:03:05 +0000 Subject: [PATCH] Allow multi level root key in addKey/delKey cli commands (#1075) --- .../lib/Lemonldap/NG/Manager/Cli.pm | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Cli.pm b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Cli.pm index 7435359d1..233393186 100644 --- a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Cli.pm +++ b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Cli.pm @@ -80,12 +80,13 @@ sub addKey { unless ( @_ % 3 == 0 ) { die 'usage: "addKey (?:rootKey newKey newValue)+'; } + my $sep = $self->sep; my @list; while (@_) { my $root = shift; my $newKey = shift; my $value = shift; - unless ( $root =~ /$simpleHashKeys$/o ) { + unless ( $root =~ /$simpleHashKeys$/o or $root =~ /$sep/o ) { die "$root is not a simple hash. Aborting"; } push @list, [ $root, $newKey, $value ]; @@ -93,7 +94,16 @@ sub addKey { require Clone; my $new = Clone::clone( $self->mgr->currentConf ); foreach my $el (@list) { - $new->{ $el->[0] }->{ $el->[1] } = $el->[2]; + my @path = split $sep, $el->[0]; + if ( $#path == 0 ) { + $new->{ $path[0] }->{ $el->[1] } = $el->[2]; + } + elsif ( $#path == 1 ) { + $new->{ $path[0] }->{ $path[1] }->{ $el->[1] } = $el->[2]; + } + else { + die $el->[0] . " has too many levels. Aborting"; + } } return $self->_save($new); } @@ -103,11 +113,12 @@ sub delKey { unless ( @_ % 2 == 0 ) { die 'usage: "delKey (?:rootKey key)+'; } + my $sep = $self->sep; my @list; while (@_) { my $root = shift; my $key = shift; - unless ( $root =~ /$simpleHashKeys$/o ) { + unless ( $root =~ /$simpleHashKeys$/o or $root =~ /$sep/o ) { die "$root is not a simple hash. Aborting"; } push @list, [ $root, $key ]; @@ -115,7 +126,16 @@ sub delKey { require Clone; my $new = Clone::clone( $self->mgr->currentConf ); foreach my $el (@list) { - delete $new->{ $el->[0] }->{ $el->[1] }; + my @path = split $sep, $el->[0]; + if ( $#path == 0 ) { + delete $new->{ $path[0] }->{ $el->[1] }; + } + elsif ( $#path == 1 ) { + delete $new->{ $path[0] }->{ $path[1] }->{ $el->[1] }; + } + else { + die $el->[0] . " has too many levels. Aborting"; + } } return $self->_save($new); }