X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=ovsdb%2Fovsdb-client.c;h=da6a6d6d79f040ebde865a35992760f5e672bce6;hb=7e71ab66e9a164359c0a5f1d6a20bb2efc15500f;hp=8502f19bec14464ffc9c6996a404036bfdae5fe3;hpb=772387d560e565abf212d5863698a6834f501639;p=openvswitch diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c index 8502f19b..da6a6d6d 100644 --- a/ovsdb/ovsdb-client.c +++ b/ovsdb/ovsdb-client.c @@ -49,7 +49,8 @@ VLOG_DEFINE_THIS_MODULE(ovsdb_client); static enum { FMT_TABLE, /* Textual table. */ FMT_HTML, /* HTML table. */ - FMT_CSV /* Comma-separated lines. */ + FMT_CSV, /* Comma-separated lines. */ + FMT_JSON /* JSON. */ } output_format; /* --no-headings: Whether table output should include headings. */ @@ -119,6 +120,8 @@ parse_options(int argc, char *argv[]) output_format = FMT_HTML; } else if (!strcmp(optarg, "csv")) { output_format = FMT_CSV; + } else if (!strcmp(optarg, "json")) { + output_format = FMT_JSON; } else { ovs_fatal(0, "unknown output format \"%s\"", optarg); } @@ -179,6 +182,9 @@ usage(void) " list databases available on SERVER\n" "\n get-schema SERVER DATABASE\n" " retrieve schema for DATABASE from SERVER\n" + "\n get-schema-version SERVER DATABASE\n" + " retrieve schema for DATABASE from SERVER and report only its\n" + " version number on stdout\n" "\n list-tables SERVER DATABASE\n" " list tables for DATABASE on SERVER\n" "\n list-columns SERVER DATABASE [TABLE]\n" @@ -196,7 +202,8 @@ usage(void) stream_usage("SERVER", true, true, true); printf("\nOutput formatting options:\n" " -f, --format=FORMAT set output formatting to FORMAT\n" - " (\"table\", \"html\", or \"csv\"\n" + " (\"table\", \"html\", \"csv\", " + "or \"json\")\n" " --no-headings omit table heading row\n" " --pretty pretty-print JSON in output"); daemon_usage(); @@ -676,6 +683,46 @@ table_print_csv__(const struct table *table) } } +static void +table_print_json__(const struct table *table) +{ + struct json *json, *headings, *data; + size_t x, y; + char *s; + + json = json_object_create(); + if (table->caption) { + json_object_put_string(json, "caption", table->caption); + } + + headings = json_array_create_empty(); + for (x = 0; x < table->n_columns; x++) { + const struct column *column = &table->columns[x]; + json_array_add(headings, json_string_create(column->heading)); + } + json_object_put(json, "headings", headings); + + data = json_array_create_empty(); + for (y = 0; y < table->n_rows; y++) { + struct json *row = json_array_create_empty(); + for (x = 0; x < table->n_columns; x++) { + const struct cell *cell = table_cell__(table, y, x); + if (cell->text) { + json_array_add(row, json_string_create(cell->text)); + } else { + json_array_add(row, json_clone(cell->json)); + } + } + json_array_add(data, row); + } + json_object_put(json, "data", data); + + s = json_to_string(json, json_flags); + json_destroy(json); + puts(s); + free(s); +} + static void table_print(const struct table *table) { @@ -691,6 +738,10 @@ table_print(const struct table *table) case FMT_CSV: table_print_csv__(table); break; + + case FMT_JSON: + table_print_json__(table); + break; } } @@ -733,6 +784,14 @@ do_get_schema(int argc OVS_UNUSED, char *argv[]) ovsdb_schema_destroy(schema); } +static void +do_get_schema_version(int argc OVS_UNUSED, char *argv[]) +{ + struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]); + puts(schema->version); + ovsdb_schema_destroy(schema); +} + static void do_list_tables(int argc OVS_UNUSED, char *argv[]) { @@ -1296,6 +1355,7 @@ do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) static const struct command all_commands[] = { { "list-dbs", 1, 1, do_list_dbs }, { "get-schema", 2, 2, do_get_schema }, + { "get-schema-version", 2, 2, do_get_schema_version }, { "list-tables", 2, 2, do_list_tables }, { "list-columns", 2, 3, do_list_columns }, { "transact", 2, 2, do_transact },