Code

sysdb: If available, use YAJL to pretty-print JSON output.
[sysdb.git] / src / tools / sysdb / command.c
index 2706dd220e12fbe8e5bacb0af539b313d08a8a8a..4de2b44e4fc79245027a22c5c2932bd82b46f742 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "tools/sysdb/command.h"
 #include "tools/sysdb/input.h"
+#include "tools/sysdb/json.h"
 
 #include "frontend/proto.h"
 #include "utils/error.h"
 #include <stdlib.h>
 #include <string.h>
 
+static void
+ok_printer(sdb_strbuf_t *buf)
+{
+       const char *msg = sdb_strbuf_string(buf);
+       if (msg && *msg)
+               printf("%s\n", msg);
+       else
+               printf("OK\n");
+} /* ok_printer */
+
 static void
 log_printer(sdb_strbuf_t *buf)
 {
-       uint32_t prio = sdb_proto_unmarshal_int32(SDB_STRBUF_STR(buf));
+       uint32_t prio = 0;
 
-       if (prio == UINT32_MAX) {
+       if (sdb_proto_unmarshal_int32(SDB_STRBUF_STR(buf), &prio) < 0) {
                sdb_log(SDB_LOG_WARNING, "Received a LOG message with invalid "
                                "or missing priority");
                prio = (uint32_t)SDB_LOG_ERR;
@@ -79,13 +90,16 @@ 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));
-       printf("%s\n", sdb_strbuf_string(buf));
+       if (sdb_json_print(buf))
+               sdb_log(SDB_LOG_ERR, "Failed to print result");
+       printf("\n");
 } /* data_printer */
 
 static struct {
        int status;
        void (*printer)(sdb_strbuf_t *);
 } response_printers[] = {
+       { SDB_CONNECTION_OK,   ok_printer },
        { SDB_CONNECTION_LOG,  log_printer },
        { SDB_CONNECTION_DATA, data_printer },
 };
@@ -195,7 +209,8 @@ sdb_command_exec(sdb_input_t *input)
                        break;
                }
 
-               if ((status == SDB_CONNECTION_DATA)
+               if ((status == SDB_CONNECTION_OK)
+                               || (status == SDB_CONNECTION_DATA)
                                || (status == SDB_CONNECTION_ERROR))
                        break;
        }
@@ -203,5 +218,25 @@ sdb_command_exec(sdb_input_t *input)
        return data;
 } /* sdb_command_exec */
 
+void
+sdb_command_print_server_version(sdb_input_t *input)
+{
+       sdb_strbuf_t *buf = sdb_strbuf_create(32);
+       uint32_t code = 0, version = 0;
+       const char *extra;
+
+       if ((sdb_client_rpc(input->client, SDB_CONNECTION_SERVER_VERSION,
+                                       0, NULL, &code, buf) < 0) || (code != SDB_CONNECTION_OK))
+               return;
+       if (sdb_strbuf_len(buf) < sizeof(version))
+               return;
+
+       sdb_proto_unmarshal_int32(SDB_STRBUF_STR(buf), &version);
+       extra = sdb_strbuf_string(buf) + sizeof(version);
+       sdb_log(SDB_LOG_INFO, "SysDB server %d.%d.%d%s",
+                       SDB_VERSION_DECODE((int)version), extra);
+       sdb_strbuf_destroy(buf);
+} /* sdb_command_print_server_version */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */