Browse Source

srp_load: Add --current-path option

Serj Kalichev 4 months ago
parent
commit
b883ebf75c
4 changed files with 36 additions and 15 deletions
  1. 22 6
      bin/srp_load.c
  2. 4 3
      src/klish_plugin_sysrepo.h
  3. 4 3
      src/pline.c
  4. 6 3
      src/syms.c

+ 22 - 6
bin/srp_load.c

@@ -21,10 +21,11 @@
 #define DEFAULT_DATASTORE SR_DS_CANDIDATE
 
 typedef struct cmd_opts_s {
+	char *cfg; // Configuration options
+	char *file; // File to load
+	char *user; // NACM user
+	char *current_path; // Current sysrepo path
 	bool_t verbose;
-	char *cfg;
-	char *file;
-	char *user;
 	bool_t stop_on_error;
 	sr_datastore_t datastore;
 } cmd_opts_t;
@@ -43,6 +44,7 @@ int main(int argc, char **argv)
 	pline_opts_t opts;
 	cmd_opts_t *cmd_opts = NULL;
 	int fd = STDIN_FILENO;
+	faux_argv_t *cur_path = NULL;
 
 	cmd_opts = cmd_opts_init();
 	if (cmd_opts_parse(argc, argv, cmd_opts) < 0) {
@@ -57,12 +59,18 @@ int main(int argc, char **argv)
 			goto out;
 		}
 	}
-
 	pline_opts_init(&opts);
-	ret = srp_mass_set(fd, cmd_opts->datastore, &opts,
-		cmd_opts->user, cmd_opts->stop_on_error);
+	if (cmd_opts->current_path) {
+		cur_path = faux_argv_new();
+		faux_argv_parse(cur_path, cmd_opts->current_path);
+	}
+
+	ret = srp_mass_set(fd, cmd_opts->datastore, cur_path,
+		&opts, cmd_opts->user, cmd_opts->stop_on_error);
 
 out:
+	if (cur_path)
+		faux_argv_free(cur_path);
 	if (cmd_opts->file)
 		close(fd);
 	cmd_opts_free(cmd_opts);
@@ -85,6 +93,7 @@ static cmd_opts_t *cmd_opts_init(void)
 	opts->file = NULL;
 	opts->user = NULL;
 	opts->datastore = DEFAULT_DATASTORE;
+	opts->current_path = NULL;
 
 	return opts;
 }
@@ -97,6 +106,7 @@ static void cmd_opts_free(cmd_opts_t *opts)
 	faux_str_free(opts->cfg);
 	faux_str_free(opts->file);
 	faux_str_free(opts->user);
+	faux_str_free(opts->current_path);
 
 	faux_free(opts);
 }
@@ -112,6 +122,7 @@ static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts)
 		{"user",		1, NULL, 'u'},
 		{"stop-on-error",	0, NULL, 'e'},
 		{"datastore",		1, NULL, 'd'},
+		{"current-path",	1, NULL, 'p'},
 		{NULL,			0, NULL, 0}
 	};
 
@@ -145,6 +156,10 @@ static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts)
 			if (!kly_str2ds(optarg, strlen(optarg), &opts->datastore))
 				return BOOL_FALSE;
 			break;
+		case 'p':
+			faux_str_free(opts->current_path);
+			opts->current_path = faux_str_dup(optarg);
+			break;
 		default:
 			help(-1, argv[0]);
 			_exit(-1);
@@ -194,5 +209,6 @@ static void help(int status, const char *argv0)
 		printf("\t-u <name>, --user=<name> NACM user.\n");
 		printf("\t-f <path>, --conf=<path> Config file.\n");
 		printf("\t-d <ds>, --datastore=<ds> Datastore.\n");
+		printf("\t-p <sr-path>, --current-path=<sr-path> Current sysrepo path.\n");
 	}
 }

+ 4 - 3
src/klish_plugin_sysrepo.h

@@ -182,7 +182,8 @@ C_DECL_BEGIN
 pline_t *pline_new(sr_session_ctx_t *sess);
 void pline_opts_init(pline_opts_t *opts);
 int pline_opts_parse(const char *conf, pline_opts_t *opts);
-pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, pline_opts_t *opts);
+pline_t *pline_parse(sr_session_ctx_t *sess, const faux_argv_t *argv,
+	const pline_opts_t *opts);
 pexpr_t *pline_current_expr(pline_t *pline);
 
 void pline_free(pline_t *pline);
@@ -256,8 +257,8 @@ int srp_diff(kcontext_t *context);
 int srp_deactivate(kcontext_t *context);
 
 // Service functions
-int srp_mass_set(int fd, sr_datastore_t ds, pline_opts_t *opts,
-	const char *user, bool_t stop_on_error);
+int srp_mass_set(int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
+	const pline_opts_t *opts, const char *user, bool_t stop_on_error);
 
 // Plugin's user-data service functions
 pline_opts_t *srp_udata_opts(kcontext_t *context);

+ 4 - 3
src/pline.c

@@ -502,8 +502,8 @@ static void pline_add_compl_leaf(pline_t *pline, const struct lysc_node *node,
 }
 
 
-static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
-	pline_t *pline, pline_opts_t *opts)
+static bool_t pline_parse_module(const struct lys_module *module,
+	const faux_argv_t *argv, pline_t *pline, const pline_opts_t *opts)
 {
 	faux_argv_node_t *arg = faux_argv_iter(argv);
 	const struct lysc_node *node = NULL;
@@ -866,7 +866,8 @@ static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *a
 }
 
 
-pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, pline_opts_t *opts)
+pline_t *pline_parse(sr_session_ctx_t *sess, const faux_argv_t *argv,
+	const pline_opts_t *opts)
 {
 	const struct ly_ctx *ctx = NULL;
 	struct lys_module *module = NULL;

+ 6 - 3
src/syms.c

@@ -1187,8 +1187,8 @@ int srp_compl_xpath(kcontext_t *context)
 // Function for mass config strings load. It can load stream of KPath strings
 // (without "set" command, only path and value). Function doesn't use
 // pre-connected session because it can be executed within FILTER or utility.
-int srp_mass_set(int fd, sr_datastore_t ds, pline_opts_t *opts,
-	const char *user, bool_t stop_on_error)
+int srp_mass_set(int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
+	const pline_opts_t *opts, const char *user, bool_t stop_on_error)
 {
 	int ret = -1;
 	int err = SR_ERR_OK;
@@ -1230,7 +1230,10 @@ int srp_mass_set(int fd, sr_datastore_t ds, pline_opts_t *opts,
 		pline_t *pline = NULL;
 		faux_argv_t *args = NULL;
 
-		args = faux_argv_new();
+		if (cur_path)
+			args = faux_argv_dup(cur_path);
+		else
+			args = faux_argv_new();
 		faux_argv_parse(args, line);
 		pline = pline_parse(sess, args, opts);
 		faux_argv_free(args);