]> git.donarmstrong.com Git - bin.git/commitdiff
* add update option, dump, and document options in db-hash
authorDon Armstrong <don@donarmstrong.com>
Mon, 1 Nov 2010 23:15:14 +0000 (23:15 +0000)
committerDon Armstrong <don@donarmstrong.com>
Mon, 1 Nov 2010 23:15:14 +0000 (23:15 +0000)
db-hash

diff --git a/db-hash b/db-hash
index 28f820918f004f67ff4acfb3040a790341d1a2a8..db7066e6110fc4796b7b4d4a4c2417ee36e4f400 100755 (executable)
--- a/db-hash
+++ b/db-hash
@@ -24,6 +24,9 @@ db-hash - create a database and query using it
  Options:
   --create, -c create dbname
   --update, -u update dbname, create if it doesn't exist
+  --dump, -D dump dbname
+  --missing-newline, -n output a newline for unhashed keys
+  --include-key, -k output the key as well as the value
   --debug, -d debugging level (Default 0)
   --help, -h display this help
   --man, -m display manual
@@ -32,6 +35,28 @@ db-hash - create a database and query using it
 
 =over
 
+=item B<--create,-c>
+
+Create a database
+
+=item B<--update,-u>
+
+Update a database; replaces all keys with the values loaded from the
+file, but keeps existing keys which were not replaced
+
+=item B<--dump,-D>
+
+Dump database to stdout
+
+=item B<--include-key,-k>
+
+Output the key as well as the value. (Off by default)
+
+=item B<--missing-newline,-n>
+
+If a key doesn't exist in the database, output a newline. (Forced on
+when --include-key set)
+
 =item B<--debug, -d>
 
 Debug verbosity. (Default 0)
@@ -61,10 +86,16 @@ my %options = (debug           => 0,
               man             => 0,
               create          => 0,
               update          => 0,
+              dump            => 0,
+#             include_key     => 0,
+              missing_newline => 1,
               );
 
 GetOptions(\%options,
           'create|c','update|u',
+          'dump|D',
+          'include_key|include-key|k!',
+          'missing_newline|missing-newline|n!',
           'debug|d+','help|h|?','man|m');
 
 pod2usage() if $options{help};
@@ -94,8 +125,21 @@ else {
        die "Unable to open $db for writing: $!";
 }
 
+if (not defined $options{include_key}) {
+    $options{include_key} = $options{dump} ? 0 : 1;
+}
+
+
 if ($options{update}) {
-    die "update currently not supported"
+    my %fast_db;
+    while (<STDIN>) {
+       chomp;
+       my ($key,@val) = split /\t/;
+       $fast_db{$key} = [make_list($fast_db{$key} // [],@val)];
+    }
+    for my $key (keys %fast_db) {
+       $t_db{$key} = $fast_db{$key};
+    }
 }
 elsif ($options{create}) {
     my %fast_db;
@@ -108,12 +152,36 @@ elsif ($options{create}) {
        $t_db{$key} = $fast_db{$key};
     }
 }
+elsif ($options{dump}) {
+    for my $key (keys %t_db) {
+       print "$key:".join("\t",make_list($t_db{$key}))."\n";
+    }
+}
 else {
-    if (@keys) {
-       print map {"$_\n"} map {make_list($t_db{$_} // [])} @keys;
+    if (!@keys) {
+       output_keys(<STDIN>);
     }
     else {
-       print map {"$_\n"} map {make_list($t_db{$_} // [])} <STDIN>;
+       output_keys(@keys);
+    }
+}
+
+sub output_keys {
+    my @temp = @_;
+    for my $key (@temp) {
+       if (not exists $t_db{$key}) {
+           chomp $key;
+       }
+       if ($options{include_key}) {
+           print $key."\t";
+       }
+       if (exists $t_db{$key}) {
+           print join("\t",make_list($t_db{$key}));
+           print "\n";
+       }
+       elsif ($options{missing_newline}) {
+           print "\n";
+       }
     }
 }