Refactor of cli show commands:

- Changed `cli_show_config()`
- Removed `cli_show_config_state()`
- Replaced `cli_auto_show()` with `cli_show_auto_mode()`
This commit is contained in:
Olof hagsand 2022-09-15 17:42:57 +02:00
parent 9a224ea4a0
commit a6f67755b3
32 changed files with 706 additions and 746 deletions

View file

@ -337,201 +337,6 @@ cli_auto_top(clicon_handle h,
return retval;
}
/*! CLI callback: Working point tree show
* @param[in] h CLICON handle
* @param[in] cvv Vector of variables from CLIgen command-line
* @param[in] argv Vector of user-supplied keywords
* Format of argv:
* <treename> Name of generated cligen parse-tree, eg "datamodel"
* <dbname> "running"|"candidate"|"startup"
* <format> "text"|"xml"|"json"|"cli"|"netconf" (see format_enum)
* <pretty> true|false: pretty-print or not
* <state> true|false: also print state
* <default> Retrieval default mode: report-all, trim, explicit, report-all-tagged,
* report-all-tagged-default, report-all-tagged-strip
* <prefix> CLI prefix: to print before cli syntax output
* @see cli_show_auto
* @note default mods accorsing to RFC6243 + two extra modes based on report-all-tagged:
* report-all-tagged-default Strip "default" attribute (=report-all xxx)
* report-all-tagged-strip Strip "default" attribute and all nodes tagged with it (=trim)
* XXX Merge with cli_show_auto
*/
int
cli_auto_show(clicon_handle h,
cvec *cvv,
cvec *argv)
{
int retval = -1;
char *treename;
char *db;
char *api_path = NULL;
char *formatstr;
enum format_enum format;
pt_head *ph;
char *xpath = NULL;
cxobj *xp;
cvec *nsc = NULL;
yang_stmt *yspec;
cxobj *xerr;
cxobj *xt = NULL;
cxobj **vec = NULL;
size_t veclen;
int i;
int skiproot;
int pretty;
char *prefix = NULL;
int state;
cg_var *boolcv = NULL;
char *defaultstr = NULL; /* with extended tagged modes */
char *withdefaultstr = NULL; /* RFC 6243 modes */
if (cvec_len(argv) < 5 || cvec_len(argv) > 7){
clicon_err(OE_PLUGIN, EINVAL, "Usage: <treename> <database> <format> <pretty> <state> [<default> <cli-prefix>].");
goto done;
}
/* First argv argument: treename */
treename = cv_string_get(cvec_i(argv, 0));
/* Second argv argument: Database */
db = cv_string_get(cvec_i(argv, 1));
/* Third format: output format */
formatstr = cv_string_get(cvec_i(argv, 2));
if ((int)(format = format_str2int(formatstr)) < 0){
clicon_err(OE_PLUGIN, 0, "Not valid format: %s", formatstr);
goto done;
}
/* Fourth: pretty-print */
if ((boolcv = cv_new(CGV_BOOL)) == NULL){
clicon_err(OE_UNIX, errno, "cv_new");
goto done;
}
if (cv_parse(cv_string_get(cvec_i(argv, 3)), boolcv) < 0){
clicon_err(OE_UNIX, errno, "Parse boolean %s", cv_string_get(cvec_i(argv, 3)));
goto done;
}
pretty = cv_bool_get(boolcv);
/* Fifth: state */
if (cv_parse(cv_string_get(cvec_i(argv, 4)), boolcv) < 0){
clicon_err(OE_UNIX, errno, "Parse boolean %s", cv_string_get(cvec_i(argv, 4)));
goto done;
}
state = cv_bool_get(boolcv);
/* Sixth: default */
if (cvec_len(argv) > 5) {
defaultstr = cv_string_get(cvec_i(argv, 5));
/* From extended to RFC6243 withdefault modes */
if (strcmp(defaultstr, "report-all-tagged-strip") == 0)
withdefaultstr = "report-all-tagged";
else if (strcmp(defaultstr, "report-all-tagged-default") == 0)
withdefaultstr = "report-all-tagged";
else if (strcmp(defaultstr, "report-all") != 0 &&
strcmp(defaultstr, "trim") != 0 &&
strcmp(defaultstr, "explicit") != 0 &&
strcmp(defaultstr, "report-all-tagged") != 0){
clicon_err(OE_YANG, EINVAL, "Unexpected with-default option: %s", defaultstr);
goto done;
}
else
withdefaultstr = defaultstr;
}
/* Seventh: cli prefix */
if (cvec_len(argv) > 6) {
prefix = cv_string_get(cvec_i(argv, 6));
}
if ((yspec = clicon_dbspec_yang(h)) == NULL){
clicon_err(OE_FATAL, 0, "No DB_SPEC");
goto done;
}
if ((ph = cligen_ph_find(cli_cligen(h), treename)) == NULL){ /* XXX not used */
clicon_err(OE_PLUGIN, 0, "No such parsetree header: %s", treename);
goto done;
}
/* Store this as edit-mode */
if (clicon_data_get(h, "cli-edit-mode", &api_path) == 0 && strlen(api_path))
;
else
api_path = "/";
if (api_path2xpath(api_path, yspec, &xpath, &nsc, NULL) < 0)
goto done;
skiproot = (xpath != NULL) && (strcmp(xpath,"/") != 0);
if (state && strcmp(db, "running") != 0){
clicon_err(OE_FATAL, 0, "Show state only for running database, not %s", db);
goto done;
}
if (state == 0){ /* Get configuration-only from a database */
if (clicon_rpc_get_config(h, NULL, db, xpath, nsc, withdefaultstr, &xt) < 0)
goto done;
}
else { /* Get configuration and state from running */
if (clicon_rpc_get(h, xpath, nsc, CONTENT_ALL, -1, withdefaultstr, &xt) < 0)
goto done;
}
if ((xerr = xpath_first(xt, NULL, "/rpc-error")) != NULL){
clixon_netconf_error(xerr, "Get configuration", NULL);
goto done;
}
/* Special tagged modes: strip wd:default=true attribute and (optionally) nodes associated with it */
if (defaultstr &&
(strcmp(defaultstr, "report-all-tagged-strip") == 0 ||
strcmp(defaultstr, "report-all-tagged-default") == 0)){
if (purge_tagged_nodes(xt, IETF_NETCONF_WITH_DEFAULTS_ATTR_NAMESPACE, "default", "true",
strcmp(defaultstr, "report-all-tagged-strip")
) < 0)
goto done;
/* Remove empty containers */
if (xml_defaults_nopresence(xt, 2) < 0)
goto done;
}
if (xpath_vec(xt, nsc, "%s", &vec, &veclen, xpath) < 0)
goto done;
for (i=0; i<veclen; i++){
xp = vec[i];
/* Print configuration according to format */
switch (format){
case FORMAT_XML:
if (clixon_xml2file(stdout, xp, 0, pretty, cligen_output, skiproot, 1) < 0)
goto done;
fprintf(stdout, "\n");
break;
case FORMAT_JSON:
if (clixon_json2file(stdout, xp, pretty, cligen_output, skiproot, 1) < 0)
goto done;
fprintf(stdout, "\n");
break;
case FORMAT_TEXT:
if (clixon_txt2file(stdout, xp, 0, cligen_output, skiproot, 1) < 0)
goto done;
break;
case FORMAT_CLI:
if (clixon_cli2file(h, stdout, xp, prefix, cligen_output, skiproot) < 0)
goto done;
break;
case FORMAT_NETCONF:
cligen_output(stdout, "<rpc xmlns=\"%s\" %s><edit-config><target><candidate/></target><config>",
NETCONF_BASE_NAMESPACE, NETCONF_MESSAGE_ID_ATTR);
if (pretty)
cligen_output(stdout, "\n");
if (clixon_xml2file(stdout, xp, 2, pretty, cligen_output, skiproot, 1) < 0)
goto done;
cligen_output(stdout, "</config></edit-config></rpc>]]>]]>\n");
break;
} /* switch */
}
retval = 0;
done:
if (boolcv)
cv_free(boolcv);
if (xt)
xml_free(xt);
if (nsc)
xml_nsctx_free(nsc);
if (vec)
free(vec);
if (xpath)
free(xpath);
return retval;
}
/*! CLI callback: set auto db item
* @param[in] h Clicon handle
* @param[in] cvv Vector of cli string and instantiated variables

File diff suppressed because it is too large Load diff

View file

@ -148,8 +148,6 @@ int cli_show_config_state(clicon_handle h, cvec *cvv, cvec *argv);
int cli_show_auto(clicon_handle h, cvec *cvv, cvec *argv);
int cli_show_auto_state(clicon_handle h, cvec *cvv, cvec *argv);
int cli_show_options(clicon_handle h, cvec *cvv, cvec *argv);
/* cli_auto.c: Autocli mode support */
@ -157,7 +155,10 @@ int cli_show_options(clicon_handle h, cvec *cvv, cvec *argv);
int cli_auto_edit(clicon_handle h, cvec *cvv1, cvec *argv);
int cli_auto_up(clicon_handle h, cvec *cvv, cvec *argv);
int cli_auto_top(clicon_handle h, cvec *cvv, cvec *argv);
#if 1 // Obsolete: Use cli_show_auto_mode instead
int cli_auto_show(clicon_handle h, cvec *cvv, cvec *argv);
#endif
int cli_show_auto_mode(clicon_handle h, cvec *cvv, cvec *argv);
int cli_auto_set(clicon_handle h, cvec *cvv, cvec *argv);
int cli_auto_merge(clicon_handle h, cvec *cvv, cvec *argv);
int cli_auto_create(clicon_handle h, cvec *cvv, cvec *argv);