* More YANG extension functionality,

* See [Augment auto-cli for hiding/modifying cli syntax #156](https://github.com/clicon/clixon/issues/156) and [hiding auto-generated CLI entries #153](https://github.com/clicon/clixon/issues/153)
  * Extensions can be used in augmentations
  * Extension `autocli-op` has been added to add "hidden" commands in the autocli
  * Documentation: https://clixon-docs.readthedocs.io/en/latest/misc.html#extensions
This commit is contained in:
Olof hagsand 2020-12-08 17:21:37 +01:00
parent e1b94d94d2
commit b8641f30bd
14 changed files with 547 additions and 50 deletions

View file

@ -659,10 +659,12 @@ yang2cli_leaf(clicon_handle h,
int key_leaf,
cbuf *cb)
{
yang_stmt *yd; /* description */
int retval = -1;
char *helptext = NULL;
char *s;
yang_stmt *yd; /* description */
int retval = -1;
char *helptext = NULL;
char *s;
char *opext = NULL;
int extralevel = 0;
/* description */
if ((yd = yang_find(ys, Y_DESCRIPTION, NULL)) != NULL){
@ -674,27 +676,40 @@ yang2cli_leaf(clicon_handle h,
*s = '\0';
}
cprintf(cb, "%*s", level*3, "");
/* Look for autocli-op defined in clixon-lib.yang */
if (yang_extension_value(ys, "autocli-op", CLIXON_LIB_NS, &opext) < 0)
goto done;
if (gt == GT_VARS|| gt == GT_ALL || gt == GT_HIDE){
cprintf(cb, "%s", yang_argument_get(ys));
yang2cli_helptext(cb, helptext);
cprintf(cb, " ");
if ((show_tree == 0) || (key_leaf == 1)) {
if (!show_tree || key_leaf) {
if (opext && strcmp(opext, "hide") == 0){
cprintf(cb, ",hide{");
extralevel = 1;
}
if (yang2cli_var(h, ys, helptext, cb) < 0)
goto done;
}
else{
if (opext && strcmp(opext, "hide") == 0){
cprintf(cb, ",hide");
}
}
}
else{
if (!show_tree || key_leaf) {
if (yang2cli_var(h, ys, helptext, cb) < 0)
goto done;
}
}
else
if ((show_tree == 0) || (key_leaf == 1)) {
if (yang2cli_var(h, ys, helptext, cb) < 0)
goto done;
}
if (callback){
if (cli_callback_generate(h, ys, cb) < 0)
goto done;
cprintf(cb, ";\n");
}
if (extralevel)
cprintf(cb, "}\n");
retval = 0;
done:
if (helptext)
@ -787,9 +802,18 @@ yang2cli_list(clicon_handle h,
int retval = -1;
char *helptext = NULL;
char *s;
int list_has_callback = 0;
int last_key = 0;
int extralevel = 0;
char *opext = NULL;
cprintf(cb, "%*s%s", level*3, "", yang_argument_get(ys));
/* Look for autocli-op defined in clixon-lib.yang */
if (yang_extension_value(ys, "autocli-op", CLIXON_LIB_NS, &opext) < 0)
goto done;
if (opext != NULL && strcmp(opext, "hide") == 0){
cprintf(cb, ",hide");
extralevel = 1;
}
if ((yd = yang_find(ys, Y_DESCRIPTION, NULL)) != NULL){
if ((helptext = strdup(yang_argument_get(yd))) == NULL){
clicon_err(OE_UNIX, errno, "strdup");
@ -811,25 +835,25 @@ yang2cli_list(clicon_handle h,
goto done;
}
/* Print key variable now, and skip it in loop below
Note, only print callback on last statement
* Note, only print callback on last statement
*/
list_has_callback = cvec_next(cvk, cvi)?0:1;
if (show_tree == 1) {
if (list_has_callback) {
if (cli_callback_generate(h, ys, cb) < 0)
goto done;
cprintf(cb, ";\n");
cprintf(cb, "{\n");
}
last_key = cvec_next(cvk, cvi)?0:1;
if (last_key){
if (show_tree) {
if (cli_callback_generate(h, ys, cb) < 0)
goto done;
cprintf(cb, ";\n");
cprintf(cb, "{\n");
}
else if (extralevel)
cprintf(cb, "{\n");
}
if (yang2cli_leaf(h, yleaf,
(gt==GT_VARS||gt==GT_HIDE)?GT_NONE:gt, level+1,
list_has_callback, show_tree, 1,
last_key, show_tree, 1,
cb) < 0)
goto done;
}
cprintf(cb, "{\n");
yc = NULL;
while ((yc = yn_each(ys, yc)) != NULL) {
@ -848,7 +872,7 @@ yang2cli_list(clicon_handle h,
goto done;
}
cprintf(cb, "%*s}\n", level*3, "");
if ((show_tree == 1) && (list_has_callback)) {
if (last_key && (show_tree||extralevel)) {
cprintf(cb, "%*s}\n", level*3, "");
}
retval = 0;
@ -1013,8 +1037,7 @@ yang2cli(clicon_handle h,
if ((globals = cvec_new(0)) == NULL)
goto done;
/* load cli syntax */
if (cligen_parse_str(cli_cligen(h), cbuf_get(cb),
"yang2cli", pt, globals) < 0)
if (cligen_parse_str(cli_cligen(h), cbuf_get(cb), "yang2cli", pt, globals) < 0)
goto done;
cvec_free(globals);
/* Resolve the expand callback functions in the generated syntax.