From 2a9a5675ce1e15e4576db4a70429612c478a892a Mon Sep 17 00:00:00 2001 From: cjhardee Date: Sat, 24 Apr 2010 02:08:21 +0000 Subject: [PATCH] This addresses [ xcat-Feature Requests-2991347 ] chtab should be able to append a new value to an attribute allows += to be used to append values in a field instead of only being able to replace with = fixed the version option git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@5880 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- xCAT-server/sbin/chtab | 56 +++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/xCAT-server/sbin/chtab b/xCAT-server/sbin/chtab index a66f345ed..8c9c784e0 100755 --- a/xCAT-server/sbin/chtab +++ b/xCAT-server/sbin/chtab @@ -50,7 +50,8 @@ if ($::HELP) { &usage; exit(0);} # display the version statement if -v or --verison is specified if ($::VERSION) { - print "chtab version 2.0\n"; + my $version = xCAT::Utils->Version(); + print "chtab $version\n"; exit(0); } @@ -101,17 +102,50 @@ else { my $table; my $column; my $value; + ($table,$temp) = split('\.',$_,2); - ($column,$value) = split("=",$temp,2); - unless ($tables{$table}) { - my $tab = xCAT::Table->new($table,-create => 1,-autocommit => 0); - if ($tab) { - $tables{$table}=$tab; - } else { - print "Table $table does not exist.\n"; - exit(1); - } - } + + #try to create the entry if it doesn't exist + unless ($tables{$table}) { + my $tab = xCAT::Table->new($table,-create => 1,-autocommit => 0); + if ($tab) { + $tables{$table}=$tab; + } else { + print "Table $table does not exist.\n"; + exit(1); + } + } + + #splice assignment + if(grep /\+=/, $temp) { + ($column,$value) = split('\+=',$temp,2); + + #grab the current values to check against + my ($attrHash) = $tables{$table}->getAttribs(\%keyhash, $column); + my @existing = split(",",$attrHash->{$column}); + + #if it has values, merge the new and old ones together so no dupes + if (@existing) { + my @values = split(",",$value); + my %seen = (); + my @uniq = (); + my $item; + + foreach $item (@existing,@values) { + unless ($seen{$item}) { + # if we get here, we have not seen it before + $seen{$item} = 1; + push(@uniq, $item); + } + } + $value = join(",",@uniq); + } + } + #normal non-splicing assignment + else { + ($column,$value) = split("=",$temp,2); + } + $tableupdates{$table}{$column}=$value; }