ovs-vsctl: Add --if-exists option to "get" command, for map columns.
authorBen Pfaff <blp@nicira.com>
Fri, 29 Jan 2010 00:03:52 +0000 (16:03 -0800)
committerBen Pfaff <blp@nicira.com>
Fri, 29 Jan 2010 00:10:12 +0000 (16:10 -0800)
utilities/ovs-vsctl.8.in
utilities/ovs-vsctl.c

index d80310b..84565b9 100644 (file)
@@ -459,11 +459,16 @@ records are specified, lists all the records in \fItable\fR.
 The UUIDs shown for rows created in the same \fBovs\-vsctl\fR
 invocation will be wrong.
 .
-.IP "\fBget \fItable record column\fR[\fB:\fIkey\fR]..."
+.IP "[\fB\-\-if\-exists\fR] \fBget \fItable record column\fR[\fB:\fIkey\fR]..."
 Prints the value of each specified \fIcolumn\fR in the given
 \fIrecord\fR in \fItable\fR.  For map columns, a \fIkey\fR may
 optionally be specified, in which case the value associated with
 \fIkey\fR in the column is printed, instead of the entire map.
+.IP
+For a map column, without \fB\-\-if\-exists\fR it is an error if
+\fIkey\fR does not exist; with it, a blank line is printed.  If
+\fIcolumn\fR is not a map column or if \fIkey\fR is not specified,
+\fB\-\-if\-exists\fR has no effect.
 .
 .IP "[\fB\-\-force\fR] \fBset \fItable record column\fR[\fB:\fIkey\fR]\fB=\fIvalue\fR..."
 Sets the value of each specified \fIcolumn\fR in the given
index dcc4927..e180af4 100644 (file)
@@ -1931,6 +1931,7 @@ error:
 static void
 cmd_get(struct vsctl_context *ctx)
 {
+    bool if_exists = shash_find(&ctx->options, "--if-exists");
     const char *table_name = ctx->argv[1];
     const char *record_id = ctx->argv[2];
     const struct vsctl_table_class *table;
@@ -1965,14 +1966,15 @@ cmd_get(struct vsctl_context *ctx)
             idx = ovsdb_datum_find_key(&datum, &key,
                                        column->idl->type.key_type);
             if (idx == UINT_MAX) {
-                ovs_fatal(0, "no key %s in %s record \"%s\" column %s",
-                          key_string, table_name, record_id,
-                          column->idl->name);
-
+                if (!if_exists) {
+                    ovs_fatal(0, "no key \"%s\" in %s record \"%s\" column %s",
+                              key_string, table->class->name, record_id,
+                              column->idl->name);
+                }
+            } else {
+                ovsdb_atom_to_string(&datum.values[idx],
+                                     column->idl->type.value_type, out);
             }
-            ovsdb_atom_to_string(&datum.values[idx],
-                                 column->idl->type.value_type, out);
-
             ovsdb_atom_destroy(&key, column->idl->type.key_type);
         } else {
             ovsdb_datum_to_string(&datum, &column->idl->type, out);
@@ -2607,7 +2609,7 @@ static const struct vsctl_command_syntax all_commands[] = {
     {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
 
     /* Parameter commands. */
-    {"get", 3, INT_MAX, cmd_get, NULL, ""},
+    {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
     {"list", 1, INT_MAX, cmd_list, NULL, ""},
     {"set", 3, INT_MAX, cmd_set, NULL, "--force"},
     {"add", 4, INT_MAX, cmd_add, NULL, "--force"},