[6.0] c6c636217 Make help -j output real JSON, and push backend.list -j into the schema intended for JSON output.

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Thu Aug 16 08:52:57 UTC 2018


commit c6c6362170ddbe918ed1c2d2702ce77b2d76f987
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sun May 13 20:52:10 2018 +0000

    Make help -j output real JSON, and push backend.list -j into
    the schema intended for JSON output.
    
    Conflicts:
            bin/varnishd/cache/cache_backend.c
            bin/varnishd/cache/cache_backend_probe.c
            bin/varnishd/cache/cache_director.c
            bin/varnishtest/tests/d00005.vtc
            bin/varnishtest/tests/v00014.vtc
    
    Only help -j is fixed, the backend changes are left out.

diff --git a/bin/varnishtest/tests/b00008.vtc b/bin/varnishtest/tests/b00008.vtc
index e536a81ee..1ec361155 100644
--- a/bin/varnishtest/tests/b00008.vtc
+++ b/bin/varnishtest/tests/b00008.vtc
@@ -26,7 +26,7 @@ varnish v1 -start
 
 varnish v1 -cliok "help"
 
-varnish v1 -cliok "help -j"
+varnish v1 -clijson "help -j"
 
 varnish v1 -clierr 106 "param.set waiter HASH(0x8839c4c)"
 
diff --git a/include/vcli_serve.h b/include/vcli_serve.h
index 7bc04c3da..8378993c4 100644
--- a/include/vcli_serve.h
+++ b/include/vcli_serve.h
@@ -85,7 +85,8 @@ int VCLI_Overflow(struct cli *cli);
 void VCLI_Out(struct cli *cli, const char *fmt, ...) v_printflike_(2, 3);
 void VCLI_Quote(struct cli *cli, const char *str);
 void VCLI_JSON_str(struct cli *cli, const char *str);
-void VCLI_JSON_ver(struct cli *cli, unsigned ver, const char * const * av);
+void VCLI_JSON_begin(struct cli *cli, unsigned ver, const char * const * av);
+void VCLI_JSON_end(struct cli *cli);
 void VCLI_SetResult(struct cli *cli, unsigned r);
 
 typedef int cls_cb_f(void *priv);
diff --git a/lib/libvarnish/vcli_serve.c b/lib/libvarnish/vcli_serve.c
index 67c681642..5171d3173 100644
--- a/lib/libvarnish/vcli_serve.c
+++ b/lib/libvarnish/vcli_serve.c
@@ -159,26 +159,35 @@ VCLS_func_help_json(struct cli *cli, const char * const *av, void *priv)
 	cs = cli->cls;
 	CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
 
-	VCLI_JSON_ver(cli, 1, av);
+	VCLI_JSON_begin(cli, 1, av);
 	VTAILQ_FOREACH(clp, &cs->funcs, list) {
 		if (clp->auth > cli->auth)
 			continue;
-		VCLI_Out(cli, ",\n  {");
-		VCLI_Out(cli, "\n  \"request\": ");
+		VCLI_Out(cli, ",\n  {\n");
+		VSB_indent(cli->sb, 2);
+		VCLI_Out(cli, "\"request\": ");
 		VCLI_JSON_str(cli, clp->desc->request);
-		VCLI_Out(cli, ",\n  \"syntax\": ");
+		VCLI_Out(cli, ",\n");
+		VCLI_Out(cli, "\"syntax\": ");
 		VCLI_JSON_str(cli, clp->desc->syntax);
-		VCLI_Out(cli, ",\n  \"help\": ");
+		VCLI_Out(cli, ",\n");
+		VCLI_Out(cli, "\"help\": ");
 		VCLI_JSON_str(cli, clp->desc->help);
-		VCLI_Out(cli, ",\n  \"minarg\": %d", clp->desc->minarg);
-		VCLI_Out(cli, ", \"maxarg\": %d", clp->desc->maxarg);
-		VCLI_Out(cli, ", \"flags\": ");
+		VCLI_Out(cli, ",\n");
+		VCLI_Out(cli, "\"minarg\": %d", clp->desc->minarg);
+		VCLI_Out(cli, ",\n");
+		VCLI_Out(cli, "\"maxarg\": %d", clp->desc->maxarg);
+		VCLI_Out(cli, ",\n");
+		VCLI_Out(cli, "\"flags\": ");
 		VCLI_JSON_str(cli, clp->flags);
-		VCLI_Out(cli, ", \"json\": %s",
+		VCLI_Out(cli, ",\n");
+		VCLI_Out(cli, "\"json\": %s",
 		    clp->jsonfunc == NULL ? "false" : "true");
-		VCLI_Out(cli, "\n  }");
+		VCLI_Out(cli, "\n");
+		VSB_indent(cli->sb, -2);
+		VCLI_Out(cli, "}");
 	}
-	VCLI_Out(cli, "\n]\n");
+	VCLI_JSON_end(cli);
 }
 
 /*--------------------------------------------------------------------
@@ -641,12 +650,14 @@ VCLI_JSON_str(struct cli *cli, const char *s)
 {
 
 	CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
+	VSB_putc(cli->sb, '"');
 	VSB_quote(cli->sb, s, -1, VSB_QUOTE_JSON);
+	VSB_putc(cli->sb, '"');
 }
 
 /*lint -e{818} cli could be const */
 void
-VCLI_JSON_ver(struct cli *cli, unsigned ver, const char * const * av)
+VCLI_JSON_begin(struct cli *cli, unsigned ver, const char * const * av)
 {
 	int i;
 
@@ -658,6 +669,15 @@ VCLI_JSON_ver(struct cli *cli, unsigned ver, const char * const * av)
 			VCLI_Out(cli, ", ");
 	}
 	VCLI_Out(cli, "]");
+	VSB_indent(cli->sb, 2);
+}
+
+void
+VCLI_JSON_end(struct cli *cli)
+{
+	VSB_indent(cli->sb, -2);
+	VCLI_Out(cli, "\n");
+	VCLI_Out(cli, "]\n");
 }
 
 /*lint -e{818} cli could be const */


More information about the varnish-commit mailing list