Browse Source

Rather ugly show command

Serj Kalichev 1 year ago
parent
commit
8be4a0f60f
4 changed files with 80 additions and 0 deletions
  1. 1 0
      src/plugin.c
  2. 70 0
      src/syms.c
  3. 1 0
      src/syms.h
  4. 8 0
      xml/sysrepo.xml

+ 1 - 0
src/plugin.c

@@ -71,6 +71,7 @@ int kplugin_sysrepo_init(kcontext_t *context)
 	kplugin_add_syms(plugin, ksym_new("srp_insert", srp_insert));
 	kplugin_add_syms(plugin, ksym_new("srp_commit", srp_commit));
 	kplugin_add_syms(plugin, ksym_new("srp_rollback", srp_rollback));
+	kplugin_add_syms(plugin, ksym_new("srp_show", srp_show));
 
 	return 0;
 }

+ 70 - 0
src/syms.c

@@ -19,6 +19,7 @@
 #include <sysrepo.h>
 #include <sysrepo/xpath.h>
 
+#include "sr_copypaste.h"
 #include "pline.h"
 #include "syms.h"
 
@@ -784,3 +785,72 @@ err:
 
 	return ret;
 }
+
+
+int srp_show(kcontext_t *context)
+{
+	int ret = -1;
+	faux_argv_t *args = NULL;
+	pline_t *pline = NULL;
+	sr_conn_ctx_t *conn = NULL;
+	sr_session_ctx_t *sess = NULL;
+	pexpr_t *expr = NULL;
+	faux_argv_t *cur_path = NULL;
+	kplugin_t *plugin = NULL;
+	sr_data_t *data = NULL;
+	struct ly_out *out = NULL;
+	struct lyd_node *child = NULL;
+
+	assert(context);
+
+	if (sr_connect(SR_CONN_DEFAULT, &conn))
+		return -1;
+	if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
+		sr_disconnect(conn);
+		return -1;
+	}
+
+	plugin = kcontext_plugin(context);
+	cur_path = (faux_argv_t *)kplugin_udata(plugin);
+	args = param2argv(cur_path, kcontext_pargv(context), "path");
+	pline = pline_parse(sess, args, 0);
+	faux_argv_free(args);
+
+	if (pline->invalid) {
+		fprintf(stderr, "Invalid 'show' request\n");
+		goto err;
+	}
+
+	if (faux_list_len(pline->exprs) > 1) {
+		fprintf(stderr, "Can't process more than one object\n");
+		goto err;
+	}
+
+	expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
+	if (!(expr->pat & PT_EDIT)) {
+		fprintf(stderr, "Illegal expression for 'show' operation\n");
+		goto err;
+	}
+
+	if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
+		fprintf(stderr, "Can't get specified subtree\n");
+		goto err;
+	}
+
+	ly_out_new_file(stdout, &out);
+	lyd_print_tree(out, data->tree, LYD_XML, 0);
+	ly_out_free(out, NULL, 0);
+//	child = lyd_child(data->tree);
+//	if (child) {
+//		ly_out_new_file(stdout, &out);
+//		lyd_print_all(out, child, LYD_XML, 0);
+//	}
+	sr_release_data(data);
+
+	ret = 0;
+err:
+	pline_free(pline);
+	sr_disconnect(conn);
+
+	return ret;
+}

+ 1 - 0
src/syms.h

@@ -36,6 +36,7 @@ int srp_up(kcontext_t *context);
 int srp_insert(kcontext_t *context);
 int srp_commit(kcontext_t *context);
 int srp_rollback(kcontext_t *context);
+int srp_show(kcontext_t *context);
 
 C_DECL_END
 

+ 8 - 0
xml/sysrepo.xml

@@ -168,6 +168,14 @@
 		<ACTION sym="srp_rollback@sysrepo"/>
 	</ENTRY>
 
+	<ENTRY name="show" help="Show data hierarchy" mode="sequence">
+		<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
+		<ENTRY name="path" min="0" max="100">
+			<ENTRY name="PLINE_EDIT" purpose="ptype" ref="/PLINE_EDIT"/>
+		</ENTRY>
+		<ACTION sym="srp_show@sysrepo"/>
+	</ENTRY>
+
 </ENTRY>
 
 </KLISH>