From eeecbb1672352c4dc15af2ca345f2bb5cc5a5f66 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Mon, 26 Sep 2016 10:07:26 +0200 Subject: [PATCH] sysdb: Don't pretty-print JSON in non-interactive mode. This is a work-around to fix integration tests (which expect the entire output on one line). A better solution will rely on configurable output formats. Also, disable interactive mode when using -c options. It's not interactive after all :-) --- src/tools/sysdb/command.c | 20 ++++++++++---------- src/tools/sysdb/command.h | 2 +- src/tools/sysdb/input.c | 2 +- src/tools/sysdb/json.c | 8 +++++++- src/tools/sysdb/json.h | 4 +++- src/tools/sysdb/main.c | 17 ++++++++++------- 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/tools/sysdb/command.c b/src/tools/sysdb/command.c index 4de2b44..dd8b298 100644 --- a/src/tools/sysdb/command.c +++ b/src/tools/sysdb/command.c @@ -48,7 +48,7 @@ #include static void -ok_printer(sdb_strbuf_t *buf) +ok_printer(sdb_input_t __attribute__((unused)) *input, sdb_strbuf_t *buf) { const char *msg = sdb_strbuf_string(buf); if (msg && *msg) @@ -58,7 +58,7 @@ ok_printer(sdb_strbuf_t *buf) } /* ok_printer */ static void -log_printer(sdb_strbuf_t *buf) +log_printer(sdb_input_t __attribute__((unused)) *input, sdb_strbuf_t *buf) { uint32_t prio = 0; @@ -73,7 +73,7 @@ log_printer(sdb_strbuf_t *buf) } /* log_printer */ static void -data_printer(sdb_strbuf_t *buf) +data_printer(sdb_input_t __attribute__((unused)) *input, sdb_strbuf_t *buf) { size_t len = sdb_strbuf_len(buf); @@ -90,14 +90,14 @@ data_printer(sdb_strbuf_t *buf) /* At the moment, we don't care about the result type. We simply print the * result without further parsing it. */ sdb_strbuf_skip(buf, 0, sizeof(uint32_t)); - if (sdb_json_print(buf)) + if (sdb_json_print(input, buf)) sdb_log(SDB_LOG_ERR, "Failed to print result"); printf("\n"); } /* data_printer */ static struct { int status; - void (*printer)(sdb_strbuf_t *); + void (*printer)(sdb_input_t *, sdb_strbuf_t *); } response_printers[] = { { SDB_CONNECTION_OK, ok_printer }, { SDB_CONNECTION_LOG, log_printer }, @@ -118,7 +118,7 @@ clear_query(sdb_input_t *input) */ int -sdb_command_print_reply(sdb_client_t *client) +sdb_command_print_reply(sdb_input_t *input) { sdb_strbuf_t *recv_buf; const char *result; @@ -131,10 +131,10 @@ sdb_command_print_reply(sdb_client_t *client) if (! recv_buf) return -1; - if (sdb_client_recv(client, &rcode, recv_buf) < 0) + if (sdb_client_recv(input->client, &rcode, recv_buf) < 0) rcode = UINT32_MAX; - if (sdb_client_eof(client)) { + if (sdb_client_eof(input->client)) { sdb_strbuf_destroy(recv_buf); return -1; } @@ -144,7 +144,7 @@ sdb_command_print_reply(sdb_client_t *client) for (i = 0; i < SDB_STATIC_ARRAY_LEN(response_printers); ++i) { if (status == response_printers[i].status) { - response_printers[i].printer(recv_buf); + response_printers[i].printer(input, recv_buf); sdb_strbuf_destroy(recv_buf); return status; } @@ -203,7 +203,7 @@ sdb_command_exec(sdb_input_t *input) /* The server may send back log messages but will eventually reply to the * query, which is either DATA or ERROR. */ while (42) { - int status = sdb_command_print_reply(input->client); + int status = sdb_command_print_reply(input); if (status < 0) { sdb_log(SDB_LOG_ERR, "Failed to read reply from server"); break; diff --git a/src/tools/sysdb/command.h b/src/tools/sysdb/command.h index 04ad5e1..b2e5fa7 100644 --- a/src/tools/sysdb/command.h +++ b/src/tools/sysdb/command.h @@ -40,7 +40,7 @@ * - a positive value if the server returned an error */ int -sdb_command_print_reply(sdb_client_t *client); +sdb_command_print_reply(sdb_input_t *input); /* * sdb_command_exec: diff --git a/src/tools/sysdb/input.c b/src/tools/sysdb/input.c index f0f86ea..2a2d170 100644 --- a/src/tools/sysdb/input.c +++ b/src/tools/sysdb/input.c @@ -203,7 +203,7 @@ input_readline(void) /* some response / error message from the server pending */ /* XXX: clear current line */ printf("\n"); - sdb_command_print_reply(sysdb_input->client); + sdb_command_print_reply(sysdb_input); if (sdb_client_eof(sysdb_input->client)) { rl_callback_handler_remove(); diff --git a/src/tools/sysdb/json.c b/src/tools/sysdb/json.c index 703b22e..1792cda 100644 --- a/src/tools/sysdb/json.c +++ b/src/tools/sysdb/json.c @@ -107,7 +107,7 @@ printer(void __attribute__((unused)) *ctx, const char *str, size_t len) */ int -sdb_json_print(sdb_strbuf_t *buf) +sdb_json_print(sdb_input_t *input, sdb_strbuf_t *buf) { #ifdef HAVE_LIBYAJL const unsigned char *json; @@ -119,6 +119,12 @@ sdb_json_print(sdb_strbuf_t *buf) int ret = 0; + if (!input->interactive) { + /* no formatting */ + printf("%s\n", sdb_strbuf_string(buf)); + return 0; + } + gen = yajl_gen_alloc(/* alloc_funcs */ NULL); if (! gen) return -1; diff --git a/src/tools/sysdb/json.h b/src/tools/sysdb/json.h index 9fc1a70..08def61 100644 --- a/src/tools/sysdb/json.h +++ b/src/tools/sysdb/json.h @@ -25,6 +25,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "tools/sysdb/input.h" + #include "utils/strbuf.h" #ifndef SYSDB_JSON_H @@ -36,7 +38,7 @@ * printed to the standard output channel. */ int -sdb_json_print(sdb_strbuf_t *buf); +sdb_json_print(sdb_input_t *input, sdb_strbuf_t *buf); #endif /* SYSDB_JSON_H */ diff --git a/src/tools/sysdb/main.c b/src/tools/sysdb/main.c index f502027..b45ef87 100644 --- a/src/tools/sysdb/main.c +++ b/src/tools/sysdb/main.c @@ -164,7 +164,7 @@ exit_version(void) } /* exit_version */ static int -execute_commands(sdb_client_t *client, sdb_llist_t *commands) +execute_commands(sdb_input_t *input, sdb_llist_t *commands) { sdb_llist_iter_t *iter; int status = 0; @@ -178,7 +178,7 @@ execute_commands(sdb_client_t *client, sdb_llist_t *commands) while (sdb_llist_iter_has_next(iter)) { sdb_object_t *obj = sdb_llist_iter_get_next(iter); - if (sdb_client_send(client, SDB_CONNECTION_QUERY, + if (sdb_client_send(input->client, SDB_CONNECTION_QUERY, (uint32_t)strlen(obj->name), obj->name) <= 0) { sdb_log(SDB_LOG_ERR, "Failed to send command '%s' to server", obj->name); @@ -190,7 +190,7 @@ execute_commands(sdb_client_t *client, sdb_llist_t *commands) * but eventually see the reply to the query, which is either DATA or * ERROR. */ while (42) { - status = sdb_command_print_reply(client); + status = sdb_command_print_reply(input); if (status < 0) { sdb_log(SDB_LOG_ERR, "Failed to read reply from server"); break; @@ -307,6 +307,9 @@ main(int argc, char **argv) sdb_input_reset(&input); exit(1); } + input.input = sdb_strbuf_create(2048); + sdb_input_init(&input); + canonicalize_ssl_options(); if (sdb_client_set_ssl_options(input.client, &ssl_options)) { sdb_log(SDB_LOG_ERR, "Failed to apply SSL options"); @@ -322,7 +325,9 @@ main(int argc, char **argv) } if (commands) { - int status = execute_commands(input.client, commands); + int status; + input.interactive = 0; + status = execute_commands(&input, commands); sdb_llist_destroy(commands); sdb_input_reset(&input); if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA)) @@ -353,14 +358,12 @@ main(int argc, char **argv) } } - input.input = sdb_strbuf_create(2048); - sdb_input_init(&input); sdb_input_mainloop(); sdb_client_shutdown(input.client, SHUT_WR); while (! sdb_client_eof(input.client)) { /* wait for remaining data to arrive */ - sdb_command_print_reply(input.client); + sdb_command_print_reply(&input); } if (hist_file[0] != '\0') { -- 2.30.2